Search code examples
rubygoogle-analytics

How can I remove Google tracking parameters (UTM) from an URL?


I have a bunch of URLs which I would like to clean. They all contain UTM parameters, which are not necessary, or rather harmful in this case. Example:

http://houseofbuttons.tumblr.com/post/22326009438?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+HouseOfButtons+%28House+of+Buttons%29

All potential parameters begin with utm_. How can I remove them easily with a ruby script / structure without destroying other potentialy "good" URL parameters?


Solution

  • You can apply a regex to the urls to clean them up. Something like this should do the trick:

    url = 'http://houseofbuttons.tumblr.com/post/22326009438?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+HouseOfButtons+%28House+of+Buttons%29&normal_param=1'
    url.gsub(/&?utm_.+?(&|$)/, '') => "http://houseofbuttons.tumblr.com/post/22326009438?normal_param=1"