If you have a website and it runs well, your next priority should be to make sure your analytics setup is sound. This was a challenge I just had to conquer myself and sometimes it isn’t as easy as it seems like it should be. Here’s my tale…
Setting up Google Analytics in Magento Enterprise
Configuring Google Analytics in Magento couldn’t be easier. There’s a config page, you put in your profile number, activate it and then you’re done. Except in my case we weren’t.
We have a bit of a unique setup. We have a sub-domain that we need to track as well and for us, there’s no need to have the data separated into a separate profile because the sub-domain is only used for the cart. We have the main http://www. sub-domain as well as the https://secure. sub-domain.
What is a self referral?
It’s pretty simple. When your visitor traffic is properly tracking source by source but you also have traffic and conversions tracking under the same domain name as what you’re trying to track. Because the only different sub-domain we have is in our cart, this meant that only our conversion data was wrong. Fortunately (or so I thought), Google provides special code to use when you need to track sub-domains.
Google’s Recommended Code for Sub-Domains
This is the code that Google recommends you use when tracking a site that has one or more sub-domains.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-7']);
_gaq.push(['_setDomainName', '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>
This is the newest asynchronous Google Analytics tracking code. The only part that’s different from the standard install is _gaq.push(['_setDomainName', 'domain.com']);. However, in Magento, there’s no way to define that variable without modifying core PHP files. But that’s exactly what we had to do to get that setup properly.
After this was installed and we verified that it was rendering properly on both sub-domains, we waited a day and reviewed our Google Analytics account. Still, this didn’t fix our problem. We were still tracking self referrals and there was no change at all.
At this point, I went back to the drawing board which pretty much means that I went back to Google searches.
I found a few other forum threads talking about another snippet that could be added that will help fix these issues. That’s when I decided that Google’s recommendation for code was going out the window.
The Fix
This is what worked for me. If you’re facing similar problems with self referrals, I hope it works for you too. However, the only real way to get things working is trial and error. Trial and error never fails forever.
The code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-7']);
_gaq.push(['_setDomainName', 'domain.com']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addIgnoredRef', 'domain.com']);
(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>
The addition is _gaq.push(['_addIgnoredRef', 'domain.com']);.
Honestly, that’s all it took. It all seems simple now but at the time it was incredibly frustrating. As soon as the code went live and I inspected the GET requests for __utm.gif I knew that it was working properly and the problem had been solved.
Verifying Referral Data in Firebug/Chrome
In all reality, if you know what to look for you don’t have to wait a day to check Google Analytics to see if your data was tracked properly. All you really have to do is inspect the GET requests that the Google Analytics Javascript makes at page load. Here’s a screenshot of what you need to watch out for (screenshot of this blog)…

Click on the GET request for __utm.gif and then you’ll get the detail for that request. Pay special attention to the section under “Query String Parameters”, specifically the parameter called utmcc. That parameter is what recalls the tracking data stored in cookies. Here’s the value from that parameter:
_utma=189990958.53866207.1339112813.1345157959.1345242492.53;+__utmz=189990958.1345242492.53.25.utmcsr=assets.tumblr.com|utmccn=(referral)|utmcmd=referral|utmcct=/iframe.html;
Within that value, check out the parameter utmcsr, which is equal to the referral source. When you’re evaluating this on your own domain it’s important to look at this on the first page you visit when transitioning from one sub-domain to the other. That’s where the cookie information will get all screwed up. If the referrer is equal to your domain, then you still have a problem. If it is equal to the original referrer like direct, organic, etc, then you’ve solved the problem.
If I’m being honest I still do not understand why the Google Analytics code that’s recommended by Google in my account wasn’t enough. It’s really frustrating. If anyone has additional insight I’d love to understand.
I hope this has been informative. Please leave a comment if you have a question and I’ll do my best to answer or provide ideas.