Skip to main content

A wrapper for the Google Analytics API.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gcp_googleanalytics-1.0.0.tar.gz (30.7 kB view details)

Uploaded Source

File details

Details for the file gcp_googleanalytics-1.0.0.tar.gz.

File metadata

  • Download URL: gcp_googleanalytics-1.0.0.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

File hashes

Hashes for gcp_googleanalytics-1.0.0.tar.gz
Algorithm Hash digest
SHA256 67fbaf14d0611cf3bbb3b3031a353d99f82c5a40cb2c3e386af0a4d2304ac2f6
MD5 9d6a6c4409c45137fe936e3980df4ed0
BLAKE2b-256 bb4d3eadaca03a361b02c660d7b2d0199f9f692065e9b93f60dfca5976cce8d3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page