views:

233

answers:

2

I'm trying to expand on an idea from someone else's question, and running into difficulty.

The idea is to insert the utm_source (from an ad campaign) into the url hash dynamically, have Google Analytics track the page, and then remove the hash. (We don't want to keep the hash because: 1. it's a duplicate page, and 2. If the user bookmarks it and comes back, it looks like another ad click)

Here's the code that almost works:

// save the old hash
var campaignSource = "Some Banner Ad";
var oldHash = document.location.hash;

// add campaign data to the hash
document.location.hash = 'utm_source=' + escape(campaignSource);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
// restore the old hash:
document.location.hash = oldHash;

The caveat is that it adds two more entries to the history (back button) on each change.

The question: How can I make the browser skip the history for hash changes?

+1  A: 

You can leave the second hash change out and just use

history.back();

to go back one step. This results in no new pages in back button but one page on forward button which will disappear when the user navigates to somewhere else.

I don't think you can prevent the hash change sticking into the history.

Tatu Ulmanen
+9  A: 

If you want to skip adding the entry to the history, then use window.location.replace instead

window.location.replace("#hash");

This way the URL on the address bar will be replaced instead of adding a new entry to the browser history object

Andris