Sign inDemoInstall


Package Overview
File Explorer

Install Socket

Protect your apps from supply chain attacks



A wrapper for the Google Analytics API.




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 <>`__.


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

.. code:: python

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

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

.. code:: shell

    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 <>`__.

.. |Build Status| image::



Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc