将观看次数从Google Analytics(分析)添加到网页 [英] Add view count to webpage from Google Analytics
问题描述
我正在尝试为网页创建观看次数功能.为此,我创建了一个新属性,并从 Google Analytics(分析)生成了跟踪ID.
I am trying to create a view count feature for a webpage. To do this, I created a new property and generated a tracking id from Google Analytics.
有没有办法从那里使用页面访问数据并将其显示在我跟踪的同一页面上?
Is there a way I can use the page vists data from there and display it on the same page that I am tracking?
推荐答案
可以,但是由于GA API已通过身份验证,因此您将需要以一种或另一种方式处理该身份验证.
You can, but since GA APIs are authenticated, you will need to handle that authentication one way or another.
您基本上有3个选择:
- 正在执行OAuth的用户:默认情况下,您需要实现OAuth流程,例如查询浏览器正在这样做,如果计数器是公开的,显然这是不够的,因为访问者将没有适当的GA权限来从该属性获取数据
- 预先生成的访问令牌:您可以手动生成
访问令牌
,然后使用该令牌发出请求: https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference#auth-options
- User doing OAuth: by default you would need to implement the OAuth flow like the Query Explorer is doing, which is obviously not adequate if the counter is meant to be public, as visitors won't have the appropriate GA permissions to get data from that property
- Pre generated access token: you can generate the
access token
manually, and use that token to make the requests: https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference#auth-options
serverAuth.access_token类型:字符串
serverAuth.access_token Type: string
如果您已经具有有效的访问令牌,则可以将其传递给直接授权方法,不会提示用户授权.有关如何检索访问令牌的详细信息,请参见OAuth 2.0文档.
If you already have a valid access token, you can pass it to the authorize method directly and the user will not be prompted to authorize. For details on how to retrieve an access token, see the OAuth 2.0 documentation.
- 中间件:您可以使用工具从GA API中提取数据,然后从该工具中查询数据.例如,您可以:
- Middleware: you can use a tool to pull data from the GA API and then query data from that tool. For instance you can:
- 使用Google Analytics(分析)插件将您的数据提取到Google表格
- 使用导入范围 将该数据导入另一个Google表格中
- 从2发布Google表格,因此可以通过API(例如CSV)公开读取.您将获得一个类似于
https://docs.google.com/spreadsheets/d/e/{some-uid}/pub?gid = 0& single = true& output = csv 的URL.代码>
- 从网站的该URL中获取数据(例如
- Use the GA add-on to pull your data to a Google Sheet
- Import that data into another Google Sheet with IMPORTRANGE
- Publish the Google Sheet from 2 so it's publicly readable via API (for instance as CSV). You will be provided with a URL that looks like
https://docs.google.com/spreadsheets/d/e/{some-uid}/pub?gid=0&single=true&output=csv
- Fetch data from that URL from the website (eg with jQuery)
注意:如果您很小心,您可能不必执行第2步,并且可以直接从Steo 1以CSV格式发布工作表:由于这是CSV导出,因此人们无法使用原始电子表格,因此没有安全风险.但是为了安全起见(例如,您可能会错误地发布整个电子表格),我将执行步骤2.
Note: you probably don't have to do step 2 if you're careful, and can publish as CSV your sheet from steo 1 directly: since it's a CSV export, people cannot get to the original spreadsheet so no security risk. But just to be on the safe side (eg you could publish the whole spreadsheet by mistake), I would do step 2.
要使用哪种中间件,可能有无数的选择:)
As for which middleware you want to use, there's probably an infinite number of options :)
这篇关于将观看次数从Google Analytics(分析)添加到网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Middleware: you can use a tool to pull data from the GA API and then query data from that tool. For instance you can: