谷歌分析-媒体信息从cookie成功的交易

Google Analytic - Medium information from cookie on successful transaction

本文关键字:cookie 成功 交易 信息 媒体 谷歌      更新时间:2023-09-26

谁能告诉我正确的方向?

我想从Google生成的"__UTMZ"cookie中抓取媒体。我目前在我的电子商务商店的标题中有旧的"ga.js"代码,当访问者到达我的网站时生成cookie。我还编写了一些代码,用于读取成功交易页面上的cookie,并将带有cookie字符串的订单信息保存到日志文件中。

它似乎工作良好,在开发中。然而,在我的现场实现这一点后。我正在为分析cookie获取空白信息。我得到了订单信息,但没有cookie字符串应该在的地方。当我自己做的时候,它工作了,但我要么得到"not-set"或"(none)",我想这是因为我直接到达了站点。

我做错了吗?我真的只想从订单的媒介,无论是自然搜索还是cpc。就是这样。

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)"}

参考

下面是在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()函数来清理数据

供参考,你得到的是"(none)"值因为你直接登陆了你的网站所以没有任何媒介

还使用谷歌的chrome扩展标签助手来检查您是否正确安装了GA代码在您的网站

有标准字段campaignMedium

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