Skip to main content

Google Analytics for Python and the command-line

Build Status

google-analytics takes the pain out of working with the Google Analytics reporting APIs. It supports both the Core and the Real Time API. It is written in Python but there’s also a full-featured command-line interface.

  • Authentication. OAuth2 is a bit of a pain and we’ve made it easier, both for interactive use and for server applications. We can also save your credentials in the operating system’s keychain if you want to.

  • 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 Real Time APIs.

  • Reporting. Generate reports from the command-line. Optionally describe reports and queries in easy-to-read and easy-to-write YAML files. Reports in Python work better too: 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.

  • Exports. Clean JSON and CSV – as well as Pandas data frames – so you can analyze the data in anything from Excel to R.

This package is built on top of Google’s own API client for Python.

Quickstart

First, install the package using pip

  • Python 2: pip install googleanalytics

  • Python 3: pip3 install googleanalytics

Then, create a new project in the Google Developers Console, enable the Analytics API under “APIs & auth > APIs” and generate credentials for an installed application under “APIs & auth > Credentials”. Keep this page open. For more detail, look at the wiki page on authentication.

After that, executing your first query is as easy as

import googleanalytics as ga
accounts = ga.authenticate()
profile = accounts[0].webproperties[0].profile
pageviews = profile.core.query.metrics('pageviews').range('yesterday').value
print(pageviews)

Or on the command-line, that’d be:

googleanalytics --identity <identity> --account <account> --webproperty <webproperty> \
    query pageviews --start yesterday

The account, webproperty and profile determine what data you’ll be querying. Learn more about profiles and querying on the Querying wiki page, or alternatively look at the Common Queries page for lots of examples. Read more about how to work with the resulting data in Working With Reports. On The Command-Line has more details about the command-line application.

The example above will authorize the app and authenticate you interactively. It is also possible to pass credentials as arguments in Python, using environment variables or from your operating system’s keychain. Authentication is treated in much more depth on the authentication wiki page.

Metadata

Release files for googleanalytics 0.26.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.26.0
File Size Uploaded Provenance Actions
googleanalytics-0.26.0.tar.gz 31.0 kB Without provenance Details for googleanalytics-0.26.0.tar.gz

Release files / googleanalytics-0.26.0.tar.gz

Download URL googleanalytics-0.26.0.tar.gz
Size 31.0 kB
Tags Source
SHA-256 checksum
How to use checksums
291f31e7db0dc267c3c86292bfcbabc2fbb3eec89dd500db62bb7d85346a7812
BLAKE2b-256 checksum
How to use checksums
53bc0e3bbd0944f68c0c59526fad3d46a1c74b14cee58e515199a8477cb82f81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for googleanalytics 0.26.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.26.0 This release

1 release file No provenance

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