How to escape apostrophe in Google Analytics Enhanced Ecommerce - google-analytics

Brands with an apostrophe were not appearing in my Google Analytics reports. This was my code:
<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-52143532-4', 'auto');
ga('require', 'ec');
ga('ec:addProduct', {
'id': 'pay-q39-instead-of-q90-fo', 'name': 'Pay Q39 instead of Q90 for haircut (2018-09-17)', 'category': 'Other',
'brand': 'The Barber's Shop', 'variant': 'male', 'price': '5.07' });
// Transaction level information is provided via an actionFieldObject.
ga('ec:setAction', 'detail'); ga('send', 'event', 'ecommerce', 'detail'); </script>
The way I tried to fix it was by using the following code, simply by escaping the apostrophe:
Brands with an apostrophe were not appearing in my Google Analytics reports. This was my code:
<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-52143532-4', 'auto');
ga('require', 'ec');
ga('ec:addProduct', {
'id': 'pay-q39-instead-of-q90-fo', 'name': 'Pay Q39 instead of Q90 for haircut (2018-09-17)', 'category': 'Other',
'brand': 'The Barber\'s Shop', 'variant': 'male', 'price': '5.07' });
// Transaction level information is provided via an actionFieldObject.
ga('ec:setAction', 'detail'); ga('send', 'event', 'ecommerce', 'detail'); </script>
I am simply using 'brand': 'The Barber\'s Shop' instead of 'brand': 'The Barber's Shop'. However, I still do not see data in Google Analytics about brands whose names have an apostrophe in it. Is this the correct way to escape apostrophes in Google Analytics? Thank you.
UPDATE 1: If Google Analytics does not like the 'brand': 'The Barber\'s Shop' solution instead of 'brand': 'The Barber's Shop', then I am planning to write some code to remove any appearance of an apostrophe altogether and have 'brand': 'The Barbers Shop' instead. It would surprise me though, if Google Analytics cannot not handle apostrophes somehow.

It may sounds silly, but have you tried something like the following?
<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-52143532-4", "auto");
ga("require", "ec");
ga("ec:addProduct", {
"id": "pay-q39-instead-of-q90-fo",
"name": "Pay Q39 instead of Q90 for haircut (2018-09-17)",
"category": "Other",
"brand": "The Barber's Shop",
"variant": "male",
"price": "5.07"
});
// Transaction level information is provided via an actionFieldObject.
ga("ec:setAction", "detail");
ga("send", "event", "ecommerce", "detail");
</script>
I replaced the single quotes with double quotes, this should avoid the confusion and "fix" the problem.

See UPDATE 1 in the question. I ended up removing apostrophes altogether, so I have this now:
'brand': 'The Barbers Shop'
It still surprises me though, to think that Google Analytics cannot handle apostrophes, if that is the case.

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.

google-analytics: Conversions not showing on Transactions

I have installed Google tag manager and the following tag is firing and being count accordingly for my E-commerce conversion (tag called 'Google Analytics eCommerce'):
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-76425812-2', 'auto');
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
'id': inspectionID
});
ga('ecommerce:addItem', {
'id': inspectionID,
'name': 'Inspection',
'quantity': 1
});
ga('ecommerce:send');
</script>
Nevertheless I am trying to create a tag and to avoid firing multiple tags for the same conversion. For that, I am creating a universal tag to save the utm_source and I am storing it on a Custom Javascript Variable.
TAG:
<script type="text/javascript">
var url = window.location.href;
var keyToFind = 'utm_source';
if(url.indexOf(keyToFind + '=') > -1)
{
var valueFound = url.substr(url.indexOf(keyToFind + '=') + keyToFind.length + 1).split('&')[0];
sessionStorage.setItem(keyToFind, valueFound);
}
</script>
Java Script Variable:
function()
{
return sessionStorage.getItem('utm_source');
}
When I use this variable to trigger any other tag (this is not triggering the Google Analytics eCommerce tag at all), GA stops considering the conversions.
Anyone may have an idea about what can be happening in the case?
Thanks,
Helton

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 enhanced ecommerce not tracking

I have been tormented with this problem for several days, dont know whats the deal.
I have this code on the product page:
ga('create', 'UA-XXXXXXXX-1');
ga('require', 'ec');
ga('ec:addProduct', {
'id': '145645',
'name': 'BlaBlaBla',
'category': 'BlaBlaBla',
'brand': 'BlaBlaBla',
});
ga('ec:setAction', 'detail', { 'step': 1 });
ga('send', 'pageview');
GA debugger plugin output indicates that everything is OK:
Running command: ga("require", "ec")
Running command: ga("ec:addProduct", {id: "145645", name: "BlaBlaBla", category: "BlaBlaBla", brand: "BlaBlaBla"})
Running command: ga("ec:setAction", "detail", {step: 1})
Running command: ga("send", "pageview")
But no data is seen on the GA page!
The enhanced ecommerce plugin for now is performing only on the products pages for the reason of testing. IP filter is off, all script and adblockers disabled.
I think my problem is lying on the top, just can`t figure it out.
Any help would be appreciated!
EDIT:
On the purchase page Enhanced e-commerce is working, I see the data on GA page!
ga('create', 'UA-XXXXXXX-1');
ga('require', 'ec');
ga('ec:addProduct', {
'id': '137883',
'name': "BlaBlaBla",
'price': 2790.00,
'quantity': 1
});
ga('ec:setAction', 'purchase', {
'id': '1595',
'revenue': 2790.00,
});
ga('send', 'pageview');
The {step:1} is intended to be used in the checkout action and you're using it in the detail one.
From enhanced ecommerce tracking documentation:
step (Number): A number representing a step in the checkout process.
Optional on checkout actions.
I would remove the third parameter changing:
ga('ec:setAction', 'detail', { 'step': 1 });
by
ga('ec:setAction', 'detail');

Resources