views:

3636

answers:

5

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...)

A: 

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.

Chacha102
Yeah, I saw that, but it looks like the suggested solution is to either download the GA script and hack it, or hack its internal variables from outside - both of which sound dangerous and unstable...
Psionides
+6  A: 

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.

Török Gábor
I'm not so sure. If both of the trackers call _setVar to set a custom tracking variable, it seems that only the variable from the second tracker can be found in the cookies, and the one from the first tracker is getting lost. Unless I'm doing something wrong...
Psionides
@Psionides: and it seems that's true--so far I always used the same settings for different trackers. I updated my answer.
Török Gábor
+2  A: 

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).

Richard Nienaber
Yes, but it doesn't work across domains - I can't set a cookie for a different domain that is displayed in the location bar. So if my widget is embedded at e.g. http://website.com, and the script is loaded from http://widget.com, then I can only set cookies for http://website.com (and so does the embedding website itself) - and we still have a conflict.
Psionides
Added more info for cross domain linking.
Richard Nienaber
A: 
var otherTracker = _gat._getTracker(”UA-22222-1″);
otherTracker._setDomainName(’domain.com’);
otherTracker._trackPageview();
code
+6  A: 

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

http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html#MultipleTrackers

Paul Thompson
Thanks for the info!
Psionides