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

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.

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.

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.

Is it possible to integrate google analytics dashboard to our own website?

I want to show google analytics dashboard to my users. It is not possible to give access of "google analytics dashboard" to all users. So I want to integrate it on my own website. Will it be possible to integrate google analytics dashboard on a particular website?
This question gets asked quite a bit. It is important to understand the security model for the API. All Google APIs use OAuth2.0 to authenticate the user. This is designed to make it easy to use the API to access the Users's data on behalf of the user. In contrast you will need to do work to show Your data to your users.
With that said, an example of what you are looking for is the Server side Auth example provided by the Analytics API team.
The simplest answer is to query the API on the server side, with a service account and serve that data as you serve any other data to your users. Read the scenarios in detail, and pick one that best suits your end goals. Understand how the API views the following entities.
A User uses an application.
A Developer develops Application.
An Application has a project id and its associated credentials.
A user authorizes an application to access their data via the API.
You are breaking this paradigm because you are the user and developer from the API's perspective. You want to share your data with people who are not authorized to view it, so you will need to use a service account to access that data and share it directly with your users.
Also remember to add the service account to Analytics View you wish it to access.
Check out EmbeddedAnalytics if you prefer not to get bogged down learning oAuth2, the CoreReporting API, and lastly a charting tool. We simplify the process. All you do is create your chart and embed a snippet of code in your site where you want the chart/dashboard to show.

Allowing user access to analytics

I would like to enable (some) of my users to view my website's analytics data.
Our website has a bunch of groups where users can add content. I would like to enable certain users (group admins), to view usage statistics for that specific group. Ofcourse the data should only be accessable to those users with the VIEW_ANALYTICS right, not to any others.
Is there any way to do this in Google Analytics, or is there some other platform I could use to easily achieve this goal (we use AWS for our hosting)?
Regards,
klmdb
The Google Embed API allows you to display analytics data in your own pages. Pages that are only accessible to the members of your VIEW_ANALYTICS group.
You can create a service account and give it the appropriate access in the analytics user management for the property.
You also authorize the service account to use the embed API in the developers API console. (I don't have enough points to include more than two links in an answer.) In the API console you can generate a private key (p12 or json format) which you then use on your server to authenticate the request for analytics data.
You don't say which technologies you are using, but they show Java and Python examples of how to set up the authentication by the Service Account. I've also seen some node.js and php libraries.
This way you don't have to manage who has access through the google anayltics admin system, you just need to control who has access to your "dashboard" page.
I have done this all with client-side javascript and it works. Of course the client-side nature means that anybody who knows how to use browser debug tools can get our private-key, but in our particular case I'm not worried about it. On the other had it seem like you need to keep some security around this so I wouldn't recommend the client-side solution.

Google Analytics - Access custom segments through Service Account

My application needs to fetch Google Analytics data from backend servers.
I am using Google Service Account in my application.
Though I have given all permissions to my service account through Google Developer console, it turns out that my service account cannot access custom segments of my user account.
A way could have been to copy these segments from user account to service account, but there seems like no way to login via service account and be able to copy segments from user account to service account.
So, there seems like no way to be able to fetch custom segments in a web application from backend...
Is there any way to do so?
Can any other account(web application, Installed application account) serve my purpose?
Reading the documentation of above two Google accounts reveals that user would be prompted for authentication (at least once) during authorization in these 2 accounts which is not feasible in backend servers.
I found a similar thread for my query Google Analytics V3 - How to create custom segments for API Service Accounts , but user in this thread resorted to not using segments at all, but I do need to use them in my account.
PS: I am writing my application in RubyOnRails and would be using Ruby Gem for Google Authentication
Answer: No there is no way to add segments to the Service account. Even the Mangment api only allows you to list segments not add them. Segment Management API documentation
An Idea: You could us normal Oauth2 Authenticate it using your account, Save the refresh token someplace and use that in your application to see the data. Then it would be like the Application is always you and would be able to see your segments.
Sorry I don't know ruby so I cant help you with getting the idea working.

Resources