我希望能够使用Javascript提取包含所有广告系列跟踪信息的Google Analytics跟踪Cookie中存储的数据。它需要使用ga.js而不是urchin.js来使用较新版本的GA。我找到了一个与urchin.js一起使用的方法,但我们不会将其用于跟踪。有人知道如何从Google使用的Cookie中提取Campaign
,Source
,Medium
,Content
和Term
吗?
答案 0 :(得分:7)
我最终自己搞清楚了。我只是挖掘了cookie存储的内容并提取了信息。这就是我想出的:
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);
}
}
}
我确信它可以更精简但我很高兴能够实现这一目标。一旦你拥有了这些变量,你可以随心所欲地做任何事情。
答案 1 :(得分:4)
我写这篇文章是为了将值解析为关联数组并使用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;
}
答案 2 :(得分:4)
现在有效:
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();
答案 3 :(得分:0)
回复@Eduardo评论(关于选择的答案不适用于AdWords),如果您想让它适用于AdWords流量,这个答案可能值得一试:
How to capture if a visitor is from a google adwords click
虽然没试过。