Google Analytic - 成功交易时Cookie中的中等信息

时间:2015-10-19 13:02:35

标签: javascript php cookies google-analytics

有人能指出我正确的方向吗?

我想从Google生成的“__UTMZ”Cookie中获取媒介。我目前在电子商务商店的标题中包含旧的“ga.js”代码,以便在访问者访问我的网站时生成Cookie。我还有在成功的交易页面上读取cookie的代码,并将带有cookie字符串的订单信息保存到日志文件中。

在开发过程中似乎工作正常。但是,在我的实时网站上实现此功能之后。我收到了分析cookie的空白信息。我得到了订单信息,但cookie字符串应该没有。当我自己做的时候它起了作用,但是我要么“未设置”或“(无)”,我认为是因为我直接到达了网站。

我是不是错了?我真的只想要订单中的介质,无论是有机搜索还是cpc。而已。

3 个答案:

答案 0 :(得分:1)

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');

我用于解析__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)"}

答案 1 :(得分:0)

供参考Get cookie by name

以下是在utmz中获取utmcmd的代码

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');

获得__utmz的值后,您可以执行一些split()和pop()函数来清理数据

你是否得到了#34;(无)&#34;价值,因为你直接登陆你的网站,所以没有任何媒体

使用Chrome扩展程序Tag Assistant来检查您是否在网站上正确安装了GA代码

答案 2 :(得分:0)