I have two different GA profiles that I want to report to all the time. This HTML that will have the tracking code will also be run from different top-level domains.
Reading these links: https://developers.google.com/analytics/devguides/collection/gajs/#MultipleCommands https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains
I created this sample code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var parts = location.hostname.split('.');
var subdomain = parts.shift();
var topleveldomain = parts.join('.');
var _gaq = _gaq || [];
_gaq.push(
['_setAccount', 'UA-12345-5'],
['_setDomainName', topleveldomain],
['_setAllowLinker', true]
['_trackPageview'],
['b._setAccount', 'UA-12345-2'],
['b._setDomainName', topleveldomain],
['b._setAllowLinker', true]
['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>
</head>
<body>
<a href="#" onClick="_gaq.push(['_trackPageview', '/testing-ga/']);">Play</a>
</body>
I used JavaScript to determine the top-level domain of the site hosting the tracking code, so it sets the command of _setDomainName to the value of the current domain.
I also added a link with a onclick event to send _trackPageview to GA for testing purposes.
The problem I'm having is that when the page loads, it doesn't sends the request to GA (should send page views request to a different profile each). This is not happening.
It just sends the request to one profile (the first in the array) when I do the onclick event, the normal page view is not firing up.
However, it does sends the page view requests if I remove the: ['_setAllowLinker', true] from both items of the array. But I need that if I'm going to be hosting the tracking code under different top-level domains, right?
Any ideas?
Thanks!
You're missing a couple of commas in your _gaq.push()
parameters:
_gaq.push(
['_setAccount', 'UA-12345-5'],
['_setDomainName', topleveldomain],
['_setAllowLinker', true], <===
['_trackPageview'],
['b._setAccount', 'UA-12345-2'],
['b._setDomainName', topleveldomain],
['b._setAllowLinker', true], <===
['b._trackPageview']
);