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
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
var otherTracker = _gat._getTracker(”UA-22222-1″);
otherTracker._setDomainName(’domain.com’);
otherTracker._trackPageview();
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.
Now made easy with the new asynchronous tracking code. :)
https://developers.google.com/analytics/devguides/collection/gajs/#MultipleCommands
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>
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.