1

Can someone point me in the right direction?

I want to grab the medium from the "__UTMZ" cookie that is generated by Google. I currently have the old "ga.js" code in the headers on my ecommerce store to generate the cookie when a visitor reaches my site. I also have code that reads in the cookie on the successful transaction page and saves the order information with the cookie string to a log file.

It seems to work fine, in development. However, after implementing this on my live site. I am getting blank information for the analytic cookie. I get the order information but there's nothing where the cookie string should be. It worked when I was doing it myself but I was either getting "not-set" or "(none)", which I figured was because I reaching the site directly.

Am I going about this wrong? I really just want the medium from the order, as in whether it was an organic search or cpc. That's it.

DEV PSC
  • 11
  • 2

3 Answers3

2

Modified version of Glize/dom/Cookie:

  /**
   * Gets the value for the first cookie with the given name.
   * @param {string} key The name of the cookie to get.
   * @param {string=} opt_default The optional default value.
   * @return {string} The value of the cookie. If no cookie is set this
   *     returns opt_default or undefined if opt_default is not provided.
   */
  function getCookie(key, opt_default)  {
    return unescape(
        (document.cookie.match(key + '=([^;].+?)(;|$)') || [])[1] || opt_default || '');
  }

  // Gets the value of utmz
  var cookie = getCookie('__utmz');

Another one function I'm using for parse __utmz cookie:

/**
 * Gets campaign data from utmz cookie.
 * @return {!Object.<string, string>} Returns parsed data as:
 * {
 *   'utmcsr': 'Source (utm_source)',
 *   'utmcmd': 'Medium (utm_medium)',
 *   'utmccn': 'Campaign (utm_campaign)',
 *   'utmctr': 'Keyword (utm_term)',
 *   'utmcct': 'Ad Content (utm_content)'
 * }
 */
function getCampaignData() {
    /** @type {!Object.<string, string>} */ var result = {};
    /** @type {string} */ var utmz = getCookie('__utmz').split('|');
    /** @type {number} */ var length = utmz.length;
    /** @type {number} */ var i = 0;

    for (; i < length;) {
        /** @type {!Array} */ var pairs = utmz[i++].split('=');
        /** @type {string} */ var key = pairs[0].split('.').pop()
        result[key] = pairs.pop();
    }
    return result;
}

console.log(getCampaignData());
// Object {utmcsr: "(direct)", utmccn: "(direct)", utmcmd: "(none)"}
Valentin Podkamennyi
  • 7,161
  • 4
  • 29
  • 44
0

For reference Get cookie by name

Here is the code to get the utmcmd inside utmz

function getCookie(name) { //Gets the cookie
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}

// Gets the value of utmz
var cookie = getCookie('__utmz');

After you get the value of the __utmz you can do some split() and pop() functions to clean up your data

And FYI you are getting the "(none)" value because you landed on you site directly so there wasn't any medium

Use also the chrome extension Tag Assistant by Google to check if you had correctly installed the GA code on your site

Community
  • 1
  • 1
sky
  • 151
  • 10
0

There is standard field campaignMedium

https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#campaignMedium

Alex
  • 639
  • 1
  • 5
  • 7