Campaign Tracking using _setAllowAnchor
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_._setAllowAnchor] 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:
http://www.lunametrics.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:
http://www.lunametrics.com/index.html#utm_source=december&utm_medium=email& utm_campaign=monthlynewsletter
The Example in the GA Documentation looks like this (This doesn’t work):
http://www.lunametrics.com/index.html?utm_source=december#utm_medium=email#utm_campaign=monthlynewsletter
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:
http://lunametrics.com/?page=1234#utm_source=henson&utm_medium=john&utm_campaign=test
Will provide this result:

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


February 4th, 2009 at 6:04 pm
A good follow up thanks, sure this will be appreciated as you are quite right – the documentation sucks.
February 5th, 2009 at 10:23 am
Just to make sure,
this should work not only for campaign tracking but to any
parametring using the ? mark.
Right?
February 5th, 2009 at 10:29 am
Yes, in a more general sense, the _setAllowAnchor just says “Hey GA, # is equal to ? now so don’t just ignore it.”
February 5th, 2009 at 8:40 pm
Hi,
i’m confused about an issue.
Isn’t the only way for a url (with a parameter query) to be considered
as duplicated page is when it is placed on an HTML page?
I mean, lets say im tagging a url in a newsletter:
Tagged = example.com/campaign?newsletter
Original = example.com.campaign
The url from the newsletter cant get indexed and there for won’t
be treated as a duplicated page…right?
February 6th, 2009 at 9:26 am
That is correct. A search engine will not index your email.
However, keep in mind that if someone copies a link from your newsletter and posts it on a webpage, then it would (probably) get indexed. (I suspect that this would be a fairly uncommon thing for people to do, but I don’t really have any data on it.)
But mostly, I just used the newsletter example out of habit. It is a common example of a type of marketing campaign that many people are already familiar with.
February 6th, 2009 at 1:17 pm
Thanks John.
I thought that this would solve my main problem with duplicated content.
For analysis needs, i want to differentiate the source of the URL.
e.g I hand out the *same* root URL to few of my promoters with
an addition of their name so i can see who sent me more traffic.
example.com/campaign?Dani
example.com/campaign?Anna
example.com/campaign?Mike
BUT i want to see it on my top content report.
i mean i want to see where did the traffic from each promoter went next,
or how many pages each URL had….and that i can’t do with campaign reports..
So as long as everything after the # isn’t captured by content report..
it’s quite impossible…..
Am i missing something that can be done?
A
February 6th, 2009 at 1:42 pm
You can give them
example.com/campaign#ref=dani
Then in your GA JavaScript on every page of your site, add some javascript to check the URL. If it has #ref=xyz then instead of calling:
pageTracker._trackPageview();
call something like:
pageTracker._trackPageview(window.location.href+window.location.search+’&'+window.location.hash);
(Warning: My syntax may not be correct)
This way, everything will work the same, except when someone comes in with a URL with #ref= at the end, if the JavaScript sees that, then it will execute the alternate version, and compile the URL that it reports to GA itself and include the info after the hash.
Does that help?
February 6th, 2009 at 2:27 pm
Helps a lot,
but not without the help of my programmer
Tahnks.
February 9th, 2009 at 6:47 pm
I like the post. Very informative.
Does this only apply to the newest version of the tracking code?
Is there a similar fix for the Urchin.js version?
Thanks
February 19th, 2009 at 6:20 pm
the _setAllowAnchor method was initially designed to solve duplicate content worries in search engines. From now, we can also use the canonical url method instead of the _setAllowAnchor method
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
March 13th, 2009 at 9:20 am
Thanks, John.
You pointed out that if my url already contains a search part I can use both the search and hash together to form a new link, but what if my url contains a hash part already?
Oriignal url looks like this: http://www.domain.com/product#aff=123
Replacing it with this: http://www.domain.com/product#aff=123&utm_source=src&utm_medium=med produces a direct referral in the _utmz cookie. Any solution for this case?
Cheers,
Juvaly
March 16th, 2009 at 10:54 am
Juvaly,
Are you using pageTracker._setAllowAnchor(true); on the landing page?
March 20th, 2009 at 9:20 pm
Hey John,
Any suggestions on how to preserve jump tags AND get the benefits of the # delimiter?
Thanks,
D
April 1st, 2009 at 9:46 am
Debbie
I don’t think you can do both, unfortunately.
If you want to use the # as a jump to a particular portion of the page, then it seems you need to use /page.html?…&…&…#Jump
Using /page.html#…&…&…#Jump does not seem to work, nor does /page.html#Jump…&…&…&…
May 1st, 2009 at 2:35 pm
Another excellent post, John. Thanks for all your analytics help. Quick point of clarification:
If we want to use the (#) tag instead of the (?), and we also want to preserve tracking across multiple domains, would we need to use both:
1) pageTracker._setAllowAnchor(true);
and
2) pageTracker._setAllowHash(false);
so our code could look like this:
var pageTracker = _gat._getTracker(“UA-XXXXXX-1″);
pageTracker._setAllowHash(false);
pageTracker._setDomainName(“none”);
pageTracker._setAllowAnchor(true);
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
and we would tag each link like this:
May 1st, 2009 at 4:40 pm
track each link with this onclick event:
onclick=”pageTracker._link(this.href, true); return false;”
May 5th, 2009 at 8:40 am
Dave
That is correct. If you want to do cross-domain tracking, you can use the code that you posted above.
John
June 12th, 2009 at 3:21 pm
It is possible that using the pageTracker._setAllowAnchor(true); with a code that has two profile this affect the information of the report making the visit to fell down.
I attach and example:
var pageTracker = _gat._getTracker(“UA-XXXXX”);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
var pageTracker = _gat._getTracker(“UA-XXXXX”);
pageTracker._setLocalRemoteServerMode();
pageTracker._trackPageview();
June 15th, 2009 at 8:32 am
I do not think that the _setAllowAnchor(true), specifically, would cause that. Are you using campaign parameters internal to your own site? If so you might make sure that _setAllowAnchor(true) was set for both pieces of code.
Are you using pageTracker._setVar in your code anywhere, or any events or ecommerce tracking?
I might try the following:
var pageTracker = _gat._getTracker(”UA-XXXXX”);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
var otherTracker = _gat._getTracker(”UA-XXXXX”);
otherTracker._setAllowAnchor(true);
otherTracker._setLocalRemoteServerMode();
otherTracker._trackPageview();
And if you call pageTracker._setVar(. . .); anywhere, you can also call otherTracker._setVar(. . .);
Likewise if you’re calling events or ecommerce code, you can do it with both pageTracker and with otherTracker.
(The name “otherTracker” is entirely arbitrary.)