Skip to content

Instantly share code, notes, and snippets.

@pedrobachiega
Created November 20, 2012 12:14
Show Gist options
  • Save pedrobachiega/4117593 to your computer and use it in GitHub Desktop.
Save pedrobachiega/4117593 to your computer and use it in GitHub Desktop.
Reading and parsing GA Cookie UTMZ
cookie_ga_utmz = cookies['__utmz']
source, medium, campaign, value = nil
splited = cookie_ga_utmz.split("|")
splited.each do |token|
if token.include?("utmcsr=")
source = URI.unescape(token.split("utmcsr=")[1])
elsif token.include?("utmcmd=")
medium = URI.unescape(token.split("utmcmd=")[1])
elsif token.include?("utmccn=")
campaign = URI.unescape(token.split("utmccn=")[1])
elsif (token.include?("utmctr=") || token.include?("utmcct="))
#encoding problem when ISO-8859-1, its is ignoring chars like cedilha
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
value = ic.iconv(URI.unescape(token.split("=")[1]) + ' ')[0..-2]
end
end
if cookie_ga_utmz.include?("utmgclid=")
medium = "cpc"
end
name = "#{medium} - #{value}"
case medium
when "(none)"
name = "Tráfego Direto"
when "referral"
name = "Links (outros sites) - #{source}#{value}"
when "organic"
name = "Busca Orgânica - #{value}"
when "cpc"
name = "Campanhas (CPC) - #{value}"
when "email"
name = "E-mail"
when "feed", "rss"
name = "Feed RSS"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment