如何从网址中删除Google跟踪参数(UTM)?

时间:2012-10-10 14:55:31

标签: ruby google-analytics

我有一堆网址,我想清理。它们都包含UTM个参数,这些参数不是必需的,或者在这种情况下非常有害。例如:

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

所有潜在参数均以utm_开头。 如何在不破坏其他潜在的“好”URL参数的情况下使用ruby脚本/结构轻松删除它们?

2 个答案:

答案 0 :(得分:10)

这使用URI lib来解构和更改查询字符串(没有正则表达式):

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"

答案 1 :(得分:8)

您可以将正则表达式应用于网址以进行清理。这样的事情可以解决问题:

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"