Remove Google Analytics tracking data from your URLs

I found a nice article at jasoncodes. which helped me to solve my problem about the query paramaters in url..

If you use FeedBurner for your feeds, then it has a feature which integrates with Google Analytics to tell you how many of your hits come via feed readers. This is rather useful because feed readers typically don’t pass any useful referrer information (especially desktop clients). This tracking can be enabled by ticking the “Track clicks as a traffic source in Google Analytics” option in your FeedBurner account.

However, this comes with a disadvantage to those of us who care about clean URLs.

It pollutes the urls like this:

http://example.com/2011/03/post-name/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+example

utm_source=twitter is another tracking code which gets added to short URLs when you post to Twitter in addition to tracking your feeds. Either way, a big problem with this is that these URLs are often shared around and not everybody takes the time to remove all the cruft and share the clean canonical URL.

Luckily Google Analytics API has asynchronous tracking that lets us queue a function to be ran after the tracking request has been sent. This combined with the history.replaceState method in HTML5 lets us remove the the tracking data from the URL without reloading the page or breaking the browser’s history.

Just add the following code to your Google Analytics tracking JavaScript to make this happen :

_gaq.push(function() {
if (history.replaceState) history.replaceState(null, '', location.pathname + location.hash);
});

So the full tracking script will be:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(function() {
if (history.replaceState) history.replaceState(null, '', location.pathname + location.hash);
});

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

Note: This method also clean the other data which you have entered (besides google analytics and feedburner. Like the wordpress mobile pack force redirect ) without loosing its functionality.

[Source – jasoncodes. ]

  • http://sorebuttcheeks.blogspot.com/ steroids

     i wasn’t aware the info was made public.

    • http://sahilp.in SAHIL

      it is available publicly, check the google analytics api

  • Guest

    Plagiarism is so low. How could you rip off someone else’s work? Asshole.

    • http://sahilp.in SAHIL

      calm down bro why the abusive language you can just say it nicely … i have added link back to your site…