I am currently new for Google analytics API throu Javascript.
I am implementing some of charts and for first load, page is redirecting me to google site to login with Analytics account and then returning to my orignal page, refresh & chart show.
Now, instead of redirecting to google analytics, is there any other way that I can store credentials on either in code behind or in config file and it will be read from there when page get loaded?
Let me know if there are any other ways/ ideas around?
I can give you some help.
Initially we need to authenticate the users, For that purpose we are redirecting the user to Google.[Mostly using Oauth]
After the authentication done. Google will redirect to your site with the access Token.
Access token is the key to fetch the analytics data from Google's database by using Management API and core reporting API.
You have to do some exchanges with google to make the token as permanent and you can store it your database.But based on some conditons this token may expire.
Visit this url : https://developers.google.com/analytics/devguides/ and understand the Google Analytics, Core Reporting API and Oauth Process
Down load this useful PHP client library for Google Analytics https://code.google.com/p/google-api-php-client/ and have fun.
Related
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.
Apps Script keeps asking for offline permission with Youtube's API
In Google Apps Script, access to YouTube Reporting API is Forbidden
These are very similar questions however, I am logged in as the owner. The script is in the owner's drive.
My problem
I have been trying to access YouTube Analytics Reports from the Content Owner using GAS(Google Apps Script). There are two accounts a Brand account (holds the data i.e. the video content) and a User account. They both use the same credentials(email & pwd). When authorizing as the user, I get a 403(forbidden). When authorizing as the brand, I am asked to authorize offline access again and again, stopping me from ever being able to make a request.
What I've tried...
Running from Node, using OAUTH Playground tokens, and it works. But I need to stick with GAS.
Using google-script-oauth2: to hard code credentials (similarly to the Node solution) but I couldn't set the refresh token. I got a 401(invalid credentials) - I believe the bearer token was expired
Setting up credentials for and enabling both youtube and youtube analytics APIs
scrapping old credentials and attaching a new project to GAS
removing auth in privacy settings and re-authenticating
toggling a use unsafe scripts option and repeating step 3
switching between contentOwner and channel parameters for the ids param
NOTE: I have two OAUTH2 credentials app script and the web client both are set as Web Applications.
GAS Code
function test(){
YouTubeAnalytics.Reports.query("contentOwner==id", "2017-03-01", "2017-03-31", "likes");
}
Final Thoughts
I believe there is a disconnect between the Brand and the user such that the brand is not given the same access to the OAUTH 2 token. Therefore, it never generates a refresh token. So, the request sees that I am the authorized channel owner but it can't finish the OAUTH flow.
If this is true, why, and how do I fix it? If not, what do you think could be the error?
I just realized the script is in the user account and not the brand account. Would that affect the validation?
Also on the actual channel, the user is listed as the Primary Owner.
I think the problem has to do with Apps Script's limited OAuth scopes. Open the Script Editor and navigate to Resources->Advanced Google Services and you'll see that YouTube's Reporting API is not supported. However, you can get around this by using Service Accounts. They are a pain to set up but once you get them up and running you can explicitly request the scopes required to make authorized calls on the API from GAS.
Here are a few links to get you started:
Using OAuth 2.0 for Server to Server Applications
OAuth 2.0 Scopes for Google APIs
For Using OAuth 2.0 for Server to Server Applications you'll want to scroll down to the section titled Preparing to make an authorized API call. Select the HTTP/REST tab and it should walk you thru the steps you'll need to setup a service account (you'll need to create a JWT - JSON Web Token from your Google console for use in your API calls). You'll also need to retrieve the needed OAuth scope urls for YouTube's Reporting API from the second link provided. Once you're all set up you'll be able to make requests to the API directly from Apps Script using URLFetchApp. Best of luck with your project.
One more thing. Check out Google's API Explorer listing for the YouTube Reporting API. You can get the URL endpoints (and request methods) you'll need to use in your UrlFetchApp calls by playing with the Explorer.
UPDATE 4/28/2017
Also be sure to enable the YouTube Reporting API from your Google API Console.
I am using Googe Analytics API. I had successfully configured google analytics api and get successful data but issue is that when I use any other email account to login and view the page, it says authentication issue.
I want to access analytics api without gmail login. I will hard code credentials to for login, but how to do it with javaScript?
How to get analytics data without login in api?
So if I am correct, you want to retrieve data from a Google specific account without having to log in or sign in that account.
You could use tokens to connect the API rather than manually log in (if that is what you are referring). Other options could be extracting the data into a virtual repository that you can use for presentation layer on the client side so you don't have to deal with credentials or security flaws on having to log in or hard code the authentication.
This should be do-able on javascript and jquery to make your data viewable.
To query GA without authenticating each time you will need obtain and store the refresh token while authenticating GA.
(not sure if we can retrieve refresh token with Js)
Once we have refresh token we can query API for access token again.
I'm working on a page that will be available to certain users where they can see shared Google Analytics data. I want to pull the data with JavaScript and I've succeeded in getting an OAuth token for the account with the GA data, but it expires in 1 hour.
How can I use this token for all users coming to the page? I don't need the users to be able to access their own GA data, just the shared data. Is there a way to make the token permanent or to refresh it?
Below I have listed three different possible solutions:
The easiest method might be to grant permission for your desired users to have read access to the view (profile) you want to share and use the embed API to authenticate those users and display that data.
You could look into using the Google Analytics Super proxy. The Google Analytics superProxy allows you to publicly share your Google Analytics reporting data. You can use it to power your own custom dashboards and widgets, transform responses to various formats, test, and much more.
Or you could look into using an authenticated service account. The Google OAuth 2.0 system supports server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved.
Is it possible to present the Google Picker to a user who isn't logged in with her Google account? I would like to let the user select files from a shared Google Drive of the Google account of my website. I can access these files with OAuth on the server. But is it also possible to present the files with Google Picker? Maybe with this method:
PickerBuilder.setOAuthToken(string)
Sets an OAuth token to use for authenticating the current user.
Depending on the scope of the token, only certain views will display
data. Valid scopes are Google Docs, Drive, and Photos. This method
should be used instead of .setAuthUser when a user is authenticated
with OAuth.
Google Picker API reference
I have tried this and I think it is not possible. Or is it?
This method of the API does work (I use it currently). Just note that the auth token from google expires and you may need to refresh your token otherwise the google picker will return a 401 when trying to retrieve the list of files from google drive.
PickerBuilder.setOAuthToken(string)