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

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

问题描述

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

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

  • 推荐网站(GA 中的来源")
  • 付费或自然(GA 中的中等")
  • 关键字
  • 第一次/回来
  • 访问次数

如果 Google Analytics API 无法实现,是否还有其他简单方法可以做到(除了解析 HTTP Referer、将访问统计信息存储在 DB 中等)?

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.)?

推荐答案

如果您仍在使用 ga.js(Google Analytics 跟踪代码的旧版本),您可以使用以下代码生成您想要的值浏览器,通过读取浏览器 cookie.(大多数人会迁移到analytics.js,它不会在__utmz cookie 中存储活动信息.)

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.)

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

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

关于推荐、媒介和活动信息:

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)`

对于浏览量和访问量:

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

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

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天全站免登陆