1

We work with an affiliate program where you need to use different affiliate links for some specific countries. Because of that our links in the content are pointed to an internal page http://www.oursite.com/affiliateprogram where we do a javascript redirect based on geo IP (we use maxmind javascript service to detecct the country code: http://j.maxmind.com/app/country.js ). We also have a 5 second meta refresh to the main affiliate link in case the country code detection fails.

This is the meta tag:

<meta http-equiv="refresh" content="5;url=http://www.externallink.com?affiliate=252346">       

and this is part of the javascript redirection:

if (country=='IT') {
    window.location.replace('http://www.externallink.it/?affiliate=252342');
}
if (country=='ES') {
    window.location.replace('http://www.externallink.es/?affiliate=252343');
}
if (country=='FR') {
    window.location.replace('http://www.externallink.fr/?affiliate=252344');
}
else {
    window.location.replace('http://www.externallink.com/?affiliate=252346');
}   

The question is; how can we track the visits of each outbound link using Google Analytics?

2 Answers 2

1

Google Analytics lets you manually insert a hit to a URL of your choice with this syntax: (It's intended for tracking links within AJAX applications like GMail)

_gaq.push(['_trackPageview', URL]);

My advice would be to do something like this:

function redirect(url) {
    _gaq.push(['_trackPageview', '/outbound/'+url]);
    window.location.replace(url);
}

if (country=='IT') {
    redirect('http://www.externallink.it/?affiliate=252342');
}
if (country=='ES') {
    redirect('http://www.externallink.es/?affiliate=252343');
}
if (country=='FR') {
    redirect('http://www.externallink.fr/?affiliate=252344');
}
else {
    redirect('http://www.externallink.com/?affiliate=252346');
}

It's actually how outbound link tracking is done in several Analytics integration plugins I've run across.

2
  • -1. Urchin tracker is deprecated, and should not be used.
    – Yahel
    Commented Oct 4, 2011 at 1:45
  • Sorry. I forgot about that. I'm in the middle of gradually migrating my sites off Google Analytics and Google Libraries API because it doesn't feel right to be letting Google track my users when I put so much effort into limiting where they can track me. As a result, I didn't bother upgrading to the new API.
    – ssokolow
    Commented Oct 4, 2011 at 20:09
0

First, make sure to add the Google Analytics tracking code (you can find it in your account).

Then, use the _trackPageview or _trackEvent function to track the redirect.

function trackRedirect(id, target) {
  _gaq.push(['_trackPageview', '/out/' + id]);
  window.location.replace(target);
}

if (country == 'IT') {
  trackRedirect('252342', 'http://www.externallink.it/?affiliate=252342');
} else if (country == 'ES') {
  trackRedirect('252343', 'http://www.externallink.es/?affiliate=252343');
} else if (country == 'FR') {
  trackRedirect('252344', 'http://www.externallink.fr/?affiliate=252344');
} else {
  trackRedirect('252346', 'http://www.externallink.com/?affiliate=252346');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.