If you want to regularly extract data from Google Analytics, it is worthwhile scripting the process. However, it was more complex than I expected.
I started with jaco’s post, and then added some detail from there.
Setting up your Google account
Since Google has moved to OAuth2, you need a service account to get a script to work.
There are a few steps to do from the Google Developers Console:
- Create a project (eg. GAStatistics). This step may take a minute or two.
- From the project page, go to APIs & auth -> APIs, and add permissions for the Analytics API (accepting the T&Cs)
- From the Credentials section of APIs & auth, create a new client ID, and pick Service Account when prompted. You should now automatically download a .p12 file that includes your client ids and secret keys.
- For your service account, save the email address that is generated. You’ll use in a few places.
Next you need grant your service account access to your Google Analytics account.
- From Google Analytics, pick the web property you want to gather data from.
- Navigate to the Admin tab at the top, and then to the User Management section (you need to have permissions, otherwise ask your site admin)
- Add your service account email to Add permissions for:, and choose the type of permissions. I usually pick Read & Analyze, as I’m paranoid about messing data up with a malformed script.
From Google’s side of things, you’re ready to go.
Starting a Python project
For service accounts, you need to use SignedJwtAssertionCredentials, which needs pyopenssl to work. For details, see this, or just include it.
My script isn’t too different from jaco’s example, however I added a few more things to get you started.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
This structure gives a good starting point for playing around with different queries.
I did run into an issue where credentials wouldn’t refresh from the cache. This has been fixed, but may need to patch manually.
Calling Google Analytics from the API
I found that I was often using filters to get at the data I wanted, and the filter syntax is documented.
Some examples I found useful:
filters='ga:hostname!=localhost' # exclude development data filters='ga:source=~email' # only email campaigns filters='ga:source=~email;ga:campaign=~^\w' # email campaigns, with a non-blank campaign tag
To avoid date arithmetic in Python, the API also allows relative dates, so you can write things like:
start = '31daysAgo' end = 'yesterday'
You can use the dimensions tag to split by additional fields, such as
There are various selectors that can be used in queries, including ones for custom variables, goals and dimensions. Our conversion team has several custom goals setup, so I can query for the results using:
I feel like I’m only scratching the surface with Google Analytics every time I use it. Figuring out scripting allows me to dig into further integrations for our normal workflows.