如何使用 Javascript 从他们的 cookie 中提取 Google Analytics 活动数据? [英] How do I extract Google Analytics campaign data from their cookie with Javascript?

查看:26
本文介绍了如何使用 Javascript 从他们的 cookie 中提取 Google Analytics 活动数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用 Javascript 提取存储在 Google Analytics 跟踪 cookie 中的数据以及所有广告系列跟踪信息.它需要使用 ga.js 而不是 urchin.js 来处理较新版本的 GA.我找到了一种适用于 urchin.js 的方法,但我们不使用它进行跟踪.有谁知道如何提取CampaignSourceMediumContentTerm> 来自 Google 使用的 cookie?

I'd like to be able to pull out the data stored in the Google Analytics tracking cookie with all the campaign tracking information using Javascript. It needs to work with the newer version of GA using ga.js, not urchin.js. I found a method that works with urchin.js but we don't use that for our tracking. Does anybody know how to extract the Campaign, Source, Medium, Content and Term from the cookie Google uses?

推荐答案

我最终自己解决了这个问题.我只是挖掘了 cookie 存储的内容并提取了信息.这是我想出的:

I ended up figuring this out on my own. I just dug into what the cookie had stored and extracted the information. Here's what I came up with:

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

我相信它可以更精简,但我很高兴能做到这一点.一旦你有了这些变量,你就可以用它们做任何你需要的事情.

I'm sure it could be more streamlined but I was just happy to get this far with it. Once you have these variables you can do whatever you need with them.

这篇关于如何使用 Javascript 从他们的 cookie 中提取 Google Analytics 活动数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆