13

I'm developing an application where each 'business' has it's 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 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 don't want customers to provide their own UA code
  • I don't want them to require to have GA account
  • Customers don't need to have Google email account
  • I don't want to build entire frontend and backend myself. I would rather use something existing

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:

  • http://ga-dev-tools.appspot.com/demos/embed-api/
    • This is basically exactly what I want for my customers to be displayed on my site (like in the examples), except that Embed Api tries to authorize users to their own (owned) google analytics. I want it instead to use my own Google Analytics data (or rather part of it)
    • They way I thought about limiting data access would be for every one of my customer to create a View in GA, Add filter to that View so only customer pages are listed there, assign User to the view, and use the Embed Api to display data from that View only. There are a couple of problems with that:
      • To assign User to View we need email address. And this must be either google account email, or account from a project created with Google Developers Console (application).
      • In other words I can't create (in any way that I know) and account that would be a shield account for my customers to subset of my GA data that they would be interested in. It must be either a real user or real application email address.
      • So what I tried to do is... I created an app in Google Developers Console, Created new OAuth Service Account. Using Ruby code (that in production app would be running on backend) I obtained OAuth token. I added this email of my OAuth service account as a User to the View
      • I wanted this server side generated oauth token to be used by Embed Api. That would achieve the effect that I generate the token for on my backend and user can use it without having GA user in my GA property. So I changed according to documentation the basic Embed Api example to use
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 responsbile for displaying it. I would rather use Embed API but it seems not be well suited for the usecase 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 usecase easily and let the business owners see the effects of their marketing (traffic, sales) would be interesting as well.

Related:

  • stackoverflow .com/questions/13514775/using-google-analytics-api-to-show-subset-of-data-for-customers-of-web-applicati
  • stackoverflow .com/questions/3994708/google-analytics-customer-data
  • stackoverflow .com/questions/4245132/google-analytics-api-filter-by-uri
  • embeddedanalytics .com seems like something that could be useful, but their page and graphs looks like from few years ago. I would like something more pretty.
  • oocharts .com seems to be interesting because of what their docs.oocharts .com says about queries. But they don't charge anything for their product so I am skeptical of their business model and whether it is a good longterm solution.
  • I don't have enough karma to post links ;)

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!

  • This question is really to big but. If you record all the data to one Google Analytics account there is a limit of 10k requests you can make against the GA API per day per view you cant extend that. Embeded API uses Javascript you cant use a service account with Javascript. – DaImTo Sep 17 '14 at 18:41
  • Using the Embed API with the `serverAuth` param is the recommended way to do exactly what you want. I don't know why it's not working for you. Can you elaborate on the problems you're having or [file an issue](https://code.google.com/p/analytics-issues/issues/entry?template=Embed%20API%20-%20Defect) for this? – Philip Walton Sep 17 '14 at 20:32
  • @PhilipWalton Thank you for reassuring me that it is indeed a right way to approach this kind of problems. I submitted a bug https://code.google.com/p/analytics-issues/issues/detail?id=496 as you asked for. – Robert Pankowecki Sep 18 '14 at 08:00

2 Answers2

3

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.

Philip Walton
  • 26,214
  • 15
  • 55
  • 81
1

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.

MarkeD
  • 2,079
  • 2
  • 14
  • 31
  • I don't think it would solve my problem. I don't see any authentication/authorization built in and any filtering that could be set up. That means my customers could edit the frontend JS (or issue HTTP queries directly to GASP) and obtain analytics data about pages maintained by other customers. – Robert Pankowecki Nov 02 '14 at 09:49
  • You can pre-authenticate before time for your admin GA account, then build the URLs with the filters needed. Then your app serves up only the correct JSON URLs with the data needed as the end user requests it, perhaps prompted by a dropdown or similar. – MarkeD Nov 04 '14 at 13:48