【问题标题】:Display visitor count without authenticating with Google Analytics无需使用 Google Analytics 进行身份验证即可显示访问者数量
【发布时间】:2018-05-07 21:11:09
【问题描述】:

如何在无需像 Google Analytics 那样登录或进行身份验证的情况下向您的网页显示访问者数量?

我阅读了他们的文档并找到了服务帐户,但问题是没有用 JavaScript 编写的完整示例。

我尝试了这个实现。 但是,googleServiceAccountKey 中没有 client_email。

import google from 'googleapis'
import googleServiceAccountKey from '/path/to/private/google-service-account-private-key.json' // see docs on how to generate a service account

const googleJWTClient = new google.auth.JWT(
  googleServiceAccountKey.client_email,
  null,
  googleServiceAccountKey.private_key,
  ['https://www.googleapis.com/auth/analytics.readonly'], // You may need to specify scopes other than analytics
  null,
)

googleJWTClient.authorize((error, access_token) => {
   if (error) {
      return console.error("Couldn't get access token", e)
   }
   // ... access_token ready to use to fetch data and return to client
   // even serve access_token back to client for use in `gapi.analytics.auth.authorize`
})

我尝试了带有身份验证按钮的示例,但我不希望用户必须进行身份验证才能查看页面查看次数。 https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js

/* 非谷歌分析 API*/ https://www.freevisitorcounters.com/ 一个示例,每次用户刷新页面时都会计数,这是错误的实现。 http://javascriptexample.blogspot.de/2008/09/visit-counter.html

有人知道怎么做吗?或者也欢迎其他库或解决方案。

提前致谢!

【问题讨论】:

  • 到目前为止你尝试过什么?如果你真的写了一些关于你尝试过的东西和你真正想要的东西,那么你获得帮助的机会就会大得多。与可能的解决方案相关的具体问题会很好,而不是“我该怎么做”。 :)
  • 我已经编辑了帖子并添加了我尝试过的内容:)

标签: javascript google-analytics google-analytics-api


【解决方案1】:

使用 node.js 时,您可以执行类似的操作

var server = http.createServer(app);    

server.getConnections(function(error, count) {

    console.log(count);

});

【讨论】:

    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多