Google analytics credentials at client side - javascript

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.
Here, I would like to avoid users to redirect to google analytics page and instead of that I want to stored credentials somewhere in config file or in application variable.
I tried to search and read many blogs & articles however no success.
Is there any trick/ ideas by which I can get any API which could resolve this issue?

Google doesn't allow you to pull the physical charts onto your site, since they are generated on the fly. What you can do, is grab the data from their API and then display the data as needed on your site. I have searched and found a few examples with this one looking like it might do the trick.
It goes through the steps of pulling the data through the API, displaying/formatting/styling the data, and adding the chart to the page. Try that, Embed Analytics Using the Google Analytics API.

the reason why you are redirected to a Google login page, is that Google connects to the analytics data for whichever user you log in as. This means each user that logs into the site would see their own Analytics info, not the info from a static account.
If you want it to be the same user's analytics data no matter who is logging in, you would need to setup a "Service Account" on Google's API Console (https://code.google.com/apis/console/), then use those credentials when logging in.
It seems like a lot of people are looking for a solution to this, so I created a nuGet package that:
- connects to a "Service Account" you set up in the API Console
- Pulls any Google Analytics data you would like
- Displays that data using Google's Charts API
and it does all of this in a very easy to modify way. You can see more here: https://www.nuget.org/packages/GoogleAnalytics.GoogleCharts.NET/.

Related

Get all pages API

I am working on a project that we need to retrieve all the unique visitors for each page in Google Analytics.
It is possible to see all the pages in one website and get the information for each page in the Google Analytics. However I could not find the Rest API for get all the pages or visitors for each page.
The final goal is that we need to have a drop box which shows all the pages/urls and after we select one of them it shows the number of the visitors.
You can use the Google Analytics API to extract the data on all the pages. You should start by checking out the Page Tracking dimensions.
Once you have the page you want you can filter additional requests to get additional data for each of the pages.
Tip: Look into the Google javascript client library.

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

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?

Accessing Google Analytics API data from javascript without authentication

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.

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