I’m very surprised at how there appears to be just one lone example/sample showing the use of Google Analytics for a cross-domain, third party website, or e-commerce implementation. Or maybe I’m just a bad googler. My scenario involves hosting content which includes a subscribe or buy now link that takes the user to a third party e-commerce site where the actual order is processed.
Here’s what I found. I searched for “google analytics cross domain” and this came up as the second
It should be first) link:
google analytics cross domain. Ok, that’s the API, but without knowing a few tricks I couldn’t get it to work.
So then I tried, “google analytics third party”, and once again, it’s not the first result, it’s the third. But I found a decent example with google analytics third party
The only thing missing is highlighting of the important code. Here is my new and improved example for adding google analytics code to your third-party e-commerce or other external partner site. I’ve included a small wordpress modification which can also come in handy.
- Add the proper onclick code to your anchor links.
onclick="pageTracker._link(this.href); return false;"
Just for the heck of it, I’ve don’t this with my Wordpress bookmark links by modifying the functions.php in the theme directory:
function AddTrackingGA($content)
{
$link = "pageTracker._link(this.href);return false;";
$content = preg_replace("/<a ([^>]+)>/sim", "<a $1 onclick=\"" . $link. "\">", $content);
return $content;
}
add_filter('wp_list_bookmarks', 'AddTrackingGA');
Or, if you like, just add
to any anchor (<a>) tags that go to your third party (soon to be)tracking enabled site. - Put this code on your own site (or modify your current tags to add the bold code). And of course, don’t forget to change the _getTracker call to your tracking tag.
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost
+ "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script>
- Put this content on your third party, e-commerce website. Place it on all pages which you have access to on the third party site. In my case I put it on the subscription form as well as the thank you for your successful subscription page:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script>
That’s more code than I typically like to throw around in a blog post, but that will have to do.
Finally, I’m impatient and want to verify things are working. Because of Google’s rather difficult 24 hour latency, I like to verify that my Google Analytics tracking code is actually working. There are two tools that I use typically to confirm that Google Analytics is working. I like Fiddler for Windows which is a robust http and web traffic proxy sniffer that gives me every detail about my browser’s requests and the server’s responses. Or, the les robust, but still effective LiveHTTPHeaders plugin for Mozilla Firefox can give you a similar confirmation. That confirmation is a request for a little gif hosted by google-analytics.com that looks like this:
http://www.google-analytics.com/__utm.gif?utmwv= … … …
If you see that request in Fiddler or LiveHTTPHEaders along with your website requests and third party requests, then you have successfully installed Google Analytics in a third party, multiple domain scenario.
Tags: Google Analytics