Skip to main content
`python-google-analytics` provides a light wrapper on top of Google's own Google Analytics API client for Python. It also includes a minimalistic command-line interface to explore your account.

* **Authentication.** OAuth2 is a bit of a pain and we've made it easier.
* **Querying.** Easier to query per week, month or year. Query using metric IDs or using their full names, whichever you think is nicer. Work with both the Core and the Live APIs.
* **Reporting.** Iterate through the entire report or column-per-column.
* **Exploration.** Traverse the account hierarchy from account to webproperty to profile to dimensions, both programmatically and with the included command-line interface.

## Authentication

The newest Google Analytics API, v3, only supports authentication using OAuth2. It won't work with your account username and password. There's a few steps to get started.

1. Go to https://console.developers.google.com/project and create a new project. This registers your application with Google.
2. Click on your project, go to `APIs & auth` and then `Credentials`.
3. Find your Client ID and Client secret. Optionally, add these to your environment variables, e.g. by adding `export GOOGLE_ANALYTICS_CLIENT_ID=...` (and so on) to your `~/.bashrc`

Now we're ready to authenticate. If you've put the client secrets in the environment, you can use these environment variables to authenticate:

import os
import googleanalytics as ga
accounts = ga.oauth.ask_and_authenticate(os.environ)

`accounts` is a list of user accounts that your credentials have given you access to.

If you didn't add the client secrets to your environment variables, you can also just pass them directly from your code:

import googleanalytics as ga
client = dict(
client_id='myproj.apps.googleusercontent.com',
client_secret='mysecret'
)
accounts = ga.oauth.ask_and_authenticate(**client)

### Storing credentials

If you'd like to store the OAuth2 tokens so you don't have to ask for permission every time you run your code, you can first request tokens and only later use them to authenticate, allowing you to save those tokens for later use. Here's one way that could work:

import googleanalytics as ga
client = dict(
client_id='myproj.apps.googleusercontent.com',
client_secret='mysecret'
)

if os.path.exists('tokens.json'):
tokens = json.load(open('tokens.json'))
else:
tokens = ga.oauth.ask(**client)
json.dump(tokens, open('tokens.json', 'w'), indent=4)

accounts = ga.oauth.authenticate(**tokens)

## Querying

The querying interface currently looks like this.

account = accounts[0]
webproperty = account.webproperties[0]
profile = webproperty.profiles[0]

print account.metrics
print account.dimensions
print account.metrics['pageviews'] == account.metrics['ga:pageviews']

q = profile.query('pageviews').range('2014-06-01', days=5)
report = q.execute()
print report['pageviews']

## CLI

`python-google-analytics` comes with a small command-line interface through the `gash` command. Use `--help` to find out more about how it works.

The command-line interface currently comes with three subcommands:

* `auth`: get a Google Analytics OAuth token, given a client id and secret (provided as arguments, or procured from the environment)
* `revoke`: revoke existing authentication, useful for debugging or when your existing tokens for some reason don't work anymore
* `ls`: explore your account

## auth

You may specify the `client_id` and `client_secret` on the
commandline, optionally prefaced with a short and more
easily-remembered name for this client.

If no `client_id` and `client_secret` are specified, these
will be fetched from your environment variables,
by default these are in `GOOGLE_ANALYTICS_CLIENT_ID` and
`GOOGLE_ANALYTICS_CLIENT_SECRET` but you may specify a
suffix as the first argument to this command.

# look in environment variables
gash auth

# look in `GOOGLE_ANALYTICS_CLIENT_ID_B`
# and `GOOGLE_ANALYTICS_CLIENT_SECRET_B`
# environment variables
gash auth b

# specify client information on the command line
gash auth myid mysecret

# optionally specify a more readable client name
# for later reference
gash auth mynick myid mysecret

## revoke

gash revoke <name>

## ls

# show all of your accounts
gash ls myproj
# show all of the web properties for an account
gash ls myproj myacc
# show all of the profiles for a web property
gash ls myproj myacc myprop
# show all of the columns (metrics and dimensions) for a profile
gash ls myproj myacc myprop myprof
gash ls myproj . . .
# find all metrics and dimensions that have "queried" in their name
gash ls myproj myacc . . queried

Metadata

Release files for googleanalytics 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for googleanalytics 0.2.0
File Size Uploaded
googleanalytics-0.2.0.tar.gz 10.8 kB Details

Release files / googleanalytics-0.2.0.tar.gz

Download URL googleanalytics-0.2.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e8695b4e611f14e486b4e65de977c3c94ae7ddbaeb47ae676ef9bfcae3e11b9c
BLAKE2b-256 checksum
How to use checksums
7e6072d7023fd11dadd90f2276598e13a9cdc07851db1be3695d43f9232d73ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.26.0

1 release file

0.23.0

1 release file

0.22.2

1 release file

0.22.1

1 release file

0.21.4

1 release file

0.21.3

1 release file

0.21.2

1 release file

0.21.1

1 release file

0.21.0

1 release file

0.20.5

1 release file

0.20.4

1 release file

0.20.1

1 release file

0.19.5

1 release file

0.19.4

1 release file

0.19.2

1 release file

0.19.1

1 release file

0.18.2

1 release file

0.18.1

1 release file

0.18.0

1 release file

0.17.0

1 release file

0.16.2

1 release file

0.16.1

1 release file

0.16.0

1 release file

0.15.3

1 release file

0.15.2

1 release file

0.15.1

1 release file

0.15.0

1 release file

0.14.2

1 release file

0.14.1

1 release file

0.14.0

1 release file

0.13.3

1 release file

0.13.2

1 release file

0.13.1

1 release file

0.13.0

1 release file

0.11.0

1 release file

0.10.1

1 release file

0.9.0

1 release file

0.8.8

1 release file

0.8.7

1 release file

0.8.6

1 release file

0.8.5

1 release file

0.8.4

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.2

1 release file

0.3.0

1 release file

0.2.1

1 release file

This release

0.2.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page