How do I extract Google Analytics campaign data from their cookie with Javascript?

后端 未结 4 1537
抹茶落季
抹茶落季 2020-12-24 15:41

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

4条回答
  •  离开以前
    2020-12-24 16:01

    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

    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.

提交回复
热议问题