无需身份验证即可从 javascript 访问 Google Analytics API 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14729850/
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-26 22:26:01  来源:igfitidea点击:

Accessing Google Analytics API data from javascript without authentication

javascriptgoogle-analyticsgoogle-analytics-api

提问by martinwnet

I wish to access Google Analytics data through their API using Javascript. Basically I want to display on a website a list of the most visited pages for the past week/month. I can only use javascript for this.

我希望使用 Javascript 通过他们的 API 访问 Google Analytics 数据。基本上我想在网站上显示过去一周/一个月访问量最大的页面列表。我只能为此使用javascript。

I found this example on the official site http://code.google.com/p/google-api-javascript-client/wiki/Samples#Google_Analytics_API

我在官方网站http://code.google.com/p/google-api-javascript-client/wiki/Samples#Google_Analytics_API上找到了这个例子

Is there any way of accessing Google Analytics data without authentication? Do I need to somehow load a username/password in? To be clear, I want a website (that has Google Analytics tracking) to load analytics data from its own account.

有没有办法在没有身份验证的情况下访问 Google Analytics 数据?我需要以某种方式加载用户名/密码吗?明确地说,我想要一个网站(具有 Google Analytics 跟踪)从其自己的帐户加载分析数据。

I thought there might be a way to make certain data public, then query that somehow... Or perhaps create a limited user to access it. I can find no examples of either.

我认为可能有一种方法可以公开某些数据,然后以某种方式查询......或者可能创建一个有限的用户来访问它。我找不到任何例子。

回答by Eike Pierstorff

You can't access GA withouth authentication for rather the same reason you cannot access you bank account without authentication (because that would allow everybody else to access your account without authentication which might cause some inconvenience).

您不能在没有身份验证的情况下访问 GA,原因与您在没有身份验证的情况下无法访问您的银行帐户的原因相同(因为这将允许其他所有人在没有身份验证的情况下访问您的帐户,这可能会造成一些不便)。

Google provides a complete example for GA access via Javascript: https://developers.google.com/analytics/solutions/articles/hello-analytics-api. However I don't think that's what you want (I think for that you need to be logged in to Google to actually retrieve data).

Google 提供了通过 Javascript 访问 GA 的完整示例:https: //developers.google.com/analytics/solutions/articles/hello-analytics-api。但是,我认为这不是您想要的(我认为您需要登录 Google 才能实际检索数据)。

You could however use Google Apps Script to pull data into a Google Spreadsheet and embed that into your page (at least I think that should be safe).

但是,您可以使用 Google Apps Script 将数据提取到 Google 电子表格中并将其嵌入到您的页面中(至少我认为这应该是安全的)。

Updated: Google has since released the Google Analytics superProxywhich allows to share Analytics data with the public safely.

更新:谷歌此后发布了谷歌分析超级代理,允许与公众安全地共享分析数据。