Accessing Google Analytic’s Real-Time API via Python

Honestly, the hardest part is authenticating. I’m assuming you’ve already created a project in Google’s Developer Console, turned on the GA API, setup a client_secret credential, and requested access to the real-time API beta:

  1. Copy and paste the URL that is printed from authorize_url
  2. Click “Accept” when prompted; you will be redirected to a page with a long URL
  3. Paste line 25 into your console
  4. Now, input that long URL
  5. Boom, you should be on your way

Make sure to change the hard-coded paths in here to wherever you store you Google Analytics account information and the client secrets you got from the developer console.

A few to-do items:

  • This is a really manual process for getting auth tokens…obviously there is a better way
  • I’d much rather use Python’s requests library
  • Need to parse the blob of JSON that is returned

Here are some pages I found helpful: