How do I remove Google Tracking (UTM) settings from a URL?

I have a bunch of URLs that I would like to clear. All of them contain UTM parameters, which in this case are not necessary or, rather, harmful. 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 start with utm_ . How can I easily remove them using the ruby ​​script // "> structure without destroying the other potential" good "URL parameters?

+4
source share
2 answers

You can apply regex to URLs to clear them. 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" 
+8
source

This uses the lib URI to deconstruct and modify the request (without a regular expression):

 require 'uri' str ='http://houseofbuttons.tumblr.com/post/22326009438?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+HouseOfButtons+%28House+of+Buttons%29&normal_param=1' uri = URI.parse(str) clean_key_vals = URI.decode_www_form(uri.query).reject{|k, _| k.start_with?('utm_')} uri.query = URI.encode_www_form(clean_key_vals) p uri.to_s #=> "http://houseofbuttons.tumblr.com/post/22326009438?normal_param=1" 
+9
source

Source: https://habr.com/ru/post/1438936/


All Articles