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.

Share and Enjoy:
  • Print
  • email
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • Twitter

Related Posts

  1. GA Campaign Tracking Parameters: What's Really Required?
  2. Using Different Cookies when Tracking to Multiple Accounts in GA
  3. Pitfalls of tracking to multiple accounts in GA
  4. _initData() — Always there, or always not.
  5. Hashing it Out: Referral Tracking

24 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]);

Leave a Reply

Feedback Form