Robert Pankowecki
Robert Pankowecki

Reputation: 725

Using Google Analytics to show subset of data for customers of web application using embed api

I'm developing an application where each 'business' has its own page (or rather many pages):

For example example.com/business/abc/

So, for the logged in business owners in the system I would like to give a feature 'View page analytics'. It would display how many visits (and maybe a couple of other things) that particular page has had.

Is there a way of doing this using the Google Analytics API with my constraints:

I've been researching this topic for hours trying to come up with a solution and can't figure out anything.

Here is what I tried and what problems happened to me:

gapi.analytics.auth.authorize({
  container: 'auth',
  clientid: 'xxx.apps.googleusercontent.com',
    serverAuth: {
      access_token: 'Server side generated token'
    }
});

instead of

gapi.analytics.auth.authorize({
  container: 'auth',
  clientid: 'xxx.apps.googleusercontent.com',
});

The effects are not quite what I expected. The example doesn't show anymore (I can't see my data) but I can see in Netowrking section in Chrome that it is actually receiving real data from GA. But for unknown reason, nothing is appearing.

What I try to avoid is building a solution in which I need to build server side code that is querying GA for data, providing it to the frontend and then JS is responsible for displaying it. I would rather use Embed API but it seems not to be well suited for the use case where I don't want users to play with their UA data but rather with my own UA data limited to some scope. I would like to have at least the frontend or backend part of the solution solved. The solution doesn't need to be even Google Analytics based. Anything else that would let me achieve the use case easily and let the business owners see the effects of their marketing (traffic, sales) would be interesting as well.

Related:

TLDR: Displaying subset of my GA data to my customers without forcing them to become GA users and adding them to my GA account.

Any help appreciated!

Upvotes: 13

Views: 3753

Answers (2)

MarkeD
MarkeD

Reputation: 2631

I think you need the Google Analytics Super Proxy

You download the github package and upload to your own App Engine project, do some minimal configuration and then you have an interface where you can setup Google Analytics API calls which require no user login.

It provides end user URLs that you can use to construct data tables in your front end, it also provides data-table format so it slots right into Google Charts.

So for example, you have a user that needs access to visits, revenue for site section /sectionA/

You set up the GA super proxy to serve them a URL that only includes data for that section - you can try out queries here in the GA query explorer. In this case, metrics=ga:visits,ga:productRevenue and filter~=ga:page=/sectionA/

This produces an end URL with JSON data, that refreshes daily/hourly - your choice. You import this URL into your app.

The end user then logs in to your app, and sees the chart data generated from the end URL for their login. They don't need to know about GA super proxy, they just see the end resulting chart.

You could get more sophisticated by providing dropdowns to select which data chart they see, which changes the GA super proxy URL that is requested.

Upvotes: 1

Philip Walton
Philip Walton

Reputation: 30431

Without seeing your code it's hard to know where the problem is, but using the serverAuth option definitely works. And when using the serverAuth option, you don't need to specify a client ID or container, all you need to enter is the following:

gapi.analytics.auth.authorize({
  serverAuth: {
    access_token: 'Server side generated token'
  }
});

Here's an example that will work if you enter in a valid access token and the idsfor a view to which you have access:

http://jsbin.com/vukezoheyeco/3/edit

Note: when doing auth like this, it happens sync. This can be a gotcha if you're used to an async auth flow (like normal) and you add an event handler listening for the "success" event after calling .authorize because then your handler will never run.

Upvotes: 3

Related Questions