Migrating _setDomainName from Classic to Universal Google Analytics - google-analytics

I'm trying to figure out how to correctly migrate from Google's Classic Analytics to Universal Analytics, specifically regarding the _setDomainName property of Classic Analytics.
The site I'm doing this for has several sub-domains, and the current format of the Classic tracking code is what I've been using for years on all sub-domains of this site, as well as in the main (www) URL:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'TRACKING ID']);
_gaq.push(['_setDomainName', 'sitename.com']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The Universal tracking code has a default format of:
<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', 'TRACKING ID', 'auto');
ga('send', 'pageview');
</script>
I was reading somewhere that to handle the migration of the _setDomainName property I should replace the the following line in the Universal code:
ga('create', 'TRACKING ID', 'auto');
with
ga('create', 'TRACKING ID', 'auto', {'legacyCookieDomain': 'sitename.com'});
However, I came across something else that said to replace:
ga('create', 'TRACKING ID', 'auto');
with
ga('create', 'TRACKING ID', {
cookieDomain: 'sitename.com',
legacyCookieDomain: 'sitename.com'
});
I've seen other people saying that the Universal code doesn't require any code modifications to handle this.
Does anyone know for sure what I should do here? Thanks in advance for any help!

This
ga('create', 'TRACKING ID', 'auto', {'legacyCookieDomain': 'sitename.com'});
should work best for you if your intention is to continue tracking pre-migration visitors. It uses the "auto" cookie domain so that you can track across other subdomains, like whatever.sitename.com, and the "legacyCookieDomain" parameter which supports cookies that come from ga.js or urchin.js.
[Source: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#legacyCookieDomain]
Setting the cookieDomain paremeter with a value of "sitename.com" is the same thing as setting the whole parameter to "auto".

Related

Has the Google Analytics script block changed?

I've been given some code to put into a couple of sites, as below (tracking id amended)
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-99999999-99']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
But I've just created a new GA account, and the script GA states to add is below: (again tracking id amended)
<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-99999999-99', 'auto');
ga('send', 'pageview');
</script>
Has this "standard" code changed recently or is there another reason for the difference? I'm trying to pre-empt a "why isn't this the code we sent you?" question if I use the code GA has asked me to use.
There are two versions of Google Analytics.
Classic analytics which uses ga.js your first snippet.
and
Universal analytics which uses analytics.js your second snippet.
If you have an older Google Analytics account you may be using the old snippet. All new sites are created using Universal analytics and should use the new snippet.
Technically speaking even old sits should start using the new snippet but the old snippet will still work for them. I don't think that new sites can use the old snippet tbh there would really be no point in doing so because so many features are not supported in it.
So who ever sent you that snippet is behind times and didn't realize that there is a new version of Google Analytics.

Prevent /p/__utm.gif caused by Google Analytics

I am receiving a 404 error from my Google Analytics tracking code. The following is the code I am currently using in the footer of my page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'ssl' : 'www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The 404 error appears to be caused by: /p/__utm.gif
I was using the same JS code from this question: Google Analytics and the __utm.gif file. However, that didn't work with my last report from Goolge.
Is there other way to do it?
You are using an older tracking snippet from GA. You should update to the latest version as it should solve your problem. You should also ensure that this tracking code is on all of your pages on your site. The latest code snippet looks more 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-XXXXXXX-X', 'auto');
ga('send', 'pageview');
</script>
You can get a formatted snippet for your site by:
logging into GA (Google Analytics)
Going to the admin UI by selecting the "Admin" tab at the top of the interface
If you have multiple accounts, ensure you have the correct account selected on the left (Account) column of this screen
Select the site you need the snippet for in the center (Property) column.
Select "Tracking Info" and then "Tracking Code" from the middle column.
The column should animate to the left and a new bit should appear that contains your tracking code formatted and completed for your site.
Copy and paste this into every page on your site.

Multiple accounts on Universal Analytics

I got a website builder app where users can create their own sites.
Each customer points his domain to the app's ip which has its own UA code to collect data and show page views statistics in the back office. In addition, if customer got his own Google Analytics account he may indicate it and start tracking data.
The current frontend ga.js code looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
/* app UA code */
_gaq.push(['x._setAccount', 'UA-XXXXXXX']);
_gaq.push(['x._setDomainName', 'customersdomain.com']);
_gaq.push(['x._setAllowLinker', true]);
_gaq.push(['x._trackPageview']);
/* customer's UA code */
_gaq.push(['_setAccount', 'UA-YYYYYY']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
Now I need to migrate to Universal Analytics but I am not sure what code should I use. I've done plenty of searches but I am still doubting. Would the following code work?
<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', 'customersdomain.com');
ga('create', 'UA-YYYYYYY', 'customersdomain.com');
ga('send', 'pageview');
</script>
You need a named tracker. You can set this up in the configuration object that can be passed as the third parameter instead of a cookie domain (in that case the cookieDomain setting goes into the configuration object). Plus you need two send pageview calls, one for each tracker.
<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', {
'name' : 'mycustomtracker',
'cookieDomain' : 'customersdomain.com'
});
ga('create', 'UA-YYYYYYY', 'customersdomain.com');
ga('mycustomtracker.send', 'pageview');
ga('send', 'pageview');
</script>

Name Spaced Accounts Not working Under Universal Analytics

I would like to migrate to universal analytics.js, however when I try to implement multiple accounts in the analytics.js snippet, the default account works great and shows up in Real Time, however the subsequent account never shows up on Real Time and traffic drops off to 0.
Currently I'm using ga.js to track multiple accounts in the single snippet. Here is the format I am using for that:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(
['_setAccount', 'UA-XXXXXXXX-2X'],
['_setDomainName', 'example.com'],
['_addIgnoredRef', 'example.com'],
['_trackPageview'],
['b._setAccount', 'UA-YYYYYYYY-2Y'],
['b._setDomainName', 'example.com'],
['b._addIgnoredRef', 'example.com'],
['b._trackPageview']
);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Below is the snippet I have been trying to make work with analytics.js
<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-2X', 'example.com');
ga('send', 'pageview');
ga('create', 'UA-YYYYYYYY-2Y', {'name': 'rollup'});
ga('rollup.send', 'pageview');
</script>
The "rollup" name space never registers on the rollup account in real time. The UA-XXXXXXXX-2X code registers just fine in Real Time.
I have run Google Analytics Debugger and it appears to be creating both accounts and sending just fine with no errors only info that looks appropriate:
Running command: ga(create, UA-YYYYYYYY-2Y, [object Object])
Creating new tracker: rollup
Running command: ga(rollup.send, pageview)
Could anyone offer some guidance on what might be the problem with my analytics.js snippet?
Here is the Google Universal Analytics Code that merges two Profiles and even sends demographic data.
Just replace - UA-XXXXXX-Y with UA-XXXXXX-X with your GA tracking IDs.
and replace EXAMPLE.com with the website domain and all set to go.
FYI - OldSite - you can replace it with whatever you like - its just used separate profile for the GA to send captured data. so you can use SecondProfile, IloveCodes or whatever.
This script additionally sends demographics and other data - how cool is that?
Enjoy!
And Yes thanks to all the guys out there who helped me reaching out to this answer.
<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-XXXXXX-Y', 'EXAMPLE.com');
ga('create', 'UA-XXXXX-X', {'name':'Oldsite'});
ga('send', 'displayfeatures');
ga('Oldsite.send', 'displayfeatures');
ga('send', 'pageview');
ga('Oldsite.send', 'pageview');
</script>
If you are also experiencing this, ensure that all the accounts you are attempting to track have already been upgraded to Universal Analytics via the GA Admin menu. A face palm moment for me, but perhaps this reminder will help anyone else who may be experiencing the same issue.

Google Analytics Tracking GA.js vs Analytics.js

I'm trying to use this code to track an event in Google Analytics
_trackEvent(category, action, opt_label, opt_value, opt_noninteraction)
This seems to be meant to be use with the GA.js Analytics "package", however I'm using the Analytics.js
Like this
(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');
Is there a way I can do even tracking with this code only ?
Or do I have to use
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google -analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
Do I need both of these codes?
Or is there some different way using only Analytics.js
Thanks in advance
**EDIT:
Indeed I probably didn't explain what I want, it might not even possible.
I want to work with Funnels on Events, and I want to use _trackEvent (that's what I thought it would do) to mark a user as having Entered the Funnel, if the event is send then we will have the normal funnel.
So if 10 users enter the page and 1 clicks on the button, I would have 10 events on the Funnel with 1 success
analytics.js will report to GA same as the old code, but it has different syntax, so you can't use _trackEvent.
Here is a link to the basic on-page syntax with analytics.js, and here is a link for event tracking with analytics.js
<!-- Google Analytics -->
<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-Y'); // GA account ID goes here
ga('send', {
'hitType': 'event', // Required.
'eventCategory': 'category', // Required.
'eventAction': 'action', // Required.
'eventLabel': 'opt_label',
'eventValue': opt_value,
'nonInteraction': opt_noninteraction
});
</script>
<!-- End Google Analytics -->

Resources