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
1 Answer
Reset to default 6Yes 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.