Google Analytics API (Real-time) in Google Sheets - google-analytics

I'm trying to access the Google Analytics API via Google Sheets (Google Script).
So within the script, I'm trying to use the URI as described here: https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get
The API call looks something like this:
GET https://www.googleapis.com/analytics/v3/data/realtime?ids=3254435&metrics=ga%3AActiveUsers&key={YOUR_API_KEY}
Within my Google Analytics Script, I've also created a Public API Access key:
However, I can't seem to get any data whether if I'm appending the token key in the URI or within the header.
Can any one point me to the right direction?
Thanks

The public api key is for public access APIs. Data that is not owned by a user. You need to create client id from web application.
Then you will be able to authenticate it using Oauth2.
this might get you started. https://developers.google.com/apps-script/advanced/analytics
How to know when you need to be authenticated:
Public API: Aanalytics metadata API meta data doesn't belong to any user its public data anyone can access.
User data API: Google Analytics Real-time Data the data returned is owned by a user, it is not public data viewable by everyone. Documentation states
Requires authorization
If the documentation says requires authentication then you have to be authenticated to access that call.

You can use Google Analytics API easily just by enabling them in App Script.
Below is the Guide to using Real-time API in Google Spreadsheet
https://www.tatvic.com/blog/google-analytics-real-time-data-in-google-sheet/
Just like above you can use any API no need to take care of tokens.

Related

Get access to clients data using API

Is it possible to make a platform where client can sign up and log in using their Google accounts, and accept that their Google Analytics data will be used for calculations.
They should accept the use only once, and then the program should download and process the Google Analytics data every day.
Please point me in the correct direction for documentation
Yes, you need to create a project in Google Cloud Platform, then go to : API and Services > Library > Google Analytics API. (This is to get analytics from an user). Then you need to create a project inside Firebase, allowing email authentication and you will be able to do your task.

Is it possible to create Service account and get p12 key programmatically for google accounts?

I need to extract Google Analytics data through it's API. However I do not want to generate any account and key manually (the reason is I do not want to let this technical this to be performed by any non-techie person) and want to dump that data for analysis purpose.
In order to use any of Googles APIs they need to know who you are. You as a developer are identified by the project you create on Google Developer console. Once the project is created you can create a number of different types of credentials including Service account credentials and Oauth2 Credentials.
Answer: No there is no way to programmaticlly create service account credentials. Nor is it possible to use the a Google api without first creating a project on Google developer console.
Note: Google Cloud Resource Manager does exist but is very limited in what it allows you to do.
If someone else is going to be running your application to access their Google analytics Data you should idealy be using Oauth2 in your application and not a service account.
Service accounts are designed as way for the developer to grant others access to data owned by the developer in question. They are not really intended for novice users to be creating their own projects on google developer console and generating credentials.
You are basically asking if you can obtain the credentials necessary to query the API via the API. This does not work.
I am also not sure how this would help you. Before you can do any API operations to a Google Analytics account you need to have an authorized user (e.g. a service account) added to your view or property. If you are in a situation where you can add a user programmatically to an account you might as well just read the data you need.

Get Google Analytics Data without Client Secrets

I am looking to make a web based app which creates a monthly report that has Google Analytics information in i.e. organic searches etc. Some clients will have their own Google Analytics accounts and wont be own mine.
I managed to get the OAuth 2 authorization working with my account however it wont work for clients as I needed to get the client_secret.json file from Google Developers website. I have seen a service like cyfe.com which just gets the google login information and outs the Google Analytics information in graphs and such.
Does anyone know how they manage this is possible without getting the client_secret.json file ?
oAuth2 is designed to let you access to users data with their permission, without requiring to access their password.
You should use your own client Id and client secret, these identify your application, not your users ("client" here means client of the API).
You then need to create an interface where users can allow your application to access to their data, from this you'll get an access token, allowing to your application to access to Google Analytics data of your user on their behalf.
Read "Using OAuth 2.0 to Access Google APIs", and identify the scenario that matches to what you want to achieve.
I strongly suggest that you use a library managing the authentication flow, you don't need to reinvent the wheel. Here's a list from Google: Google API Client Libraries.

Accessing Google analytics api using api key

I'm trying to implement Google analytics API using API key to make it available without authorization. But i can get examples using CLIENT ID in google developer console itself. Can anyone help me with an example using api key?
It is not possible to access Google Analytics API with the API key. You must use Open Authentication.
If you are trying to access your own data you can use a service account. Create new credentials choose Service Account.
You can then take the service account email address:
1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp#developer.gserviceaccount.com
Add it as a user at the ACCOUNT level it is very important that it be at the ACCOUNT level it wont work other wise. Your application with then be able to access your Google Analytics data with out a log in.
Without knowing what language you are working with I cant give you any examples.
Update:
If as you say you are planning on doing this with JavaScript then you will have to go with Oauth2 and request access. There is no way to use a Service account with JavaScript.
There for I strongly recommend that you find a server sided programing language to do this in. Even if it did work with JavaScript you would end up running out of quota on the API before to long.
Google Analytics' EmbedAPI will allow you to display your Google Analytics via javascript, and users will be able to log in on page.
You will still need to get your clientAPI, but then users will be able to login independently.
The javascript code is described in the dev guide and a sample is below:
<script>
gapi.analytics.ready(function() {
// Step 3: Authorize the user.
var CLIENT_ID = 'Insert your client ID here';
gapi.analytics.auth.authorize({
container: 'auth-button',
clientid: CLIENT_ID,
});
</script>
The API limit should be no problem, its 50,000 calls per day.

How can I generate a GA tracking code programmatically?

How to generate Google Analytics tracking code programmatic-ally?
I am new to Google Analytics API, can someone please help
It is NOT possible to create new Google Analytics accounts or profiles programmatically using the existing APIs.
There are two APIs available, the Data Export API and the Management API. However, they are bot read-only.
With the Google Analytics Data Export API, you can develop client applications to request data from an existing Analytics profile for an authorized user, and refine the results of the request using query parameters. Currently, the Data Export API supports read-only access to your Google Analytics data.
Currently, the Management API supports read-only access to five components of the Google Analytics Management system: Account data,Profile data, Web property data, Goal data, Advanced segments
Seems like it's finally possible in 2021 with alpha api.
https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/create#authorization-scopes
It's not currently possible to create Google Analytics accounts automatically at the moment but generating the tracking code is possible.
The only real variation between the tracking code generated each time you create a profile for a new domain is the web property ID. This is in the format UA-XXXXXXX-X. Each X is a number - the first 7 numbers are the account number and the number after the last dash relates to each web property set up for the account.
You can use the accounts feed of the API to get a list of accounts and their profiles. If an account only has one profile then it's possible to use the web property ID of it to automatically generate the tracking code. However, if there's multiple web properties then you would need a way to decide which one to use (present the user with a selection list etc).

Resources