Last Updated: February 25, 2016
·
1.015K
· polimorfico

Extract Google Analytics campaign data from their cookie

Inspiration from http://stackoverflow.com/questions/1688657/how-do-i-extract-google-analytics-campaign-data-from-their-cookie-with-javascript

I use https://github.com/carhartl/jquery-cookie to extract data from cookie.

parseAnalyticsCookie = () -> 
  # utmcsr = utm_source
  # utmccn = utm_campaign
  # utmcmd = utm_medium
  # utmctr = utm_term
  # utmcct = utm_content
  result = {}
  cookie = $.cookie("__utmz")
  if cookie
    z = cookie.split('.')
    if z.length >= 4
      for p in z[4].split('|')
        params = p.match /(.*)=(.*)/
        result[params[1]] = params[2] if params != null
  return result