Google analytics userId configuration - google-analytics

currently want to track unique users in GA (remember their session, etc). Having the problem of where to place the userId code inside the tracking code?
Is it correct to place it in between create and send? or does it matter? will it have a negative effect if I place it before create or after send?
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
Beginner level with GA.

You can put it in the create command via the optional fieldsObject, or you can set it on the tracker after it's created (but before the pageview is sent).
You cannot put it before the create command, and if you put it after the pageview is sent, it'll be too late and won't apply to that hit.
In other words, either of these two variations will work:
ga('create', 'UA-XXXXX-Y', 'auto', {userId: USER_ID});
ga('send', 'pageview');
or:
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'userId', USER_ID);
ga('send', 'pageview');

Related

Google Analytics UserID Tracking Code

I'm setting up UserID tracking in Google Analytics. Google's documentation displays this piece of code to push UserIDs into analytics:
ga('create', 'UA-XXXX-Y', { 'userId': 'USER_ID' });
ga('send', 'pageview');
However, when I'm in my Google Analytic's settings area, it displays the following piece of code to include UserIDs.
ga(‘set’, ‘&uid’, {{USER_ID}}); // Set the user ID using signed-in user_id.
What is the difference between set and create? Do they both achieve the same result?
Both of them should work. Including the user id in the 'create' call includes setting it as well.
ga('create', 'UA-XXXX-Y', {'userId': 'USER_ID'});
ga('send', 'pageview');
is equivalent to
ga('create', 'UA-XXXX-Y');
ga('set', '&uid', {{USER_ID}}); // Set the user ID using signed-in user_id.
ga('send', 'pageview');
If you do copy and paste provided in GA, then you will need to change the smart quotes to straight quotes, because smart quotes will break GA.

Cross domain tracking (universal analytic) is not working

We tried to implement auto link cross domain but it doesn't seems to work. Here is the case:
We have 2 different domains:
source.com
destination.com
When user visits source.com and click on Submit button, he is automatically redirected to the destination.com (the redirection is on the server side and not the client).
We want to have the ability to track the user action on both domains and treat it as the same user.
We have the following code on the source.com domain:
ga('create', 'UA-XXXXXX-X', 'auto', {'allowLinker': true});
ga('create', 'UA-XXXXXX-X', {'name': 'newTracker'});
ga('newTracker.send', 'pageview');
ga('send', 'pageview');
ga('require', 'linker');
ga('linker:autoLink', ['destination.com'], true, true);
And this code on the destination.com domain:
ga('create', 'UA-XXXXXX-X', 'auto', {
'allowLinker': true
});
ga('send', 'pageview');
This code is not working. we tried to use the same UA id for both but the client id seems to change between the domains and therefore I move from source.com to destination.com I see in analytics 2 different users.
We also tried to pass ourselves the _ga parameter but it also doesn't seems to work
Can anyone please advice? We can't understand what we are doing wrong.
It can be great if you can show an example code and not just reference (:

Google Analytics with two property ID's: double counting page views?

I've always used one Javascript GA tracking script on my page, for www.example.com version of my website. It used the property ID UA-XXXXXX-1. However, I read somewhere that pages viewed on your example.com domain (without www.) won't get tracked properly this way. So I added a second property, without the www. It had the property ID UA-XXXXXX-14.
Now, I'm seeing a significant rise in my page views (more than double, actually) for the stats of my UA-XXXXXX-1 property, but not in my sessions. Does this mean that Google Analytics is tracking page views twice? And if so, how is this possible, since I'm just looking at the stats for UA-XXXXXX-1?
EDIT: This is the code I'm using:
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXX-1', 'auto');
ga('send', 'pageview');
</script>
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXX-14', 'auto');
ga('send', 'pageview');
</script>
However, I read somewhere that pages viewed on your example.com domain (without www.) won't get tracked properly this way.
This is wrong. GA in its default configuration will track properly, even if www.example.com is technically a subdomain of example.com. Plus changing the domain in your account configuration will not actually affect data collection.
Does this mean that Google Analytics is tracking page views twice?
Yes, probably. However this is not related to tracking with to properties unless there is a problem with your configuration. Most likely you are accidentaly tracking twice to the same property.
If I had to take a wild guess I'd say you are doing this:
ga('create', 'UA-XXXXXX-2', 'auto');
ga('create', 'UA-XXXXXX-1', 'auto');
ga('send', 'pageview');
ga('send', 'pageview');
when you should be doing this:
ga('create', 'UA-XXXXXX-1', 'auto');
ga('send', 'pageview');
ga('create', 'UA-XXXXXX-2', 'auto');
ga('send', 'pageview');
i.e. to send the pageview before you create your second tracker (because the second one will overwrite the first).
Alternatively you could use named trackers:
ga('create', ''UA-XXXXXX-1', 'auto', {'name': 'trackerOne'});
ga('create', ''UA-XXXXXX-2', 'auto', {'name': 'trackerTwo'});
ga('trackerOne.send', 'pageview');
ga('trackerTwo.send', 'pageview');
Since the tracker objects are called by name you can be sure that your calls always go to the correct tracker, no matter in which order the code is called in your page.
However if that is the problem you should not see data in your second account. In that case you need to share your actual tracking code.

Tracking Visits on a Roll Up Account with Google Analytics Universal

I am just looking for some help setting up a single view of all the sites we have. I can find lots of help for the old analytics tracking code but not for the new universal analytics.
I've got 5 sites each with their own tracking code in place but i'd like to create a roll up account that allows me to look at an overview of all the sites in a single view. Just wondering what i need to add to the code to do this?
It's currently;
<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-XXXX', 'auto');
ga('require', 'linkid', 'linkid.js');
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>
And I would just like to know what i need to add in to be able to include the numbers in a roll up account too? I think it's possible to simply edit this code with the UA of the roll up account but I'm not sure of the correct way to format this.
Any help would be appreciated,
Thanks
Roxi
It works rather like it did with asynchronous analytics, only you pass the name of the tracker in the (optional) configuration object:
ga('create', 'UA-XXXXX-X', {
'name': 'firstTracker',
});
ga('create', 'UA-XXXXX-Y', {
'name': 'secondTracker',
});
ga('firstTracker.send', 'pageview');
ga('secondTracker.send', 'pageview');
Any other options (e.g cookie domain, which is the third parameter if you don't use a configuration object) are set as key-value pairs in the configuration object. You find this in the documentation under Advanced Configuration.

Track Multiple Domains with analytics.js

We have a site that has the same content but available under two domains. Is this is the right code to put on each page to split the domains in google analytics so we can see view the activity under each domain?
<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-1234-1' {'cookieDomain': 'domain1.com.au'});
ga('create', 'UA-1234-2' {'cookieDomain': 'domain2.co.nz'});
ga('send', 'pageview');
</script>
Thanks
According to https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced (part Working with Multiple Tracking Objects) you should create a named tracker for the second property, like:
ga('create', 'UA-XXXX-Y', {'cookieDomain': 'domain1.com'});
ga('create', 'UA-12345-6', {'name': 'newTracker', 'cookieDomain': 'domain2.com'}); // New tracker.
and then send the page view for each tracker:
ga('send', 'pageview'); // Send page view for default tracker UA-XXXX-Y
ga('newTracker.send', 'pageview'); // Send page view for new tracker.
There's also other way how to track the same website with 2 domains by using just one property (UA-1234-1) and creating 2 views with each will include traffic only from given domain names (use predefined filter "Exclude/Include only traffic from the domains")
UPDATE
Thinking again about my answer, I must admit that even the original solution in answer
ga('create', 'UA-1234-1' {'cookieDomain': 'domain1.com.au'});
ga('create', 'UA-1234-2' {'cookieDomain': 'domain2.co.nz'});
will work, because if you try to create tracker 'UA-1234-1' on domain domain2.co.nz cookie will not be set (and vice versa creating 'UA-1234-2' on domain domain1.com.au). My solution is technically OK, but then you must take care about sending all pageviews, event trackings and other informations twice - for default tracker and also for named tracker.
Sorry if this is little confusing.

Resources