Google Universal Analytics E-Commerce Tracking - google-analytics

So I've got the correct ecommerce tracking code for the new Google Universal Analytics, but I'm confused about how the variables populate? How do I get the correct item, price, category or number of items (for addItem) into this script? I've been looking all over the web, I don't think I'm seeing the full picture here.
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-44464798-1', 'safeway.com');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
'id': '200028184', // Transaction ID. Required.
'affiliation': 'Safeway', // Affiliation or store name.
'revenue': '11.99', // Grand Total.
'shipping': '5.00', // Shipping.
'tax': '1.00' // Tax.
});
ga('ecommerce:addItem', {
'id': '200028184', // Transaction ID. Required.
'name': 'Apples', // Product name. Required.
'sku': 'CVC2US', // SKU/code.
'category': '', // Category - you can put some category if we have such
'price': '11.99', // Unit price.
'quantity': '1' // Quantity.
});
// If there are additional items they should be added the same way. The sum of all Items prices should match with the "revenue" data from above.
ga('ecommerce:send'); //
</script>

I don't know what language your site is written in. Assuming its PHP or asp.net you will need to go in to the code that builds the site itself and add these tags as well.
So to answer your question, the variables and values are populated by you in the code behind of your site. Probably at the same time as when the user is checking out.

Instead of values in your script, you want to refer to the values of variables.
These would get assigned by the shopping cart code at the moment of purchase completion.
The Google documentation has a PHP example.
Analytics JS Ecommerce Dev Notes

Related

Google Analytics Page Speed Implementation

I am wondering how to implement correctly google analytics page speed measurements and how to change the sample size correctly:
As seen below in the code box we currently Google analytics including eCommerce running on our site. Now I would like to also increase the sample sizeof speed tracking (the percentage of pageviews used for speedtracking, usually 1%) to 100% on our staging system.
As far as I understood I can do that by following this instructions.
My question not is if I just add it to the existing create method in my analytics snippet or do I have to create another method below.
If I understood correctly I would now do this:
replace
ga('create', 'UA-XXXXXXX-1', 'ricomprostaging.it');
with
ga('create', 'UA-XXXXXXX-1', {'sampleRate': 5}, 'ricomprostaging.it');
Please advise if I am on the right track. I have placed the entire code of the current snippet below.
Thank you,
Fabian
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-1', 'ricomprostaging.it');
ga('send', 'pageview');
ga('send', 'pageview', 'search_query');
ga('require', 'ecommerce', 'ecommerce.js');
function trackEcommerce() {
this._addTrans = addTrans;
this._addItem = addItems;
this._trackTrans = trackTrans;
}
function addTrans(orderID,store,total,tax,shipping,city,state,country) {
ga('ecommerce:addTransaction', {
'id': orderID,
'affiliation': store,
'revenue': total,
'tax': tax,
'shipping': shipping,
'city': city,
'state': state,
'country': country
});
}
function addItems(orderID,sku,product,variation,price,qty) {
ga('ecommerce:addItem', {
'id': orderID,
'sku': sku,
'name': product,
'category': variation,
'price': price,
'quantity': qty
});
}
function trackTrans() {
ga('ecommerce:send');
}
var pageTracker = new trackEcommerce();
</script>
Yes you need to replace the create statement on all pages. However sampleRate is the wrong option: this option defines the sample rate for Google Analytics as a whole. For sampling speed, you want to use siteSpeedSampleRate. As for the code, I would use the following:
ga('create', 'UA-XXXXXXX-1', {
'siteSpeedSampleRate': 5,
'cookieDomain': 'ricomprostaging.it'
});

Missing data in Google Analytics Ecommerce

When I compare transactions in my production database with transactions in the Google Analytics Ecommerce dashboard, it is evident that I am missing a significant amount of data. I made a purchase for testing purposes in my website and in the "Thank you page" that appears after a successful purchase, I saw the following Google Analytics tracking code when I inspected the element:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '[My Google Analytics tracking ID]', 'auto');
ga('require', 'ec');
ga('ec:addProduct', {
'id': '[Product ID]', 'name': '[Product name]', 'category': '[Product category]',
'brand': '[Product brand]', 'variant': '[Product variant]', 'price': '[Product price]', 'quantity': 1 });
// Transaction level information is provided via an actionFieldObject.
ga('ec:setAction', 'purchase', { 'id': '[Transaction ID]', 'affiliation': '[Organization where the transaction took place]', 'revenue': '[Revenue value]' });
ga('send', 'pageview');
</script>
This transaction took place about 24 hours ago. When I go to the Google Analytics Ecommerce dashboard, to the "Sales Performance" section, when I look for a transaction with the ID that I sent in 'id': '[Transaction ID]' above, I cannot find that transaction:
In the oval above I am providing the [Transaction ID] but it is not found as you can see. I have found many cases in my database where there are transactions that I cannot find in Google Analytics Ecommerce.
Google Analytics is showing some transactions but not all of them. I examined the code sent to Google Analytics and it is correct, no syntax errors, everything is perfect. Why is the Google Analytics Ecommerce "Sales Performance" section missing transactions? Thank you.
UPDATE 1:
For 'name': '[Product name]', I am sending as the [Product name] a string of 145 B. To give you an idea, it would be approximately this long:
¡Lore T99 mi psumi ss E325 impl Dummyte Xtífthe + Printingand
Typesetting (Iñd Ustrylore, Ips um Hasbee, Nthein) Dustr Ss Ttandar!
(2018-01-05)
The text contains the following special characters:
¡
í
+
(
ñ
,
)
!
-
Is the length the problem (145 B), or the fact that I am sending special characters in the [Product name]?
The problem was that I was taking [Product category] from a database, and to my surprise many of those values had an empty line after the string. That was causing the Google Analytics code to look like this:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '[My Google Analytics tracking ID]', 'auto');
ga('require', 'ec');
ga('ec:addProduct', {
'id': '[Product ID]', 'name': '[Product name]', 'category': '[Product category]
',
'brand': '[Product brand]', 'variant': '[Product variant]', 'price': '[Product price]', 'quantity': 1 });
// Transaction level information is provided via an actionFieldObject.
ga('ec:setAction', 'purchase', { 'id': '[Transaction ID]', 'affiliation': '[Organization where the transaction took place]', 'revenue': '[Revenue value]' });
ga('send', 'pageview');
</script>
Notice how [Product category] does not appear in a single line. That was the cause of the problem. Surely that was interpreted as a JavaScript syntax error and Google Analytics never processed that entry, which would be a synonym of not running the Google Analytics script. The fix was to remove those unnecessary lines from my database. Alternatively, if you are using PHP for instance, you could use the PHP trim function ("Strip whitespace (or other characters) from the beginning and end of a string": http://php.net/manual/en/function.trim.php) so that your PHP code takes care of fixing those extra lines at the end of the category names. But I think the best approach would be to fix the category names in your database because otherwise a similar situation could potentially cause similar errors in other APIs or scripts that you have today or write in the future. If your code is not written in PHP, find an equivalent to the trim function in the programming language that you use.

I can not get Google Analytics to send commerce data

I rewrote this post to be clearer and to simplify the problem that I am having.
I am trying to get e-commerce data to send to Google Analytics. I am using the most basic set up to send one test transaction based on their example setup.
Google tag manager is showing the transaction, but it is not making it to the Google Analytics dashboard Conversions > Ecommerce > Overview
I have enabled Ecommerce under view in the admin panel, but no data? This is probably something simple - but I can't find it. (sample GA ID, using the proper ID in my code and the pages are tracking fine in the dashboard, just no Ecommerce data)
Here is my code:
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1234567-1', 'auto');
ga('send', 'pageview');
ga('require', 'ecommerce');
console.log('order placed');
ga('ecommerce:addTransaction', {
'id': '1234', // Transaction ID. Required.
'affiliation': 'Acme Clothing', // Affiliation or store name.
'revenue': '11.99', // Grand Total.
'shipping': '5', // Shipping.
'tax': '1.29' // Tax.
});
ga('ecommerce:addItem', {
'id': '1234', // Transaction ID. Required.
'name': 'Fluffy Pink Bunnies', // Product name. Required.
'sku': 'DD23444', // SKU/code.
'category': 'Party Toys', // Category or variation.
'price': '11.99', // Unit price.
'quantity': '1' // Quantity.
});
ga('ecommerce:send');
ga('ecommerce:clear');

Google Analytics ecommerce tracking conversion inside same domain iframe

I am trying to enable ecommerce tracking on a site.
The payment part takes part inside an iframe of a third party. Once complete the iframe refreshes to a page on the main site in the same domain as the main site.
Google aynalitics is initially set up when the top page first loads with
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '******', 'example.com');
ga('send', 'pageview');
This part is working fine. Page tracking is working.
Once the payment has completed the iframe loads this code:
window.parent.ga('require', 'ecommerce');
window.parent.ga('ecommerce:addTransaction', {
'id': '1234',
'affiliation': 'Example',
'revenue': '4.00',
'shipping': '1.25',
'currency': 'GBP'
});
window.parent.ga('ecommerce:addItem', {
'id': '1234',
'name': 'test product',
'sku': 'TEST-SKU',
'category': 'Test Category',
'price': '2.75',
'quantity': '1'
});
window.parent.ga('ecommerce:send');
I can see that network traffic is generated from this but nothing is showing up on the ecommerce analytics pages.
The ecommerce tracking options are set up on the analytics admin page
Turns out the data just hadn't come through yet and everything was working fine. I thought the 24 hour delay was just after you turn it on, but it seems there is always at least 12 hours between transactions being made and their showing up in analytics.

Analytics e-commerce tracking not tracking anything

I'm building a custom webshop on Laravel and i started on an e-commerce tracking plugin.
According to me, it should work, but it doesn't. Could someone tell me what i am doing wrong?
Here's my code on the thankyou page:
in the head section:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1443260-52', 'auto');
ga('send', 'pageview');
</script>
And further down the page:
<script>
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': '133', // Transaction ID. Required.
'affiliation': 'Demo store 1', // Affiliation or store name.
'revenue': '40.00', // Grand Total.
'shipping': '15.00', // Shipping.
'tax': '4.34' // Tax.
});
ga('ecommerce:addItem', {
'id' : '133', // Transaction ID. Required.
'name' : 'Test product', // Product name. Required.
'sku' : '12', // SKU/code.
'category' : 'Kleur - Rood | Maat - 28inch', // Category or variation.
'price' : '25.00',// Unit price.
'quantity' : '1', // Quantity.
'currency' : 'EUR'
});
ga('ecommerce:send');
</script>
I have enabled e-commerce tracking in Analytics, and pageviews are registered
Your call looks fine to me. There are a few things you should be aware of.
Newly created Analytics accounts can take 48 -72 hours to start showing data.
The standard reports of which ecommerce is one, can take 24 hours to display data. While I have seen them display data in 4 hours this is not always the case and the data is not normally very actuate.

Resources