Class: PlataformaSocial::UTM

Inherits:
Object
  • Object
show all
Defined in:
lib/plataforma_social/utm.rb

Class Method Summary collapse

Class Method Details

.add_utm_to_url(url, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/plataforma_social/utm.rb', line 3

def self.add_utm_to_url url, options = {}
  settings = {
    utm_source: 'dito',
    utm_medium: nil,
    utm_campaign: nil
  }.merge options

  parsedURL = URI.parse(url)

  params = Rack::Utils.parse_nested_query(parsedURL.query).merge(settings)
  hash   = ''

  if url.index('#')
    splittedHash = url.split('#')
    hash = splittedHash[1]
    url = splittedHash[0]
  end

  url = url.split('?')[0] + '?' + params.to_param
  url = url + '#' + hash if hash.present?

  url
end