Accessing Google Analytics API data from javascript without authentication - javascript

I wish to access Google Analytics data through their API using Javascript.
Basically I want to display on a website a list of the most visited pages for the past week/month. I can only use javascript for this.
I found this example on the official site http://code.google.com/p/google-api-javascript-client/wiki/Samples#Google_Analytics_API
Is there any way of accessing Google Analytics data without authentication? Do I need to somehow load a username/password in? To be clear, I want a website (that has Google Analytics tracking) to load analytics data from its own account.
I thought there might be a way to make certain data public, then query that somehow... Or perhaps create a limited user to access it. I can find no examples of either.

You can't access GA withouth authentication for rather the same reason you cannot access you bank account without authentication (because that would allow everybody else to access your account without authentication which might cause some inconvenience).
Google provides a complete example for GA access via Javascript: https://developers.google.com/analytics/solutions/articles/hello-analytics-api. However I don't think that's what you want (I think for that you need to be logged in to Google to actually retrieve data).
You could however use Google Apps Script to pull data into a Google Spreadsheet and embed that into your page (at least I think that should be safe).
Updated: Google has since released the Google Analytics superProxy which allows to share Analytics data with the public safely.

Related

Write to a Google spreadsheet from JavaScript using the Public API access (no OAuth)

I'd like to allow a user to visit one of my sites, enter some information into a field, and then save that information into a Google Spreadsheet via JavaScript.
I don't want the user to login via Google or have to do any special authentication.
It's Ok if the spreadsheet needs to be open to public; the data's not sensitive.
I don't want to use a Google Form, I want to have full control over the client-side UI.
I've been reading through the Google developer docs, but they only make mention to an OAuth login solution. The Google Developer Console allows you to create a "Public API access key", but does not explain how it's used.
All of the examples are for the scenario where the user is using your app to access his own files. In your case, you want the app to access your own file. This isn't easy. The only ways I can see are :-
Use a server app (appengine works well) to do the access
Very carefully set up permissions and store a refresh token in your app.
Option 2 could be your worst security nightmare if you don't do it carefully, and even then may be an infringement of Google T's and C's since it's akin to distributing your password.
Once you figure out the auth, you'll need to check out the spreadsheet API (https://developers.google.com/google-apps/spreadsheets/), as this is the API that allows you to update an existing spreadsheet. The Drive API can only upload an entire new spreadsheet.

Is it possible to show blog view counter with information Google Analytics

I have a static blog using Jekyll hosted on GitHub. I have set up Google Analytics for it and works well enough.
Now I want to show how many people viewed each post in my blog. I found Google Analytics JavaScript API to get the information. But it seems that this API uses OAuth for data access. So I think this might not be the API I needed.
Is it possible to do so with Google Analytics? I don't have any server since it's hosted on GitHub.
I finally solved this problem by Google Analytics superProxy as suggested in the comment of #EikePierstorff.
I wrote a blog on it.
Here's the main idea.
I first build a project on Google App Engile, with which I authenticate for the access of my Google Analytics. Then a URL of query (which can be pageview of certain pages) is generated in JSON format. I can set the refresh rate on this GAE project so that the JSON file can be updated from Google Analytic.
Sounds almost perfect to me. Thank you all guys for help!
You can't query the Google Analytics API without authorization by someone, that's the most important thing to remember.
It's certainly possible to display Google Analytics data on your website to users who don't have access to your account, but in order to do that, someone with access to the account needs to authorize and get an access token in order to run queries.
Normally this is done server side, and once you have a valid access token you can query the API client side (to display charts and graph, etc.). Access tokens are typically valid for 1 hour, so if you want to have your website up all the time, you'll also have to deal with refreshing the access token once it expires.
Now, since you're using Github Pages and don't have a back end, which means all the authorization will need to happen client side. While it's technically possible to do the same thing client side as server side, it's generally not a good idea because private data like your client secret, refresh token, etc. will be visible in the source code.
Applications that do auth client side typically don't authorize on behalf of a user. They require the users themselves to go through an auth flow for security reasons (as I just explained), but that would mean those users 1) have to log in, and 2) can only see the analytics data they have access to, which probably isn't what you want.
--
What you can do is run reports periodically yourself and export that data to a Google Spreadsheet. Google Spreadsheets allow you to embed charts and graphs of data as an <iframe> in external pages, so that might be an option.
At the end of the day, if you can't authorize server side you'll have to come up with some kind of workaround to make this happen.
Here are a few possibly helpful links that might point you in the right direction:
https://developers.google.com/analytics/solutions/google-analytics-spreadsheet-add-on
https://developers.google.com/analytics/devguides/reporting/embed
https://developers.google.com/analytics/solutions/report-automation-magic

Is there a way to use Google Drive with JavaScript without Google Cloud Console?

I'm looking for a way to use Google Drive to share data between a Android app and a web page hosted on my server. The web page should use JavaScript to retrieve and store data in Google Drive.
The data should only be exchanged between the browser of the user and Google Drive.
According to the documentation of accessing Google Drive from JavaScript you must first create a application using the Google Cloud Console, register it and get a client ID before you can use JavaScript to access the data.
I don't want to have to maintain a Google hosted application. Is this possible?
The only point of apis console is to register your applicaiton or in your case script with google. Its baslcy just there way of keeping track of whos accessing what i think. Once you have it registered you normaly would use a clientid and stuff as well as Oauth2 to allow users to access there google drive with your script. But you wont need to go into that becouse its your application thats storing its data there and not a user.
You will probably want to look into a service account.
https://google-developers.appspot.com/drive/service-accounts
If you want to use private spreadsheet then you have to use authentication, in this case google project is easiest one. If you don't want to use private then you can publish your spreadsheet and then make ajax call without authentication/goole project.
You're not the first person we've heard this complaint from. People want to be able to log in and create an API key without having to deal with extraneous cloud services. We're working on some things to make life easier for customers like you.
Unfortunately there is no way to do this at the present time. That said, you don't have to actually maintain a separate application. You can create a project and only use it to manage APIs.

Retrieve google analytics data without authenticating?

I want to retrieve page views for my website using the google api. I need to use the core reporting API to do this:
https://developers.google.com/analytics/devguides/reporting/core/v3/
I want, on page load, to pull down data about page views for my website. At the moment I'm having problems seeing how to achieve this without first needing to manually authenticate the user (via a popup asking for a users google credentials). I don't want this - I simply want to retrieve some data for a given Google analytics profile.
I've generated a client id and an api id and am ready to go but can't find a clear example of how to query the api in the way I need.
Any ideas where in the documentation I can find what I need?

Show GA stats of the website on the website itself

We need to show the number of visits for pages of our website. We already use GA for general reports and we assumed that it might make sense to use GA API to get number of visits data from GA instead of building our own counters and increase load to our database and web server.
I read documentation and already know how to build queries that I need. The main problem I'm trying to work around right now is an authorization process. Sounds like if we use OAuth 2.0 each user has to authenticate before he/she can see the statistics on the page and they will have access to statistics of their websites (if they have them registered with GA). In our case this is incorrect, everything we need is just to show our own GA stats at our own website. Easy task as we thought initially but it doesn't sound like that anymore.
Is there a way to authorize our website to Google (like we send come credentials when we use Maps API) and show stats automatically to any user? We wanted to implement it on client side via JS but if this is possible to implement it on server-side only - that's OK. The target platform for server-side implementation is ASP.NET (just in case). Can someone point us in the right direction?
Quite a few options:
You can use OOCharts and a script like this
Use a service like embedded analytics (paid)
Use SeeTheStats and it's widgets
Use google charts and JavaScript
Use explanium to embed a ga chart
Piwik can embed charts too.

Categories