about summary refs log tree commit diff
path: root/app/helpers/url_helper.rb
blob: 3fad2d6d3e4d2976025f89fad006622f21a6e844 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module UrlHelper
  def sanitize_query_string(url)
    return if url.blank?
    url = Addressable::URI.parse(url)
    return url.to_s if url.query.blank?
    return unless '='.in?(url.query)
    params = CGI.parse(url.query)
		params.delete_if do |key|
      k = key.downcase
      next true if k.start_with?(
        '_hs',
        'ic',
        'mc_',
        'mkt_',
        'ns_',
        'sr_',
        'utm',
        'vero_',
        'nr_',
        'ref',
      )
      next true if 'track'.in?(k)
      next true if [
        'fbclid',
        'gclid',
        'ncid',
        'ocid',
        'r',
        'spm',
      ].include?(k)
      false
    end
    url.query_values = params
    return url.to_s
  rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
    return '#'
  end
end