cleanerUrlCleaner is a plugin for the Google Analytics tracking code (analytics.js) that removes the URL parameters used by Google Analytics to identify traffic source, medium, and campaign after the hit has been sent to Google Analytics. So don’t worry – tracking will work as usual!

This not only makes your URL prettier but also prevents users from bookmarking or sharing a URL containing campaign parameters. Why is it important? Well, if someone spreads a link to your site containing, for instance, utm_source=twitter on Facebook, any subsequent traffic from that link will be counted as coming from Twitter rather than Facebook – unless our UrlCleaner plugin is being used!

Because UrlCleaner uses history.replaceState, it won’t have any effect in older browsers (Internet Explorer 9 and earlier) that lack support for it.

Implementation

Include this script after the Google Analytics script on all pages of your website:

<script async src=”//storage.googleapis.com/outfox/urlCleaner_min.js”></script>

Then register UrlCleaner as a plugin for Google Analytics by requesting
ga(‘require’, ‘urlCleaner’). After the pageview has been sent to Google Analytics, request ga(‘urlCleaner:clean’) to clean your URL and strip the campaign parameters from it.

Sample code:

ga(‘create’, ‘UA-1234567-1’, ‘auto’);
ga(‘require’, ‘urlCleaner’);
ga(‘send’, ‘pageview’);
ga(‘urlCleaner:clean’);

 

Configuration

Parameter Type Description
debug boolean Logs info to the console about which parameters have been removed by UrlCleaner if you set the value to true.
allowAnchor boolean Removes parameters following the # character in the URL if you change the setting to true. Do this if you add campaign parameters after the # character.
params string array If you want UrlCleaner to remove other parameters than the ones used by Google Analytics, send the names of the parameters in an array.

 

Sample code:

ga(‘require’, ‘urlCleaner’, {‘debug’: true, ‘allowAnchor’: true, ‘params’:[‘username’,’age’]});