dizzysoft

web development for search engine optimization

Track Google Analytics Goal After 3rd Party Website Redirect

Marketing is useless without tracking. That is one of the reasons I like doing online marketing professionally. Unfortunately many developers- even developers working for companies selling marketing services- don’t consider this when building a website.

I don’t know how many times I’ve encountered a 3rd party quickly inserting some sort of intermediary page or redirect between one action on a website and another. When this happens, and you are using Google Analytics to track visits and conversions on your website, you lose the entire chain of tracking. So, even though your visitor came as a result of your hard work doing search engine optimization, if a visitor gets redirected through a third-party website (even for a split second) the goal will be attributed to that website instead of the actual way that visitor found you in the first place.

There are a couple ways you can overcome this limitation.

One way is by enabling cross-domain tracking. There is a way of customizing the Google Analytics code on your site (as well as the third-party website that your visitors might visit during their session) to allow unbroken tracking. You can read all about this on Google: http://support.google.com/analytics/bin/static.py?hl=en&topic=1033979&guide=1034143&page=guide.cs&answer=1034342. If this solution works for you, I’d recommend using it.

Unfortunately this does not work in every scenario. You won’t be able to use cross-domain tracking as outlined here if:

  • You cannot add the Google Analytics javascript tracking code to the intermediary page or website
  • You cannot (reasonably) add the necessary code to every link or form submission that traverses this third-pary website
  • Any number of other reasons (if you know a way that cross-domain tracking won’t work with your site, please leave it in the comments below)

I ran into one such situation on SEOrisk.com. In order to login to the website, you first have to sign-in to Twitter. In order to sign-in to Twitter you first have to visit Twitter’s login page (which is obviously not hosted on my website). Even if you are already logged-in to twitter you will still pass through at least one page that lives on Twitter before landing on my own, custom “Welcome” page.

At first this didn’t bother me. Then, as more people began to participate in the website, I realized that I had no idea how these people were finding me in the first place. Sure, I could tell how visitors found me, but I didn’t know how each new person signing-in had found me. Even though I had Google Analytics on the concluding “Welcome” page- it told me all the new signups were coming from a direct visit (which is Google’s answer if it doesn’t know the actual source of the visit).

Cross-domain tracking wouldn’t work in this circumstance- it be difficult to convince Twitter to allow me to add my Google Analytics tracking code to their website. I had to come up with another solution.

Here’s what I did (in summary):

  1. When someone first visited my website, I set a cookie with the referrer information for their visit.
  2. When they then signed-in and landed on my “Welcome” page, I over-rode Google Analytic’s referrer information with the referrer information stored in that cookie.

Here’s the code on how I made that work:

First, you need some code to set and read a cookie. I just used the code from w3schools (I know “real” developers don’t like these guys, but I find this site very useful). For this case I’ve just inserted those codes into the samples below.

Second, you need to call upon that code to set the cookie with the correct referrer information (if the cookie has not yet been set) you will need to do this on every potential landing page of the website:

<script type="text/javascript">
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

var truReferrer=getCookie(“truReferrer”);
if (truReferrer==0) {
setCookie(“truReferrer”, document.referrer, 1);
}
</script>

Here you see one of the limitations of this process. We need to set an expiration date on the cookie and we don’t know how long that should be. If we don’t set an expiration time, it will expire with the end of the session (which might get lost when we switch domains). If we set this too long, we might mis-attribute the referrer information (if they have returned in another way after the cookie was initially set). In this case I’ve just set it to one day.

Third, on the “Welcome” page you need to read the content from your cookie:

<script type="text/javascript">
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
var truReferrer=getCookie("truReferrer");
</script>

Fourth, you need to override Google Analytics referrer information with the information from your cookie. This should go right in the middle of your Google Analytics code on the page that is tracking your goal, right before you track the pageview:

_gaq.push(['_setReferrerOverride', truReferrer]);
_gaq.push(['_trackPageview']);

Done.

I’d love to hear your input on how you’ve used this code and how we can make this even better. Just leave your insights in the comments below.

Tags: , ,

3 comments about Track Google Analytics Goal After 3rd Party Website Redirect

  • Hi David, we’ve implemented remarketing for a client via GA using the recommended doubleclick cookie only to find it gets blocked on 50% of sites, resulting in visits to my client’s site halving. Any ideas on how to resolve? Might make a good blog post :-)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Examples

Recent Comments

Topics