How to format the userId in Google Analytics script? - google-analytics

for my static HTML webside I embedded this recommended script code to every page of the side.
<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-96597161-1', 'auto');
ga('send', 'pageview');
ga('set', 'userId', {{USERID}});
The question is:
how to format my known Google analytics userId into this place {{USERID}}?
Is it With brackets?, with quotes?
Thanks for your help.

The analytics.js fields reference lists UserID as format type "text", which means you pass it in like you would any other string value in Javascript (i.e. in as quoted string).
In fact the same page has examples:
// Set the user ID when creating the tracker.
ga('create', 'UA-XXXX-Y', {'userId': 'as8eknlll'});
// Alternatively, you may set the user ID via the `set` method.
ga('set', 'userId', 'as8eknlll');

Related

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 Analytics pageviews tracking were not working

(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', 'Tracking ID', 'Domain'); // Replace with your property ID.
ga('send', 'pageview','URL');
You're problem is this line:
ga('create', 'Tracking ID', 'Domain'); // Replace with your property ID.
It should have your GA property Id in it which will begin with UA. Something like this:
ga('create', 'UA-12345678-1', 'auto'); // Replace with your property ID.
You can find the property id under the Home section of Google Analytics. Or alternatively look at Admin->Property->Tracking Info-Tracking Code.

Double google anaylatics on my template?

I'm not a coder though I've learned alot from this board. My friend set up a template for me to work from. there is 2 versions of google anaylatic script in there and i'm not sure which to use?
<script>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-XXXXX-X','auto');ga('send','pageview');
</script>
or....
<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-XXXXX-X', 'auto'); ga('send', 'pageview'); </script>
Thanks so much!
As per the comments, the answer is: leave one with the correct GA Id. Remove any of them in the head tag, and leave the final snippet before closing the body tag.

Universal Analytics cross domain tracking with multiple trackers

So i have Universal Analytics code with multiple trackers:
<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-xxxxxxxx-x', 'auto');
ga('create', 'UA-yyyyyyyy-y', 'auto', {'name': 'second'});
ga('send', 'pageview');
ga('second.send', 'pageview');
</script>
How do i create Cross Domain tracking since UA-yyyyyyyy-y profile is used to gather info from 2 separete domains. With one tracker it is pretty simple:
<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-yyyyyyyy-y', 'auto', {'allowLinker': true});
ga('linker:autoLink', ['other-domain.com'] );
ga('send', 'pageview');
</script>
But what do i do wen i have 2 trackers, how do i set up cross domain tracking for this Universal Analytics profile: UA-yyyyyyyy-y? If you answer my question you will also answer a part of this question:
http://stackoverflow.com/questions/20126897/google-analytics-cross-domain-tracking-with-multiple-trackers
In your example with just a single tracker, you're missing the call to require the linker plugin. Without that it won't work. Perhaps that is the source of your issue.
Regardless, in analytics.js you can call any method on any tracker by simply prefixing the method name with the tracker name. You'll notice your first example already does this in the line ga('second.send', 'pageview').
So, to implement auto-linking on both trackers you'd do something like this:
ga('create', 'UA-yyyyyyyy-y', 'auto', {allowLinker: true});
ga('require', 'linker');
ga('linker:autoLink', ['other-domain.com'] );
ga('send', 'pageview');
ga('create', 'UA-yyyyyyyy-y', 'auto', {name: 'second', allowLinker: true});
ga('second.require', 'linker');
ga('second.linker:autoLink', ['other-domain.com'] );
ga('second.send', 'pageview');
Notice the second. prefix on all the method calls in the second block which corresponds to the name "second" that I gave the tracker.
For more information on the auto-linker plugin, check out this resource:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#autolink
And for more information on the various methods and method signatures, here is the guide:
https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference

Multi google codes in one page

We have this code on the website.
<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-40584814-1', 'webistename.com');
ga('send', 'pageview');
</script>
How can I add another account to it? Can I add something like this?
Can I add something 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', 'UA-40584814-1', 'webistename.com');
**ga('create', 'UA-472147-1', 'webistename.com');**
ga('send', 'pageview');
</script>
I tried that and I can't see the stats on the new account. How can I achieve this? I know I could invite someone via an email but I want to add a completely new account.
Thanks!
Since you're using Universal Analytics (not the original Async version), you have to pass it within the create statement via the options object:
ga('create', 'UA-XXXXXX-X');
ga('create', 'UA-XXXXXX-Y', 'auto', {
'name': 'foo'
});
// later on...
ga('send', 'pageview'); // sends to account XXXXXX-X
ga('foo.send', 'pageview'); // sends to account XXXXXX-Y
The documentation
Asynchronous tracking code my friend ;)
https://developers.google.com/analytics/devguides/collection/gajs/?hl=pl#MultipleCommands

Resources