Reputation: 508
I am using Google Analytics and I am trying to set cross domain tracking for my website. I've read Google's cross domain tracking guide, but I am confused as to how to implement it properly.
The issue I am having is that the example code they give looks nothing like the tracking code I was given through my Google Analytics admin console.
My tracking code looks 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', 'MyTrackingID', 'MyDomain');
ga('send', 'pageview');
</script>
(My actual tracking ID and my domain have been censored out with MyTrackingID
and MyDomain
, respectively.)
However, the example tracking code given in the guide looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_setAllowLinker', true]);
_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>
How do I add the _gaq.push(['_setDomainName', 'A.com']);
"option" to my tracking code as instructed?
Upvotes: 3
Views: 4237
Reputation: 86
Script to add to the snippet provided by Google
On your domain you need to add
ga('require', 'linker'); // Load the plugin.
// Define which domains to autoLink.
ga('linker:autoLink', ['3-party.com', '3-party-domain.com']); //add as many as you need third party sites
on third party domain update the existing create
function to
ga('create', 'UA-XXXXXX-X', {
'allowLinker': true
});
Note by Google: While this feature is designed to work automatically for most websites, some pages may be scripted in ways that prevent Auto Linking from functioning properly.
Upvotes: 1
Reputation: 449
This code works for me. It is for Universal Analytics, not the older Google Analytics. Let's say you have two domains: source.com and destination.com and you want to track both domains:
On source.com:
<!-- Universal Analytics -->
<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-XXXXXXXXX-X', 'source.com', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['destination.com']);
ga('send', 'pageview');
</script>
On destination.com:
<!-- Universal Analytics -->
<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-XXXXXXXX-X', 'source.com',{'allowLinker': true});
ga('send', 'pageview');
</script>
Upvotes: 3
Reputation: 250
Correct implementation is following which is working perfect for myself -
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_setAllowLinker', true]);
_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>
For setting domain, you can add -
_gaq.push(['_setDomainName', 'A.com']);
below of
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
Upvotes: -2