Skip to content

On The Command Line

Highwall2016 edited this page Jan 15, 2021 · 1 revision

python-google-analytics comes with a command-line interface: the googleanalytics command. Use --help to find out more about how it works.

The command-line interface currently comes with four subcommands:

  • query: the raison d'être of this package, mostly equivalent in functionality to the Python package
  • authorize: 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
  • properties: explore your account
  • columns: see what metrics, dimensions, segments et cetera are present

query

All the querying features you get in the Python interface are also available on the command-line:

# daily pageviews and session duration for the mobile audience
googleanalytics query 'pageviews,session duration' \
    --segment 'mobile traffic' \
    --start 2015-01-01 --stop 2015-01-31 \
    --interval daily

googleanalytics --identity debrouwere --account Fusion --webproperty "Fusion (production)" \
    query pageviews \
    --start yesterday \
    --limit 10 \
    --dimensions pagepath \
    --filter pagepathlevel2=/story/ \
    --sort -pageviews \
    --interval day

Pick your output format – CSV, JSON, or the default ascii tables – with the --output flag:

googleanalytics query pageviews --output csv

Because queries require an account, webproperty and credentials to function, it might be a good idea to create aliases for your favorite account or accounts in your ~/.bashrc or similar:

alias fusion="googleanalytics --identity debrouwere --account Fusion --webproperty 'Fusion (production)'"
fusion query pageviews --start yesterday

You'll find many more examples of how to query in both Python and on the command-line on the Querying and Common Queries wiki pages.

authorize

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 or you will be prompted to enter them.

# look in environment variables, or prompt the user
googleanalytics authorize

# look in `GOOGLE_ANALYTICS_CLIENT_ID_DEBROUWERE`
# and `GOOGLE_ANALYTICS_CLIENT_SECRET_DEBROUWERE`
# environment variables, and save credentials
# under `debrouwere` in the keychain
googleanalytics authorize debrouwere

# specify client information on the command line
gash authorize debrouwere myid mysecret

revoke

Revoke access to your account. You'll have to authorize again before google-analytics will be able to work with your data.

googleanalytics revoke debrouwere

properties

# show all of your accounts
googleanalytics --identity debrouwere properties
# show all of the web properties for an account
googleanalytics --identity debrouwere properties debrouwere
# show all of the profiles for a web property
googleanalytics --identity debrouwere properties debrouwere http://debrouwere.org

columns

# show all of the columns (metrics and dimensions) for a profile
googleanalytics --identity debrouwere columns
# find all metrics and dimensions that have "queried" or "pageviews" in their name
googleanalytics --identity debrouwere columns queried
googleanalytics --identity debrouwere columns pageviews

blueprint

googleanalytics query metrics \
       --blueprint ./examples/query.yaml

Clone this wiki locally