Basj
Basj

Reputation: 46275

Make tracking codes disappear from URL bar

I'm using tracking codes to know where do the visitors of my website come from.

Example:

But then this is what gets displayed in the browser:

enter image description here

This is not very user-friendly: the users shouldn't see this information. How to get this parameter hidden on browser? (but still present in the server logs, that's where I'll do my analysis!)

Shoud I do it from JS or PHP, or even in an .htaccess RewriteRule?

PS: instead of using parameters ?param=1, I could also use a different approach and have a RewriteRule redirect example.com/mailingjuly2017/ to example.com.

Upvotes: 0

Views: 181

Answers (1)

chrisuae
chrisuae

Reputation: 1112

If you just want to remove the appearance of the parameters in the address bar without the need to refresh the page, you can try the following JS:

if (history.pushState && window.location.href.includes('?')) {
    history.pushState({}, null, window.location.origin);
}

This will remove the parameters in url address bar and should not affect server-side logs. Note that it will also remove them in the history so if the user clicks the back button and then forward, the page will be loaded without the parameters. See MDN for details and browser compatibility.

Upvotes: 2

Related Questions