获取网页浏览的Google Analytics服务器端授权计算分析数据并将其显示在首页上的随机访问者中 [英] Google Analytics Server Side Authorization to get Page View Count analytics data and display it to random visitors on the front page

查看:120
本文介绍了获取网页浏览的Google Analytics服务器端授权计算分析数据并将其显示在首页上的随机访问者中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不必登录或验证Google Analytics等的情况下向网页显示访问者数量?



我正在尝试实施Google Analytics服务器端授权获取页面查看计数分析数据并将其显示在首页上的随机访问者



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



我试过这个实现。但是,googleServiceAccountKey中没有client_email。我在哪里获得client_email?
请分享hwo在node.js + react apps上进行服务器端授权!谢谢

 从'googleapis'导入谷歌
导入googleServiceAccountKey从'/ path /到/ private / google-service- account-private-key.json'//查看有关如何生成服务帐户的文档

const googleJWTClient = new google.auth.JWT(
googleServiceAccountKey.client_email,
null ,
googleServiceAccountKey.private_key,
['https://www.googleapis.com/auth/analytics.readonly'],//您可能需要指定除分析以外的范围
null,


googleJWTClient.authorize((error,access_token)=> {
if(error){
return console.error(无法访问 token,e)
}
// ... access_token准备好用于获取数据并返回客户端
//甚至将access_token提供给客户端以便在`gapi.analytics中使用。 auth.authorize`
})

我尝试过带有身份验证按钮的示例,但我不知道不希望用户必须在ord中进行身份验证呃看到页面查看计数。



比身份验证和使用Json字符串简单得多建立自己的我认为是仪表板。






编辑:我现在已经注意到仪表板显示为私有。

你可以通过转到Google数据工作室的主屏幕来解决此问题。

单击要公开的仪表板上的菜单按钮。



如果仍然无效,可能是Google Analytics的数据需要用户身份验证。



转到Google Data studio的主屏幕,然后点击左侧菜单中的数据来源。

点击来自的数据来源谷歌分析。

确保它在右上角设置为使用所有者的证书。


How can I display visitor count to your web page without having to logging in or authenticating like Google Analytics?

I'm trying to implement Google Analytics Server Side Authorization to get Page View Count analytics data and display it to random visitors on the front page

I read their docs and found service account but the issue is that there is no full example of doing it written in JavaScript.

I tried this implementation. However, there is no client_email in googleServiceAccountKey. Where do I get client_email ? Please share hwo to do Server Side Authorization on node.js + react apps! thanks

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

I've tried examples with authentication buttons but I don't want users have to authenticate in order to see a page view count. https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js

/* Non Google Analytics API*/ https://www.freevisitorcounters.com/ One example that counts every time user refresh the page which is wrong implementation. http://javascriptexample.blogspot.de/2008/09/visit-counter.html

Does anyone know how to do this? Or other libraries or solutions are welcome too.

thanks in advance!

解决方案

I found a solution just today actually to display data without authentication.

Google has a tool called data studio.
https://datastudio.google.com/

You can link this to your Google analytics account.
After you create an account you build a dashboard to present to the users with graphs tables and charts or what you want.
It's easy click and drag.
Then you can click the embed button and get a html code you add to your webpage.
It's a simple one line ifram tag.

This is the dashboard I made today, it took me about 20-30 minutes.
http://www.hoppvader.nu/Data.php

Much simpler than authentication and using Json strings to build an own dashboard in my opinion.



EDIT: I have now noticed the dashboard shows as private.
You can fix this by going to the main screen of Google data studio.
Click the menu button on the dashboard you want public.
Click Share.
Advanced.
Click on change at "Who has access" and change it to public and save.

If it still does not work, it may be that the data from Google analytics require user authentication.

Go to the main screen of Google Data studio and click "Data sources" in the menu to the left.
Click on the data source from Google analytics.
Make sure it's set to "USING OWNER'S CREDENTIALS" at the top right corner.

这篇关于获取网页浏览的Google Analytics服务器端授权计算分析数据并将其显示在首页上的随机访问者中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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