Google Analytics Page Speed Implementation - google-analytics

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'
});

Related

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');

Is it possible to rename the IDs in Google Analytic's Enhanced Ecommerce?

What I'd like to do is change the names of certain fields for better tracking for my website. I don't have a tax, and organization doesn't ship anything. So I was wondering if I could change those fields to something else like shipping -> gifts, and track for any additional funds donated part of their initial donation, that would be awesome. I've tried googling this but there's so much information to sort through that so far I haven't found an answer.
Google's provided code
ga('create', 'UA-XXXXX-Y');
ga('require', 'ec');
ga('set', '&cu', 'EUR'); // Set tracker currency to Euros
ga('ec:setAction', 'purchase', {
id: 'T12345',
affiliation: 'Google Store - Online',
revenue: '28.03',
tax: '2.14',
shipping: '4.00',
});
ga('send', 'pageview');
The code I'd like to use
<script>
var formatDollar = '[[S120:dc:giftAmount]]';
formatDollar.split(",").join("");
var dollarAmt = formatDollar.split(",").join("");
(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-XXXXX-Y');
ga('require', 'ec');
ga('ec:setAction', 'purchase', {
id: '[[S120:dc:transactionID]]',
affiliation: '[[S120:dc:donationFormName]]',
revenue: dollarAmt,
category: '[[S120:dc:giftType]]',
currency: 'CAD'
gifts: '[[S120:dc:additionalGifts]]',
});
ga('ecommerce:send');
ga('send', 'pageview');
Would this work? (I know my UA code needs to be fixed I just forgot what the code was so i just put whatever).
I would test it, but right now the person in charge of all the GA stuff just left for the week, so I won't know if it works or not until they come back (no access to the GA account).
Thank you for your time.
You could change the name, but GA won't recognise those names and know what to do with them. If some parameters don't apply, like shipping, then you could simply leave them out; only id is required. Or if you want to include other parameters, then they would need to be done as custom dimensions:
ga('ec:setAction', 'purchase', {
id: '[[S120:dc:transactionID]]',
affiliation: '[[S120:dc:donationFormName]]',
revenue: dollarAmt,
category: '[[S120:dc:giftType]]',
currency: 'CAD',
dimension1: '[[S120:dc:additionalGifts]]',
});
where dimension1 is configured as a product-scoped custom dimension and contains the value for your gifts.

Google analytics not tracking enhanced ecommerce Data

i manage several hotels on analytics, actually have like 400, and we are updating the ecommerce data to enhanced ecommerce.
when i updated the scripts to e ecommerce on 100 hotels, no one was working, so i ran a test and i found that the isogram analytics code wasnt implemented, fixed that and now 70/100 hotels are getting ecommerce data.
idk if is matter of time to get the data on the last 30 hotels but here is the code, if anyone can see anything wrong, thanks.
<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', 'viewcode', 'auto', {'allowLinker': true});
// Load the plugin.
ga('require', 'linker');
// Define which domains to autoLink.
ga('linker:autoLink',['websites']);
ga('send', 'pageview');
ga('require', 'ec');
ga('set', '&cu', 'MXN');
ga('ec:addProduct', {
'id': '{SkuCode}',
'name': '{ProductName}',
'category': '{Category}',
'price': '{UnitPrice}',
'quantity': {Quantity}
});
ga('ec:setAction', 'purchase', {
id: '{NoReservation}',
revenue: '{Total}',
tax: '{Tax}'
});
ga('send', 'event');
</script>
The analytics code that you are using it looks right, however in order to assure that analytics code is pushed from the all the hotels websites I recommend you to follow the following steps:
1) Check that hotels domains name are correctly in the "linker" parameter
2) Verify that those websites dont't have another GA analytics code that overwrite your code. In order to avoid conflicts I recommend you to rename the "ga" global object to "myga" or something like that. Example:
<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','myga');
myga('create', 'viewcode', 'auto', {'allowLinker': true});
// Load the plugin.
myga('require', 'linker');
// Define which domains to autoLink.
myga('linker:autoLink',['websites']);
myga('send', 'pageview');
...
You can check when a hotel is pushing events if you are using the GA Real-time in the GA dashboard.

Google Universal Analytics E-Commerce Tracking

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

Resources