How do I extract Google Analytics campaign data from their cookie with Javascript?

13,770

Solution 1

I ended up figuring this out on my own. I just dug into what the cookie had stored and extracted the information. Here's what I came up with:

var ga_source = '';
var ga_campaign = '';
var ga_medium = '';
var ga_term = '';
var ga_content = '';
var gc = '';
var c_name = "__utmz";
if (document.cookie.length>0){
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1){
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        gc = unescape(document.cookie.substring(c_start,c_end));
    }
}
if(gc != ""){
    var z = gc.split('.'); 
    if(z.length >= 4){
    var y = z[4].split('|');
        for(i=0; i<y.length; i++){
            if(y[i].indexOf('utmcsr=') >= 0) ga_source = y[i].substring(y[i].indexOf('=')+1);
            if(y[i].indexOf('utmccn=') >= 0) ga_campaign = y[i].substring(y[i].indexOf('=')+1);
            if(y[i].indexOf('utmcmd=') >= 0) ga_medium = y[i].substring(y[i].indexOf('=')+1);
            if(y[i].indexOf('utmctr=') >= 0) ga_term = y[i].substring(y[i].indexOf('=')+1);
            if(y[i].indexOf('utmcct=') >= 0) ga_content = y[i].substring(y[i].indexOf('=')+1);
        }
    }
}

I'm sure it could be more streamlined but I was just happy to get this far with it. Once you have these variables you can do whatever you need with them.

Solution 2

I've rewritten this to parse the values onto an associative array and make use of quicksmode cookie functions:

function parseAnalyticsCookie() {
    // inspiration from http://stackoverflow.com/questions/1688657/how-do-i-extract-google-analytics-campaign-data-from-their-cookie-with-javascript
    // readCookie is from // http://www.quirksmode.org/js/cookies.html
    // utmcsr = utm_source
    // utmccn = utm_campaign
    // utmcmd = utm_medium
    // utmctr = utm_term
    // utmcct = utm_content
    var values = {};
    var cookie = readCookie("__utmz");
    if (cookie) {
        var z = cookie.split('.'); 
        if (z.length >= 4) {
            var y = z[4].split('|');
            for (i=0; i<y.length; i++) {
                var pair = y[i].split("=");
                values[pair[0]] = pair[1];
            }
        }
    }
    return values;
}

Solution 3

Now works:

   function extractAnalyticsData(){
      var data = {};
      var ga_source = '';
      var ga_campaign = '';
      var ga_medium = '';
      var ga_term = '';
      var ga_content = '';
      var gc = '';
      var c_name = "__utmz";
      if (document.cookie.length>0){
          c_start=document.cookie.indexOf(c_name + "=");
          if (c_start!=-1){
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            gc = unescape(document.cookie.substring(c_start,c_end));
          }
      }
      data.gc = gc;
      if(gc != ""){
          var y = gc.split('|'); 
        for(i=0; i<y.length; i++){
          if(y[i].indexOf('utmcsr=') >= 0) data.ga_source = y[i].substring(y[i].indexOf('=')+1);
          if(y[i].indexOf('utmccn=') >= 0) data.ga_campaign = y[i].substring(y[i].indexOf('=')+1);
          if(y[i].indexOf('utmcmd=') >= 0) data.ga_medium = y[i].substring(y[i].indexOf('=')+1);
              if(y[i].indexOf('utmcct=') >= 0) data.ga_content = y[i].substring(y[i].indexOf('=')+1);
          if(y[i].indexOf('utmctr=') >= 0) data.ga_term = y[i].substring(y[i].indexOf('=')+1);
        }
      }
      return data;
    };
    extractAnalyticsData();
Share:
13,770

Related videos on Youtube

Shawn Steward
Author by

Shawn Steward

I'm a web developer working full time with ASP.NET and do freelancing on the side typically with PHP and MySQL.

Updated on April 17, 2022

Comments

  • Shawn Steward
    Shawn Steward about 2 years

    I'd like to be able to pull out the data stored in the Google Analytics tracking cookie with all the campaign tracking information using Javascript. It needs to work with the newer version of GA using ga.js, not urchin.js. I found a method that works with urchin.js but we don't use that for our tracking. Does anybody know how to extract the Campaign, Source, Medium, Content and Term from the cookie Google uses?

  • Eduardo
    Eduardo about 13 years
    If the user visits your site via AdWords this won't work since the AdWords values for these variables are not present on the cookie.
  • Raptor
    Raptor over 8 years
    Does not work with facebook with utmz utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct‌​=/ this become like this: ["facebook", "", "", "", ""] since it executes gc.split('.') it remove the .com from facebook
  • Raptor
    Raptor over 8 years
    Does not work with facebook with utmz utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct‌​=/ this become like this: Object {utmcsr: "facebook"} where as it should be utmsource:facebook.com utmedium:referral utcampaign:(referral)
  • Raptor
    Raptor over 8 years
    Seems to handle facebook with utmz utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct‌​=/ correctly! Result: Object {gc: "***.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|u‌​tmcct=/", ga_source: "facebook.com", ga_campaign: "(referral)", ga_medium: "referral", ga_content: "/"} which is perfect!