Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful API and Google Analytics

I'm running an RESTful API with Python (Flask).

I want to be able to track:

  • which requests have been made
  • when did those requests happen
  • how long did it take to send the response

I want to use Google Analytics for this, because of it's nice dashboard and extended functionalities.

My question

How can I implement Google Analytics into a REST API?
OR does anyone know another tool/library that can be implemented?

This is what I found at the moment:

  • a tracking app that uses MongoDB
  • the Google data API - but this is for reading GA data, not tracking the API?
like image 243
tdhulster Avatar asked Mar 20 '13 17:03

tdhulster


2 Answers

I know this is very old post. I came across google analytics support in Python

https://developers.google.com/api-client-library/python/apis/analytics/v3

Thought this is right place to document as well (y)

like image 97
Ciasto piekarz Avatar answered Oct 02 '22 16:10

Ciasto piekarz


There are actually two ways to send server-side data to Google Analytics. The standard method is the GIF Image Request API, which is the same API that ga.js uses on the client-side. Google has started developing a new REST API known as the Measurement Protocol, but this is only in developer preview.

Server-Side GA

There are a few issues to work through when trying to send server-side data to GA.

Like @mehaase pointed out above, the gif API takes the ip address from the request, so all of your server-side requests will appear as users coming from the location of your servers. The measurement protocol doesn't let you change the request's ip either. I'll assume the publicly available gif API in this answer.

Another issue is that the gif endpoint requires a client-side cookie. You can fake this cookie on every request but this will cause each event to look like a new visitor. That's fine as long as you keep the server-side API and website in separate Google Analytics profiles.

Also beware that Google can take up to an hour to show your events once you've sent them. This can make debugging a bit painful, so be patient.

Here's the breakdown of what each variable in the GA cookie means, and a good node.js example of sending server-side data to GA.

Other Event Tracking Options

Even though GA is excellent for tracking website metrics, it's not built for tracking server-side events. A category of analytics known as event tracking is the perfect application for restful API usage tracking.

The API generally looks like this:

analytics.track('API Response', {
    method  : 'POST',
    endpoint: '/comments'
    duration: 124
    status  : 500
});

And lets you see reports on the frequencies and distributions of each event and event property You can answer questions like: how many /comments API calls happened today? How many were 200s? How many had a response higher than 200ms? etc.

Here are some event tracking tools that can help you do this:

  1. Mixpanel
  2. KissMetrics
  3. Keen.IO

I'm the co-founder of Segment.io, a company that provides a simple API for client-side, server-side and mobile analytics. We let you send data from python, php, ruby, node, java, .net, javascript, and iOS, and we'll forward it to Google Analytics, Mixpanel, KissMetrics, Keen.IO, or any of our other supported services without you having to learn their API.

And finally, here's an article from our analytics academy that explains why event tracking is useful.

like image 41
Ilya Avatar answered Oct 02 '22 15:10

Ilya