使用Google Analytics获取当前访问者的推荐人,付费/自然和关键字 [英] Get the referrer, paid/natural and keywords for the current visitor with Google Analytics

查看:273
本文介绍了使用Google Analytics获取当前访问者的推荐人,付费/自然和关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过JavaScript使用Google Analytics API获取关于当前访问者的以下信息?




  • 引荐网站('Source'在GA中)
  • 付费或自然(GA中的'Medium')

  • 关键字
  • 第一次/返回

  • 访问次数



如果Google Analytics API无法使用, easy 的方式来做到这一点(除了解析HTTP Referer,将访问统计信息存储在DB等中)?

解决方案

如果您仍在使用ga.js(旧版Google Analytics(分析)跟踪代码),则可以使用下面的代码通过浏览器cookie来在浏览器中生成所需的值。 (大多数人将迁移到analytics.js,它不会将广告系列信息存储在 __ utmz cookie中)。



我假设你有一个名为 readCookie()的函数;我倾向于使用 QuirksMode



转介,中等和广告系列信息
$ b

  var utmz = readCookie( '__utmz'); //使用cookie读取函数
var vals =(function(){
var pairs = utmz.split('。')。slice(4).join('。')。split(' |');
var ga = {};
for(var i = 0; i< pairs.length; i ++){
var temp = pairs [i] .split(' =');
ga [temp [0]] = temp [1];
}
return ga;
})();

//vals.utmcmd:中等(有机,转介,直接等)
//vals.utmcsr:来源(谷歌,facebook.com等)
// vals.utmcct:content(index.html等)
//vals.utmccn:campaign
//vals.utmctr:term(搜索词)
//vals.utmgclid:adwords- (值不相关,但表示它的AdWords自动标记的流量,但它暗示medium = cpc,尽管它将设置为(none)或(不是%20set)

浏览量和访问次数:

<$ p ('。')[1];
var visits = readCookie('__ utma')。split('。')。$ p> var pageviews = readCookie('__ utmz')。 pop()//返回访问次数

显然,如果 )=== 1 ,那么它是第一次访问者(请记住:cookie中的值将是字符串,因此您需要将它们安全地转换为数字 数字比较,即使JS是松散类型。


Is it possible to get the following information about the current visitor using Google Analytics API with JavaScript?

  • Referrer site ('Source' in GA)
  • Paid or natural ('Medium' in GA)
  • Keyword
  • First time/returning
  • Number of visits

If it's not possible with Google Analytics API is there any other easy way to do it (apart from parsing HTTP Referer, storing the visits statistics in DB etc.)?

解决方案

If you're still using ga.js (the legacy version of Google Analytics tracking code), you can use the below code to generate the values you want within the browser, by reading browser cookies. (Most people will have migrated to analytics.js, which does not store the campaign information in the __utmz cookie.)

I assume you have a function called readCookie(); I tend to use the one from QuirksMode

For referral, medium, and campaign information:

var utmz = readCookie('__utmz'); //using a cookie reading function
var vals = (function() {
        var pairs = utmz.split('.').slice(4).join('.').split('|');
        var ga = {};
        for (var i = 0; i < pairs.length; i++) {
            var temp = pairs[i].split('=');
                ga[temp[0]] = temp[1];
        }
        return ga;
    })();

//vals.utmcmd: medium (organic, referral, direct, etc)
//vals.utmcsr: source (google, facebook.com, etc)
//vals.utmcct: content (index.html, etc)
//vals.utmccn: campaign 
//vals.utmctr: term (search term)
//vals.utmgclid: adwords-only (value is irrelevant, but means its AdWords autotagged traffic, but it implies that medium=cpc, even though it'll be set to `(none)` or `(not%20set)`

For pageview count and visit count:

var pageviews = readCookie('__utmz').split('.')[1];
var visits = readCookie('__utma').split('.').pop() //returns number of visits

Obviously, if (+visits)===1, then its a first time visitor. (Remember: values from cookies will be strings, so you'll need to cast them to numbers to safely do numeric comparisons, even though JS is loosely typed.

这篇关于使用Google Analytics获取当前访问者的推荐人,付费/自然和关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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