有人能指出我正确的方向吗?
我想从 Google 生成的“__UTMZ”cookie 中获取媒体。目前,我的电子商务商店的 header 中有旧的“ga.js”代码,用于在访问者访问我的网站时生成 cookie。我还有代码读取成功交易页面上的 cookie,并将订单信息与 cookie 字符串保存到日志文件中。
它似乎在开发中工作正常。但是,在我的实时网站上实现此操作后。我得到了分析 cookie 的空白信息。我得到了订单信息,但 cookie 字符串应该在的地方没有任何内容。当我自己做的时候它有效,但我要么得到“未设置”或“(无)”,我认为这是因为我直接到达该网站。
我的做法是错的吗?我真的只是想要订单中的媒介,比如它是有机搜索还是每次点击费用。就是这样。
最佳答案
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
cookies :
/**
* 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)"}
关于javascript - Google Analytic - 来自 cookie 的关于成功交易的媒介信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33215161/