【发布时间】: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