Campaign Tracking Using _setAllowAnchor

February 4, 2009

As you know, Google Analytics allows you to define a campaign with marketing parameters at the end of a URL linking to your site.  You can set things such as the name of the campaign, the medium, the source, etc.

The _setAllowAnchor method[http://code.google.com/apis/analytics/docs/gaJSApi.html#_gat.GA_Tracker…] in GA allows you indicate these parameters in the URL with a # symbol instead of the ? symbol that is normally used.

However, the documentation for this method is a little confusing, so I’d like to go over how to use it.

The JavaScript

To enable the use of the # symbol for your GA campaigns we need to add one line into the middle of your existing GATC.  Everyone’s GATC may look a little different, but everyone should have the lines below.

var pageTracker._gat._getTracker(‘UA-xxxxxx-y’);
pageTracker._trackPageview();

Somewhere in between those two lines you need to add a line of JavaScript:

var pageTracker._gat._getTracker(‘UA-xxxxxx-y’);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();

This change should happen on every page of your website.

The Campaign URLS

After making the above change to your GATC, you can still use the standard campaign tracking parameters.  You do not need to change any of your existing campaigns.  But you also get the option to use an alternate version of campaign tracking. In the alternate version, you are just replacing the ? with a #.

This standard method for campaign tracking uses a URL that looks like this:

https://bounteous.com/index.html?utm_source=december&utm_medium=email&utm_campaign=monthlynewsletter

The alternate method for campaign tracking uses a URL that looks like this:

https://bounteous.com/index.html#utm_source=december&utm_medium=email&u…

The Example in the GA Documentation looks like this (This doesn’t work):
https://bounteous.com/index.html?utm_source=december#utm_medium=email#u…

Four Examples in GA

Here is an example of 4 different variations and what you will see in Google Analytics as a result:

1.   ?utm_campaign=1&utm_medium=1&utm_source=1
2.   ?utm_campaign=2#utm_medium=2#utm_source=2
3.   #utm_campaign=3&utm_medium=3&utm_source=3
4.   #utm_campaign=4#utm_medium=4#utm_source=4

As you can see, when the # is used in place of the &, GA doesn’t seem to recognize it as a separator.

So there you have it. I hope it is fairly straight-forward.

UPDATE

Someone pointed out that I had overlooked a possible situation. What happens if your URL already contains a query parameter such as ?page=123, what then?

**If your original URL looks like this, then the # replaces the first ampersand.**

This URL: Front Page

Will provide this result:

And remember, campaign tracking doesn’t have to be cam-painful.