Kuba Suder
Kuba Suder

Reputation: 7827

google analytics - multiple trackers on one page (cookie conflict)

I'm writing a web application that's supposed to be embedded in other people's websites (kind of a widget). I'm using Google Analytics to track all the people that visit all instances of my script on the embedding websites. The problem is that I don't know how to use it so that it doesn't interfere with those websites' own Google Analytics accounts. I'm storing the tracker variable in a namespace, so I thought that should do it, but I haven't realized that GA stores its settings in cookies (__utma, __utmz etc.), and those cookies are used by both trackers, if there are two of them on the same page... So for example if I use _setVar to store some kind of user-defined variable in Google Analytics, and the embedding site does the same, we overwrite each other's values...

Of course it would be easiest if Google provided a way to change the name of the cookies to a custom one, but I can't find any way to do it. I thought about using cookie domain or path to force a separate cookie, but this doesn't work, because if I set domain or path to something other than the real domain/path, then the cookie is not readable for the page after reload...

Does anyone know a way to have two trackers on one page and make them use separate cookies so that they don't overwrite each other's settings?

Or, if that's completely impossible - is there any other analytics service with similar functionality as GA in which this is possible? (it would have to have advanced features like event and campaign tracking...)

Upvotes: 55

Views: 60507

Answers (9)

Paul Thompson
Paul Thompson

Reputation: 821

Now made easy with the new asynchronous tracking code. :)

https://developers.google.com/analytics/devguides/collection/gajs/#MultipleCommands

Upvotes: 56

MyWebsiteSpot
MyWebsiteSpot

Reputation: 213

You can install multiple instances of the Google Analytics tracking code on your web pages to send data to multiple properties in your account. https://support.google.com/analytics/answer/1032400?hl=en

Or you can get creative and do the following per Google's instructions. https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#multipletrackers

<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', 'auto');
  ga('create', 'UA-XXXX-Y', 'auto', {'name': 'newTracker'});
  ga('send', 'pageview');
  ga('newTracker.send', 'pageview');

</script>

Upvotes: 18

Miles Lukas
Miles Lukas

Reputation: 81

In case anyone still has this issue and wants an easy paste, my issue was using my own google tracking for my code that was being added to other people's pages that might also be using google tracking. I have tested this and confirm it works as expected:

var _gaq = _gaq || [];

_gaq.push(['some_unique_name._setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['some_unique_name._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);
})();

I am using also using events

_gaq.push(['some_unique_name._trackEvent', 'Event Category', 'Event Action', 'Event Label']);

If anyone sees an issue with it, please let me know.

Upvotes: 8

Vince
Vince

Reputation: 1451

It looks like Google recommends against this practice:

Installing multiple instances of the Google Analytics Tracking code on a single web page is not a supported implementation. We suggest that you remove all but one instance, and make sure that you have the code from the correct profile installed on every page that you would like to track.

https://support.google.com/analytics/bin/answer.py?hl=en-GB&answer=1032400

Upvotes: 3

Google Analytics
Google Analytics

Reputation: 11

I have used this structure on our site and clients sites and it works like a charm...

<script type="text/javascript"> 
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");    
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js'     type='text/javascript'%3E%3C/script%3E")); 
</script> <script type="text/javascript"> 

try {

//Original tracking
var pageTracker_ORIG = _gat._getTracker("UA-XXXXXXX-1");
pageTracker_ORIG._setDomainName('.sleepinggiantmedia.co.uk');
pageTracker_ORIG._trackPageview();

//New Analytics tag
var pageTracker_SGM = _gat._getTracker("UA-XXXXXXX-1");
pageTracker_SGM._setDomainName('.sleepinggiantmedia.co.uk');
pageTracker_SGM._trackPageview();


} catch(err) {}

Upvotes: 1

code
code

Reputation: 1

var otherTracker = _gat._getTracker(”UA-22222-1″);
otherTracker._setDomainName(’domain.com’);
otherTracker._trackPageview();

Upvotes: -2

Richard Nienaber
Richard Nienaber

Reputation: 10564

According to the documentation listed by Török, it seems the correct answer is to use _setCookiePath. This causes each tracker to use completely different cookies.

Example code from website:

<script type=”text/javascript”>
    var pageTracker = _gat._getTracker(”UA-11111-1″);

    pageTracker._setDomainName(’domain.com’);

    pageTracker._setCookiePath(’/subdirectory/’);
    pageTracker._trackPageview();

    var otherTracker = _gat._getTracker(”UA-22222-1″);
    otherTracker._setDomainName(’domain.com’);
    otherTracker._trackPageview();
</script>

When you link from one domain to another, every link that posts to the other domain has to look like this:

<a href="pageTracker._linkByPost('otherdomain.com/petStoreCart/legalTerms.php');"

This will add Google Analytics specific query string values that will be used by the above script to set the cookie (source).

Upvotes: 3

viam0Zah
viam0Zah

Reputation: 26312

Don't have to use different cookie names as Google Analytics happily works with multiple trackers on the same page. See answers for question Google Analytics - Multiple Trackers for Several Accounts?.

Update

It turns out that using multiple trackers is a working method but has some pitfalls. One of those, that is, you cannot apply different user segmentation for each of them. John Henson demonstrates a workaround that coerces GA to use different cookies, may be you should check it.

Upvotes: 10

Tyler Carter
Tyler Carter

Reputation: 61557

This person is having the same problem on the Google Analytics help fourm. I'd suggest taking a look at the thread. But regularly GA doesn't support multiple trackers.

I like Clicky myself, but it costs money.

Upvotes: -3

Related Questions