Access Google Analytics API via javascript with service account - javascript

Title pretty much says it all - I need to access Analytics data for a site, using the Javascript API and authenticating via a service account. Is this possible, if so, how? Have spent hours running in circles in the Google docs to no avail.
I'm looking to build a dashboard to publically display stats for the site, hence the need for the service account.
Javascript is a must - I need a completely client-side solution.
Any ideas?

Related

React : Get data from Google Analytics API

I'm creating an application using React and Laravel.
I integrated successfuly Google Analytics using this react-ga so when I check my Analytics dashboard the statistics of the pages of my project are there.
Now I want to display those data in my application to the users.
I've searched alot and I found that I need to use Embed API. The problem here is that it request an authentification each time a user want to access to the data. Searched some more and the solution was apparently to setup a service account as a user in Google Analytics.
Now that I have the account set, the API key generated, I don't know how to call the data from the server to my application.
There is this guide for javascript but I had a hard time implementing in React.

How a list of all a users Google analytics views

I have many Analytics properties and accounts.
I am now working on JavaScript to get the reports from the Google Analytics API.
In order to make these requests I need the view idea for the account i wish to access so that i can load the related data from the backed.
The only way i have found to find these view ids is to use the Official GA Query Explorer every time to get the id by selecting Account and Property.
This is quite annoying. Because it involves human action.
Is there any way I can look up the id by the UA-xxxx string?
If you are already using the Google Analtyics API to get the reports why not use the Management APi to get a list of all the accounts the user has access to
Account summeries.list will give you that.
I am not a Javascript developer but the code should be something like this.
function queryAccounts() {
// Load the Google Analytics client library.
gapi.client.load('analytics', 'v3').then(function() {
// Get a list of all Google Analytics accounts for this user
gapi.client.analytics.management.accountSummaries.list().then(handleAccounts);
});
}
Code shamelessly ripped from Hello Analytics API: JavaScript quickstart for web applications and altered a bit using a wild guess.

Seeking decent documentation on creating a way to link a Google account with my app

I'm trying to develop a react native application that won't require a user to sign into a bunch of different services every time. For now I'm trying to get the google side of things setup where a user can click a button which will allow me to link my application to there Google user account so that when they next visit the app the don't need to log into google for the functionality to continue to work.
I'm having a hard time finding documentation about how this link can be set up but I have found this page on Google which suggests it's possible.
https://myaccount.google.com/accountlinking?hl=en-GB&pli=1
The idea would be a bit like last.fm handles Spotify. a simple login and approve the service will mean that last.fm can listen to the Spotify account without requiring further auth every time its doing said functionality.
I can't find much in terms of tutorials or documentation on this specific thing.
Google OAuth and Scopes
It sounds like you're looking to implement Google identity federation in your app - specifically, OAuth 2.0. Google gives you quite a few options depending on the complexity of your authenticated user experience.
As for permissions, the Google API documentation calls these scopes. Here's a list of all the available scopes for every Google API. Setting scopes can take a few additional steps depending on which Google apps/information your app needs access to. By default, the Google API scopes for a new project are email, profile, and openid. Here's a video explaining how to view and modify the scopes in the Google API console(mentioned below).
1. Google Sign-in Button with scopes
The simplest method would be to follow this guide from Google which explains how to set up Google Auth on the frontend.
In short, you first set up a project within the Google API Console. Create a new project and take a look at your project scopes by clicking the Credentials tab, then the OAuth Consent Screen tab. Then back in your frontend code, include a script tag to call the Google API related to authentication functionality. Next, include a meta tag containing the client key found in the Google API Console. Then just create a sign in button with a certain class and data attribute(mentioned in the guide) and users should be able to sign in. This will return a small amount of user data in your code which you can use for validation within your app.
2. Firebase with scopes
A more complex solution would be Firebase authentication which returns even more user data, the use of a database to save and retrieve data related to the user and their session, and many other handy features that would normally be time consuming to develop. As such, Firebase is often called a backend as a service(BaaS).
To get the same level of granularity of scopes as the standard OAuth scenario outlined above, you may need to use a combination of the two as described in this article from Fireship.io.

Is it possible to grant a JavaScript app, r/w permission to a Google Sheets file, without the need for the app user to log in/have a Google account?

I have a Google Spreadsheet file and a JavaScript application, hosted using Google Firebase free plan – if that changes anything. I would like to grant my application a read/write permission to the file so that when a user, posts something using my app, it will be written to the SpreadSheet as a new line. I thought that should be a simple scenario but…
While I got the read part running, using the API_KEY, the writing part seems to require OAuth authentication - even if the file itself is 100% public…
In my scenario the application itself should be the one, authenticating to the Sheet API - not the user of that application.
So, is there any way around this? Or to somehow log-in using OAuth on the application level?
Please note: I know I could use Firebase here, but let’s just assume that for now, it is not an option.
My final solution was to use Google Apps Script instead of Google Sheets API, as described here:
https://stackoverflow.com/a/49459945/2384366
After publishing the scrip with the ‘Who has access to the app’ = ‘Anyone, even anonymous’ – I was able to use the doPost function to do exactly what I wanted.
Perhaps it’s not the best solutions – but it works (and frankly, gives us much more control).

Automatically access my Google Analytics and display chart publicly on my website

I'm having some difficulties in figuring out the best way to do this:
Using Google Analytics API, or similar Google API, I would like to track a user's activity from the moment they access the page until they reach an end page, which is gonna show them back some charts with THEIR activity on my website. (Nothing too detailed, just how long they've been on each page, how many session etc.)
So far, I've managed follow the Embed API example to access THE USER's Google Analytics account and draw a chart by asking for permission, however when it comes to showing data from MY account I just can't seem to figure it out.
I want my website to automatically use my account (or service account) and draw some charts from my google analytics data and show it to every user.
What would be the best way to approach this? I've read something about access tokens but I don't know if that's the solution. Moreover, my hosting is a shared host and I don't think it allows installing Python Modules like in this example.
Cheers for the help!
If you want to show the user your data, you will have to perform the authentication on the server side. There is no way around this. It is after all, your account's data that they are accessing.
If you are unable to install Google's client library, you need to:
Get an access token using cURL (see how here)
Use that access token to perform server side authentication for the user (see how here)
The user should now be able to access your site without logging in, and see YOUR data.

Categories

Resources