Upcoming LunaMetrics Seminars
Seattle, Nov 3-7 New York City, Nov 17-21 Washington DC, Dec 1-5 Los Angeles - Anaheim, Dec 8-12

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
all-traffic-sources1
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:

campaign-google-analytics

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

http://www.lunametrics.com/blog/2009/02/04/setallowanchor/

33 Responses to “Campaign Tracking using _setAllowAnchor”

DangerMouse says:

A good follow up thanks, sure this will be appreciated as you are quite right – the documentation sucks.

A says:

Just to make sure,
this should work not only for campaign tracking but to any
parametring using the ? mark.

Right?

John says:

Yes, in a more general sense, the _setAllowAnchor just says “Hey GA, # is equal to ? now so don’t just ignore it.”

A says:

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?

John says:

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.

A says:

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

John says:

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?

A says:

Helps a lot,
but not without the help of my programmer :)

Tahnks.

Mike says:

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

noid says:

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

Juvaly says:

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

John Henson says:

Juvaly,

Are you using pageTracker._setAllowAnchor(true); on the landing page?

Debbie says:

Hey John,
Any suggestions on how to preserve jump tags AND get the benefits of the # delimiter?
Thanks,
D

John Henson says:

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…&…&…&…

Dave says:

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:

Dave says:

track each link with this onclick event:

onclick=”pageTracker._link(this.href, true); return false;”

John Henson says:

Dave

That is correct. If you want to do cross-domain tracking, you can use the code that you posted above.

John

Nicolas says:

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();

John says:

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.)

Sebastian says:

Hi John,

Just an interesting thing i found out:
When using multiple custom variables, _setAllowAnchor must be placed above the _setCustomVar call, otherwise using the hash tag would not work.

hope that helps somebody… drove me nuts until i found out.

Curious to know if this can be applied to the new Asynchronous tracking code?

John says:

Richard,

You should be able to use the _setAllowAnchor method with the async syntax. I haven’t specifically tried it, but the actual methods themselves are all the same code. You just call them differently.

iamseo says:

This is how you would use the setAllowAnchor call with the new asynchronous tracking syntax:

_gaq.push(['_setAllowAnchor'], true);

iamseo says:

Actually, sorry – the above is incorrect. Please update it to:

_gaq.push(['_setAllowAnchor', true]);

Adam says:

Thanks this works great.
If I implement this will it track the ‘?’ tracking as well as # or is it only one or the other?
Want to use the # for SEO but will be a massive job changing all the media buying over from ? so if i can use both then that would be perfect. Does this mean both will be tracked?

Thanks

John says:

It should track both ? and #

AL says:

Hi John,
Thanks for the above!
IF I’m understanding one of it’s potential uses correctly … I can now track and analyse direct traffic from externally placed SEO friendly links as Google won’t index the URL past the # but will recognise the tracking parameters in GA.
Please let me know if I’ve misunderstood :-)
Cheers, AL.

John says:

I would say that is correct. My understanding is that Google does not index portions of the URL after a #.

Tom Atkinson says:

You almost neglected to include the actual reason someone might want to do all this in the first place. I’m guessing this could be done for SEO reasons but the effects must be pretty minimal. Potentially say you had a very powerful paid link that you wanted tracked you could do this to consolidate the rankings down. Surely Google can automatically do this now in it’s indexing would be my call. I will admit though that I have seen GA tagged URLs appear in the index and thought it odd. This would solve that? Seems a bit of a waste of effort: could achieve the same thing with the canonical tag on the page. I guess the # would be more elegant tho. And helps Google not to waste bandwidth.

Tom says:

I have been playing with Google Analytics to see how I can improvise a program to grab the visitors out of Google analytics and match them up with the idxbroker IDX control panel so I can tell which visitor that signed up came from where.. For example if they came from adwords, bing or just google generic… but the problem is I can see the lead but am not able to connect them with the actual signup at the back end of idxbroker program…
If anyone has any idea how to do this I would be very interested…

Rogue registry cleaners are often marketed with alarmist advertisements that falsely claim to have pre-analyzed your PC, displaying bogus warnings to take …

Morris says:

Hey John, I was hoping you might be able to answer a question I have about this:

Is set AllowAnchor True necessary to track site search when a hash is being used instead of the question mark?

Ie, the seach results page is: example.com/#s=[query]

Do you know? We’re getting some results through, but not everything.

Mr Healthy says:

Ive Just implemented this site wide, for SEO, as im hoping more than one anchors get counted as an internal link, Just in the process of purging link bloat as ive found its diluting my PR, and link juice flow after implementing a mega menue. It is kinda crazy, where by it used to be how can i get all my links spiderd in a dropdown menue, to wow i got to many internal links. After Looking at a few well placed major e commerce sites in the health industry, I looked a their menu structure and see that they are implementing sitewide anchor text in their side menue to every section of the site. If it aint hurting their SEO efforts or is in fact helping them, ( they rank bigtime) then i thought i would give it a go. Just about to update and see what effect it has.