javascript 使用 Google Analytics 获取当前访问者的推荐人、付费/自然和关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5631830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:50:41  来源:igfitidea点击:

Get the referrer, paid/natural and keywords for the current visitor with Google Analytics

javascriptgoogle-analyticsreferrervisitor-statistic

提问by Evgenii

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

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

  • Referrer site ('Source' in GA)
  • Paid or natural ('Medium' in GA)
  • Keyword
  • First time/returning
  • Number of visits
  • 推荐网站(GA 中的“来源”)
  • 付费或自然(GA 中的“中等”)
  • 关键词
  • 第一次/回来
  • 访问次数

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

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

回答by Yahel

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

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

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

我假设您有一个名为readCookie(); 我倾向于使用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 safelydo numeric comparisons, even though JS is loosely typed.

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

回答by Ewan Heming

You should be able to get it all from the cookies set by Google Analytics. They are stored as first party cookies so JavaScript running on a page will be able to read them. The number of visits can be obtained from the last part of the __utmacookie and the referrer can be taken from __utmz. The source is the utmcsrbit of __utmzwhile the medium comes from utmcmdand the keyword is utmctr.

您应该能够从 Google Analytics 设置的 cookie 中获取所有信息。它们存储为第一方 cookie,因此在页面上运行的 JavaScript 将能够读取它们。访问次数可以从__utmacookie的最后部分获取,引用可以从 中获取__utmz。来源是utmcsr位,__utmz而媒体来自,utmcmd关键字是utmctr

回答by Dawid Adach

Apparently, this doesn't work anymore. Since 2013 where use enabled SSL, all keywords are stripped out from referer URL.

显然,这不再起作用了。自 2013 年使用启用 SSL 以来,所有关键字都从引用 URL 中删除。

From now on the only option to get some statistic (not per user) data is to enable Search Console.

从现在开始,获取一些统计数据(不是每个用户)数据的唯一选择是启用 Search Console。