Cross Domain Google Analytics - javascript

I'm aware there is a fair amount out there about this sort of thing, but I can't get mine working. I'm only worried about cross domain and not about sub domain.
I have 2 sites, www.siteA.com and www.siteB.org for example.
SiteA has a link to my registration page on SiteB.
On SiteA I have the following script (On my layoutpage/master):
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '##AnalyticsIDA##']);
_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>
On SiteB I have the following script (On my layoutpage/master):
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '##AnalyticsIDA##']);
_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>
I have a link on SiteA
testing
This takes me to www.siteB.org with all the correct parameters (same as my SiteA cookie values), but SiteB's cookies have different values to SiteA.
EDIT
Futhermore, I've also tried using _gaq.push(['_setDomainName', 'siteA.com']); and _gaq.push(['_setDomainName', 'siteB.com']); but I still get new cookies on siteB

Have you tried using _setDomainName in your GA code? From the Cross Domain docs:
(_setDomainName) Use this when you want to treat top- and sub-domains as one entity and track in the same profile. Also use this when you want to track across multiple top-level domains AND their sub-domains. In this case, you will need to using linking between the top-level domains, but not between the top-level domains and their sub-domains.
In your case, on SiteA
_gaq.push(['_setAccount', '##AnalyticsIDA##']);
_gaq.push(['_setDomainName', 'siteA.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
and on SiteB
_gaq.push(['_setAccount', '##AnalyticsIDA##']);
_gaq.push(['_setDomainName', 'siteB.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);

Try changing the onClick to:
onclick="_gaq.push(['_link', '.siteB.org']); return false;"

Related

Multiple domain tracking with an iframe - landing page showing up as iframe source in GA

So we have website A www.domaina.com and within this website there is an iframe that is shown on all pages, which has source from website B www.domainb.com.
I have setup cross domain tracking using the following code, and if test in GA real time going between the 2 sites, it is correctly showing there is only 1 visitor always. But the landing page (and active page if viewing in Real time) is showing up as the iframe source. I can see this is because using GA debugger, there are multiple tracking beacons being sent out as soon as you land on the home page, and the last one is from the iframe, showing the referring URL being www.domaina.com and the page being domainb.com. THis hence is causing self referrals to show up..
here is the code, but i have no idea how to fix! Im thinking maybe should all the _setDomainName be set to the domain's? I have read conflicting advice on this.
many thanks
GA code domain A
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7338871-1']);
_gaq.push(['_setDomainName', 'none']);
_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>
GA code domain B
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7338871-1']);
_gaq.push(['_setDomainName', 'none']);
_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>
iframe script domain A
<script type="text/javascript">
var iframeLink = "domainb.com";
_gaq.push(function() {
var pageTracker = _gat._getTrackerByName();
iframeLink = pageTracker._getLinkerUrl(iframeLink);
document.getElementById("myIFrame").src = iframeLink;
});
</script>

How to have a single google analytics account for multiple domains

I have a Google analytic account which I'm using to track analytic of the domain a.com. Now I want to add b.com statistics to the same account. Given that they don't allow free Google analytic account creation anymore.
What I have done is according to this article and this. Both are the same.
Code are below here.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-35089154-8']);
_gaq.push(['_setDomainName', 'www.a.com']);
_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>
In the b.com domain I have below code
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-35089154-8']);
_gaq.push(['_setDomainName', 'www.b.com']);
_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>
But statics of b.com is not show in Google analytic account? How can I fix this?
Create a new property in the account (which will give you a new Account ID) and use that for domain b.
What is described in the article is cross domain tracking - that is needed only when you want to track two different domains so that they appear as one domain in your account (so you can for example track movements between the domains without losing campaign information).
#Eike's answer is correct. Below are the steps how it should be done.
https://support.google.com/analytics/answer/1042508?hl=en

Two google analytics accounts per page [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
google analytics - multiple trackers on one page (cookie conflict)
I've searched around the internet about this question, but cant find the answer I am looking for.
I've developed a blog, with some special features. Each user get their own subdomain when they register.
Everything is free, but with ads. And I currently use google analytics to track the total number of visitors and pageviews.
I've developed a small counter so that each user can se how many visitors and pageviews they have, but for more detailed informatino they need to use their own "google analytics", or what ever they want to use.
So. The code I use now:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx1-1']);
_gaq.push(['_setDomainName', 'sub.domain.com']);
_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>
I dont know what type of analytics service each user will use, I let them insert their own code within the HEAD.
So, IF they use google analytics, will our two accounts at google analytics get the correct number of visitors? The code will then be:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx1-1']);
_gaq.push(['_setDomainName', 'sub.domain.com']);
_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>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx2-1']);
_gaq.push(['_setDomainName', 'sub.domain.com']);
_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>
Or do I have to do it like:
_gaq.push(
['_setAccount', 'UA-XXXXXXXX-1'],
['_trackPageview'],
['b._setAccount', 'UA-XXXXXXXX-2'],
['b._trackPageview']
);
Where both trackers are within the same . ??
Thanks!
This is an example and it works. I have the same code on mine.
_gaq.push(
['_setAccount', 'UA-XXXXX-1'],
['_trackPageview'],
['b._setAccount', 'UA-XXXXX-2'],
['b._trackPageview']
);

How to make a Google Analytics code added with script on other domain work?

I have a domain, lets call it myhost.com and i have a script addGAcode.js on myhost.com. The code contains function includeGA() that calls the standard GA code for a account set for my client domain. It looks like this:
function includeGA() {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-432432432-43']);
_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); })();
}
includeGA();
Lets call this domain groceryinbelfast.com.
Now i add to meta of groceryofbelfast script tag to include the myhost.com/addGAcode.js. Unfortunately GA does not work. The GA code is loaded properly but i suppose because it's located on other domain that the domain groceryofbelfast it cannot set cookies on groceryofbelfast domain that are necessary for statistics to work.
The point of my concept is to have GA script on my server so that when Google will change something in it or i will want to make some adjustments to it i dont need to call every time the webmaster of my client to make appropriate changes.
Any ideas?
Your includeGA() function probably won't work because _gaq needs to be declared as a global variable, not a local variable inside your includeGA() function.
You can fix that particular issue by changing to this:
function includeGA() {
window._gaq = window._gaq || [];
_gaq.push(['_setAccount', 'UA-432432432-43']);
_gaq.push(['_trackPageview']);
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);
}

How to use Google Analytics setAllowAnchor flag

I'm trying to get the setAllowAnchor flag in Google Analytics to record campaign information but it doesn't seem to be working.
The site I'm trying to use it on uses hashbang-style URLs BTW - that's the http://hostname/#!/relative-url style of navigation because there needs to be a persistant audio player across page views.
I've tried both of the below, but am not seeing any setAllowAnchor campaign in GA.
/#!/&utm_campaign=setAllowAnchor&utm_medium=hash&utm_source=test
/#utm_campaign=setAllowAnchor&utm_medium=hash&utm_source=test
The function setAllowAnchor is not deprecated. What am I doing wrong?
My Google Analytics code looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1060190-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);
_gaq.push(['_setAllowAnchor', true]);
(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>
Any Google Analytics configuration must come before the _trackPageview or any other _track* functions. So if you rewrite your code to issue the _setAllowAnchor before the _trackPageview it should work.
Also notice that _trackPageLoadTime is not needed anymore. It's issued by default and so ths function is deprecated.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1060190-1']);
_gaq.push(['_setAllowAnchor', 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>

Categories

Resources