最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing Google Analytics Data With API Key Only - Stack Overflow

programmeradmin2浏览0评论

I'm trying to use Google Analytics API in Javascript. I want to:

  • Query Data for a specific website (under my control)
  • Use the data to build an open-to-the-public dashboard

Every example I can find requires you to use OAuth to authenticate before you can query data. Because I only want to query (not delete/modify), is there a way to use a simple API Key to grab the data I need?

I'm trying to use Google Analytics API in Javascript. I want to:

  • Query Data for a specific website (under my control)
  • Use the data to build an open-to-the-public dashboard

Every example I can find requires you to use OAuth to authenticate before you can query data. Because I only want to query (not delete/modify), is there a way to use a simple API Key to grab the data I need?

Share Improve this question asked Nov 6, 2015 at 20:00 Daniel BrownDaniel Brown 3,0625 gold badges32 silver badges45 bronze badges 2
  • Ended up turning the answer/result of this into a blog post: dannyrb./posts/… – Daniel Brown Commented Jan 12, 2016 at 16:14
  • 1 dannyrb./2015/11/10/… – fabOnReact Commented Jun 25, 2018 at 11:00
Add a ment  | 

1 Answer 1

Reset to default 6

Yes and No. The Google Analytics Core Reporting API is an Authenticated API and requires the use of OAuth2, authorized with the appropriate scope:

https://www.googleapis./auth/analytics.readonly

The API key simply identifies your Google Project, it does not necessarily grant you authorization to read the data of your Google Analytics Accounts, even if that account is associated with the same Google Login. The API key is usually public and lives on the client browsers, it would not be secure to grant access to anyone's Google Analytics Account data simply if they had access to a particular key.

What you are going to need to do is use a service account to access the data. Take a look at the Google Analytics Demos and Tools site. There they have a really good example example of using a service account server side to generate an access token and then has the JavaScript Client library make the final request. Alternatively you could build something akin to the Google Analytics Super Proxy which can query the API on your behalf and generates a public URL which can then be queried separately.

发布评论

评论列表(0)

  1. 暂无评论