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 [] 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’);

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

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

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:

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

The Example in the GA Documentation looks like this (This doesn’t work):

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.


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.

John is a former LunaMetrician and contributor to our blog.

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

  • A

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


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

  • A

    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 =
    Original =

    The url from the newsletter cant get indexed and there for won’t
    be treated as a duplicated page…right?

  • 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

    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.

    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?


  • You can give them

    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:
    call something like:
    (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

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


  • Mike

    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?


  • 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

  • Juvaly

    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:
    Replacing it with this: produces a direct referral in the _utmz cookie. Any solution for this case?



  • Juvaly,

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

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

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

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


    2) pageTracker._setAllowHash(false);

    so our code could look like this:

    var pageTracker = _gat._getTracker(“UA-XXXXXX-1”);

    and we would tag each link like this:

  • track each link with this onclick event:

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

  • Dave

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


  • 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”);
    var pageTracker = _gat._getTracker(“UA-XXXXX”);

  • John

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

    var otherTracker = _gat._getTracker(”UA-XXXXX”);

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

  • 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


    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.

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

    _gaq.push([‘_setAllowAnchor’], true);

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

    _gaq.push([‘_setAllowAnchor’, true]);

  • Adam

    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?


  • John

    It should track both ? and #

  • AL

    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

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

  • 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

    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 …

  • 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:[query]

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

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

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187