This is part 2 in my series on Google Analytics e-commerce tracking. In part 1 I described, at a conceptual level, how GA e-commerce tracking works. In this post I’ll get into the specifics of the code and how to install it.
This isn’t the most exciting stuff (that will be part 3), but a correct setup leads to correct data. :)
Step 1: Activate the Reports
The first step in setting up GA e-commerce tracking is enabling the e-commerce reports. Log into GA and edit the profile settings. Specify that your site is an e-commerce site. This activates the e-commerce reports.
There are other e-commerce settings that don’t get much use (unless you’re an international site). You can specify one of 25 different currencies (wow!) and the number of decimal places you would like displayed (1,2 or 3). Even if you use USD you can specify 3 decimal places. Go ahead and try it, it’s interesting.
Remember, e-commerce reports is a profile setting that is “off” by default. You’ll need to activate the reports for each new profile you create.
Step 2: Tag your Receipt Page
I know this seems like a silly step, but make sure you add the GA tracking code you your receipt page. You must have the standard GA tracking code on your receipt page in order to track transactions. The reason is that the e-commerce tracking code is stored in the ga.js. If this file is not included on the receipt page then you can’t track transactions.
Step 3: Install the Code
This is the hard part: code construction. As we learned in part 1, GA uses a JavaScript collection technique to track e-commerce transactions. Your server code must inject transaction information into the GA JavaScript before sending the receipt page back to the browser. When the receipt page renders in the visitor’s browser the JavaScipt executes and sends the transaction info to GA.
Let’s take a look at the code:
< script type="text/javascript" >
pageTracker._addTrans(
"order-id", // required
"affiliate or store name",
"total",
"tax",
"shipping",
"city",
"state",
"country"
);
pageTracker._addItem(
"order-id", // required
"SKU",
"product name",
"product category",
"unit price", // required
"quantity" //required
);
pageTracker._trackTrans();
< /script >
The first thing that you’ll notice is that there are three distinct parts to the JavaScript. Each is a different method. The first section, identified by the
_addTrans()
method, creates the transaction and stores all the information about the transaction.
The second section, identified by the _addItem()
method, is used to add an item to the transaction. You need to create an _addItem()
section for each different item, or SKU, in the transaction. The order ID in the _addItem()
method must be the same order ID used in the _addTrans()
method. That’s how GA ties an item to a transactions.
The final section is the _trackTrans()
method. This method actually sends the data to GA by requesting the __utm.gif file once for the transaction and once for each item in the transaction. So if you have 3 different SKUs in a transaction there will be 4 requests fot the __utm.gif.
The above JavaScript can appear anywhere on your receipt page. Just make sure that it appears after the main GA page tag. The reason is that the e-commerce code is part of the pageTracker
object. If the pageTracker
object has not been created then you can’t call the e-commerce methods.
Just to reiterate a point: you must create server side code that outputs the transaction data in the format above. If you do not have access to your shopping cart code, and your cart provider does not provide e-commerce tracking, then you may be out of luck.
Once the code has been installed you should begin to see e-commerce data in your reports.
Notes and Suggestions
Like many things in Google Analytics, there are some things that can trip you up:
- If you do not wish to pass a certain piece of data to GA then do not add anything between the quotation marks. The only required fields are the order ID, the unit price of each item and the quantity of each item. Everything else is optional.
- Do not use currency identifiers or commas (to separate thousands) in any of the numeric fields (i.e. total, tax, shipping). These characters have caused problems in the past.
- Each piece of transaction data that you send to Google Analytics becomes a data element in GA, just like any other piece of data in GA (geo data, campaign data, browser data, etc.). You can do all sorts of neat things with filters and e-commerce data. More on this later.
- The affiliate or store name field is no longer used in the GA reports. However, the data is still collected by GA so you can use it in a filter.
- The transaction geographic data collected by GA is no longer used BUT it is collected. Google Analytics determines the location of the user, and thus their transaction, based on their IP address. The old version of the GA tracking code used the data specified in the transaction. Like the affiliate field, the data can be used in a filter.
Up Next
In the next post I’m going to talk about some creative ways to use e-commerce tracking. Specifically, why I believe that everyone should use e-commerce tracking on their website.
This is part 1 in a multi-part series on e-commerce tracking:
Google Analytics E-Commerce Tracking Pt. 1: How It Works
Google Analytics E-Commerce Tracking Pt. 3: Why EVERYONE Should Use It
Google Analytics E-Commerce Tracking Pt. 4: Tacking Lead Gen Forms
How is this data sent to Google? From what I see in the above code, there is no actual way to make sure the data is encrypted before it is sent out. It looks like it is sent in plain text to Google.
Is this really the case?
Thanks so much for these and other articles.
What I like the most is the simple language, detailed explanations, and images of how to implement code, modify and change code for various results.
– Your material has helped me so much in the way of Google Analytics, especially with a recent U.K. PPC client that has several sites and services. I read, collected, and passed along your articles to their programmer and we are on our way to more informed analytics understanding and reports.
Keep up the excellent work!
A new admirer.
Alex
I think I said this last time, but this is a great quick-reference for anyone beginning to do e-commerce implementations – very concise and to the point.
Hi, Thanks for your informative post, can you share a detailed example on how to implement this ecom code. I have been trying to get it to work on my website without much luck, GA tracks some transactions but doesnt track others.
Thanks,
Ram
Alex & Jeremy,
Thanks so much. I’m really glad you guys find these posts useful. If you have any suggestions as to how I can improve them please let me know. Or if there is s topic you would like discussed please let me know.
Thanks again,
Justin
Chris,
The data is sent to Google via a request for the __utm.gif file. The data is not encrypted, however, if your receipt page is on a secure server, you can use the secure version of the GA tracking code. This creates a secure request for the __utm.gif file.
Thanks for the question,
Justin
Hi Ram,
It’s not uncommon for GA to miss some transactions. As long as your data is within 10% of actual then you’re doing OK. Many things can contribute to missing transactions… visitors blocking cookies or disabling JS. It may be that people are navigating away from your receipt page before the transaction is sent to GA.
I know that doesn’t help much, but, as I said before, if you’re within 10% of your actual numbers then you’re doing well.
Sorry I did not have a better answer for you.
Justin
Hi Justin,
Thanks for responding, I changed the code to make it more useful & made a few mistakes. I had extra which I realized was a mistake thanks to your post.
Thanks
Ram
We do a lot of banner advertising on third-part sites. These ads are served through Dart/Double Click, but we want to track via Google Analytics. Any conflicts there? We’ve had some problems utilizing the GA-generated URLs with Double Click in the past, but we’re having a hard time deteriming the root cause.
Any insights would be helpful.
Thanks,
Lisa
Hi Justin,
Your blog is excellent by the way, im finding it extremely helpful!
Just a quick question (though it might come up in your third ecommerce tracking part):
We have a site with a US and UK separate URL
(oursite.com/uk and oursite.com/us). Include filters ensure all our stats are separated fine. However for the commerce tracking, it seems the profile filters do not apply – instead both profiles add together both the UK and US revenue figures etc. Are custom filters able to help me solve this problem and if so how would i go about this?
Many thanks for your time,
Kunal
Great article. helps a lot, thanks.
Hi Justin,
Thanx for this useful information. I have the following question regarding Order with multiple items. Can we repeat pageTracker._addItem ()?
Regards,
Anand
Justin,
I appreciate your posts–your delivery is very clear and approachable. Quick question about GA e-commerce tracking: when e-commerce reports are activated, are conversions still based on pageviews or does the new code allow conversions to be calculated by transaction?
Thank you for your insight,
Lisa
Hi,
good post…
i have a question: i want to track more information in the _addItem or _addTrans
An example:
pageTracker._addItem(
“343212”, //order ID
“DD4444”, //sku
“Lava Lamp”, // product name
“Decor”, // category or product variation
“34.99”, // price
“1” //quantity
“amsterdam” //Departure
“NewYork” //Destination
ect..
);
Is that possibly in GA ??
Grtz..swoany
Another great article Justin.
We have been using GA transaction information for years now. But recently it occured to us that GA’s ROI and Margin metrics may be skewed due to the fact that they reflect gross and not net revenue. We were wondering if perhaps it would be better to send net profit totals for each item and the net profit total.
For example, if we sell an item for $2.00 that costs us $1.00 to purchase wholesale, should we use $1.00 for the net profit instead of the $2.00? Do you think this provides better ROI and margin information?
Or should we leave it at the retail price and do the analysis on the backend, ignoring GA for ROI and margin numbers?
I wish GA would have a product cost field we could populate.
Hey Justin.
For readers who are also using Google Website Optimizer, I just published a simple way to get combine GWO and revenue data in GA:
http://www.prusak.com/archives/2008-02-28/revenue-tracking-with-google-website-optimizer/
– Ophir
Hi Justin,
Just wondering when you are going to publish Part III to this helpful article?
Hey Justin, Where ya been? Keep these great posts coming….
Bought your nice book. Which tips are no longer valid? I wonder because the tracking code is old. Thanks.
Great article.
Am I right in thinking that using this code will allow us to check the price/value of purchases made by a customer?
It’s sort of making sense to me. It’s the code installation that is confusing me :(
Thank You
Justin:
We’ve implemented the ecommerce tracking code exactly as you’ve described, but are still seeing 0 conversions in our goal funnel and $0 on the ecommerce tab, despite making several test purchases.
Any suggestions on where to begin trouble-shooting?
Thanks.
I noticed eCommerce collects
pageTracker._addTrans(“order-id”, “affiliate or store name”,”total”,”tax”,”shipping”,”city”,”state”,”country”);
How do I find the “affiliate or store name”,”city” and “state” information? I tried to check out the map overlay but that just gives me the internet service provider location.
thanks,
-John
Hi, I noticed eCommerce collects the data:
pageTracker._addTrans(“order-id”,”affiliate or store name”,”total”,”tax”,”shipping”,”city”,”state”,”country”)
from all my transactions. I am able to recover “order-id”,”total”,”tax”,and “shipping” from the eCommerce report. I cannot retrieve the “city”,”state”,”country” data acquired.
I tried viewing the map-overlay but I think this tracks visitors from their ISP. Am I wrong? Is there a way to obtain the “city”,”state”,”country” data acquired in eCommerce?
thanks,
-John
Lisa,
You should be able to use GA to track DoubleClick ads. I would make sure that there are no redirects that are stripping off the campaign parameters. That’s usually the culprit when it comes to campaign tracking issues.
Thanks for reading,
Justin
HI Kunal,
Segmenting e-commerce data is a bit different tan segmenting clickstream traffic. It can be done, but you need to segment it using the Affiliate field.
What we’ve done in the past is to populate the Affiliate field with something similar to the country value into the affiliate field and then create an include filter based on the affiliate field.
Hopefully that puts you on the right track.
Thanks for reading,
Justin
Anand,
Yes you can in fact have multiple occurance of adItem(). In fact, you should have a different addItem() section for each different SKU or product in the transaction.
Justin
Lisa,
Excellent question. When e-commerce tracing is used, goal conversion for a purchase are based on purchases, not pageviews.
Thanks for reading,
Justin
Hi Grtz,
Yes, you can track more information, but you can not add it in the format that you describe. The data must fit in the format that is specified by Google. You must place any additional informatuon that you want to track in the product field, category field or some other field that Google has defined.
Good luck,
Justin
Hi Chuck,
You make a great point. I think the key is to configure Google Analytics to fit your needs. If you want to measure ROI and Margin based on Net then configure GA appropriately.
Great observation.
Justin
Very helpful write-up — got it up and running in 15 minutes.
Thanks!
Hi Justin,
Very useful post! Here a **late** question: you suggest that the basic GATC be somewhere on the page before the transaction code, whereas B. Clifton shows it as embedded in it (p. 117). Does either method work the same?
Thanks!
Hi Jaques,
Good to hear from you. Thanks for the question. I don’t have Brian’s book in front of me, but I’m 99% sure it shows the e-commerce code below the main GA tracking code. The format I reference is practically identical to Brian’s. Both will work.
The key thing is to make sure that you’ve created the pageTracker object before you invoke addTrans or addItem.
I hope that makes sense.
Thanks for the question and thanks for reading.
Justin
Anyone aware of a Magento module that integrates this?
Hi Collective,
While I am unfamiliar with Magento, I just did a quick search on Google and found this result.
Hopefully it has the information that you need.
Best of luck and thanks for the question.
Justin
Justin,I put the GA code into the js file,
http://www.yidaba.com/js/topseo.js in page head;
http://www.yidaba.com/js/bottomseo.js in page bottom.
For example:http://management.yidaba.com/cwgl/rzzd/6789221.shtml
Reasonable to do so?
Hi George,
Yes, you can set the code up that way.
Thanks for the question,
Justin
Hi, could we get the ex-ante surf information before the transaction? I means the pages visited and the average time passed on the site before each purchase?
Thanks.
Hi Jinqiu,
There are not a lot of path analysis options in GA. I would suggest using the $Index value is you’re looking to gauge the value of content.
Hope that helps,
Justin
Great post, Justin. I’m at my wits end trying to customise the e-commerce script to my situation, and am hoping you could provide a small bit of advice.
I’m running a Google Adwords campaign that drives traffic to a survey page.
I understand that it’s possible to track which ad groups lead to a survey completion by using e-commerce goals.
But I have no idea how to alter the e-commerce script to include the one variable that I would like to track (i.e. the survey response ID).
Any suggestions for how a non-techie could learn how to do this?
I have installed the e-commerce tracking code on my receipts page and have been able to track checkout conversions,however my average order value and revenue indicators are not reporting, can anyone help me trouble shoot this problem.
Thanks
TC
Moe,
If you want to pull one specific value into the ecommerce code then you need to add it to the JavaScript that appears on the receipt page. I suggest adding it to the product name, product description or product category field. Then use the corresponding report to find your data.
TC,
There’s probably an issue with the data you’re collecting for revenue and transactions. The two metrics that you’re having trouble are directly related to the data collection, so I would check that first.
Thanks for the questions,
Justin
Thank you very much for this article. Amazingly, the Google documentation incorrectly tells users to put an ‘item number’ where the order id should be placed when using _addItem.
See this page:
http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55528
In the example on the page, they even use clearly different values for ‘order id’ and ‘item number’:
pageTracker._addTrans(
“1234”,// Order ID
…
);
pageTracker._addItem(
“5678”,// Item Number
…
);
…this caused analytics to register empty transactions (without any products) and create a new transaction for each item with amount £0. It made a complete mess of my stats and I couldn’t figure out why until I read your article. All makes sense now.
Thank you!
What about any other character problems? I found that double quotes in the product name is a problem. I am thinking single quotes would also be a problem. I am not sure about parens. Are there any others that you’ve come across? GA is only tracking about 25% of our transactions so far.
Hey Nathan,
You’re right, you want to avoid quotes and any other special characters in the names of products or categories. I would also avoid parens and anything that involves the SHIFT key :)
Thanks for the comment,
Justin
Great comments on reasons why GA sometimes not capturing all the transactions. THANK YOU.
In the transaction reports in Google Analytics why would 20-25% of the transactions be missing?
Hi,
I liked the article on using e-commerce on lead generation forms to get more detailed in formation. I was wondering what modifications you would recommend if I wanted to get more detailed information from my booking online form. The information required would be location of hotel they select, the hotel they select, the room the user selects, number of people etc.
Thanks
@TMosley: Missing transactions in GA Ecommerce can be for a few different reasons. The most common problem is visitor navigating away from the receipt page before the GA tracking code has a chance to send the data to Google. You may want to move your ecommerce tracking code to the top of the page to increase the likelihood that it’s sent to Google.
@Aswati: You’re on the right track. If you’ve got a booking form then think of the reservation as a product. What are the characteristics of a reservation? Hotel, room type, etc. You may want to consider capturing visitor choices as part of the ‘product’ information.
Thanks for the comments,
Justin
Still a little unclear on the coding to be put on the receipt page. So are we supppose to put the code into our thankyou.htm page with all the possible variables of an order?
ie. 1 gallon, color blue, etc..
or do I put this code into one of our asp page that dynamically retrieves the appropriate page of the orders coming in?
@Jon: The latter. You want to add code to your asp page. The code needs to generate the appropriate GA JS for each order in real time.
Hope that helps.
Great article.
I have implemented e-commerce tracking but would also like to pass 2 additional things with every transaction:
1. Delivery Method (Delivery vs Carry out)
2. Payment method (Cash vs Credit)
I thought of ‘cheating’ the system and using the State and Country fields respectively to pass this data since they aren’t of much use to my implementation.
However when I perform an advanced segment in the reports, Bill region and Territory have no data.
So, is there a way to actually cheat analytics with the fields to measure other metrics? Or can someone propose a better way to track the aforementioned metrics (Delivery and Payment).
Thanks in advance.
@Yannis: Try using the Affiliate field. GA uses IP based data for the ecommerce geo data. They have deprecated the geo fields in the ecommerce tag.
Thanks for the question!
Justin
Can any one tell me why this isn’t working. GA only detects one item. Not both of them.
pageTracker._trackPageview();
pageTracker._addTrans(“201004211005109519″,””,”1088.00″,”217.60″,”39.00″,”HOLLVIKEN”,””,”Sverige”);
pageTracker._addItem(“201004211005109519″,”366″,”Gratulationskort”,”card”,”100.00″,”10″);
pageTracker._addItem(“201004211005109519″,”100″,”Kuvert”,”card”,”1.00″,”10″);
pageTracker._trackTrans();
Only the first item is detected, not the second?????
PLEASE HELP!!!
@Smukke: The code looks good, it may be an issue with the quotes. Check the headers and make sure there is a GIF request for each of the items in the code. If they’re not being sent then there is some type of issue with the JS. Which is usually the problem.
Hope that helps,
Justin
We are seeing duplicate orders, and also missing orders. I have asked our vendor to move the GA tracking code higher up on the page, so that it will load before the customer moves on to another page. But they are resisting, saying Google instructs that the code be placed before the closing tag – they also refuse to implement the asynch code. It does say that here: http://support.google.com/googleanalytics/bin/static.py?hl=en&selected=a9_a9h1_a9h1t2&page=troubleshooter.cs&ctx=gatsc_a9_a9h1_a9h1t2_55574&problem=gatsc
But I was hoping there was newer instructions out there, something official from Google, that would back up my request to move the code up.
Any other recommendations to fix our missing or duplicate orders would be greatly appreciated. Thanks!
@Jodi: If you’re missing more than 10% of the orders then there is definitely a code problem. Depending on the size and weight of the page moving the code may not help. But I think that’s a good first step. You can try sending them to this article about placing the async code in the head.
As for the duplicate orders, this is almost always a problem with outputting the ecommerce tracking code multiple times on the receipt page. Which is an issue that the vendor needs to clean up. The other thing to check is all the fringe cases when a person can view their order summary. Make sure the ecommerce code is not embedded in any of those pages.
Good luck.
Great tips on implementation, but where do you get the code?
@Janine: There is no tag to actually grab. You need to have your server output the transaction in the ecommerce format. You can find the ecommerce instructions on the GA code site. But remember, you need to have server level code that outputs the transaction in the GA format.
Hope that helps.
Hello,
Could you please clarify following for me.
We have e-commerce site that sells subsriptions.
To get subscription customers go through the funnel:
1. Register
2. Select product
3. Basket
4. Confirm basket
5. Pay
6. Thank you page
On stage 5 (pay) we redirect customer to payment-processing-site (billing company). After successful payment payment-processing-site invites customers to return to our site on our thank you page. But most of the customers don’t click on link. They go directly to our site and miss the thank you page.
Approximately 50% of customers fails with the payment.
We also have 4 different payment-processing-companies with 4 different independent sites.
How can we implement GA e-commerce in our case?
Thank you.
@Maximus: Implementing ecommerce depend on the third-party processing site. If they will let you add Google Analytics to your pages on their site, then you can track transactions. However, you will also need to implement cross-domain tracking to correctly attribute transactions to marketing activities. But, if they do not let you add the code, then you don’t have many options.