Skip to content
Highwall2016 edited this page Jan 15, 2021 · 2 revisions

This page mirrors the "Common Queries" page from the official Google Analytics documentation, with queries translated into Python. Query descriptions are copied verbatim from there.

(Open a GitHub issue if you'd like to see the equivalent queries on the command-line.)

All queries below build on a basic query for daily data from the last 30 days:

import googleanalytics as ga
accounts = ga.authenticate()
profile = accounts[0].webproperties[0].profile
query = profile.core.query.daily(months=-1)

To learn more about authentication or how to work with reports (the output of these queries), take a look at the README first.

General queries

Users and Pageviews Over Time

This query returns the total users and pageviews for the specified time period. Note that this query doesn't require any dimensions.

query.metrics('sessions', 'pageviews')
googleanalytics query 'sessions,pageviews'

Mobile traffic

This query returns some information about sessions which occurred from mobile devices. Note that "Mobile Traffic" is defined using the default segment ID -14.

query \
    .metrics('sessions', 'pageviews', 'session duration') \
    .dimensions('mobile device info', 'source') \
    .segment('mobile traffic')
googleanalytics query 'sessions,pageviews,session duration' \
    --dimensions 'mobile device info,source' \
    --segment 'mobile traffic'

Revenue Generating Campaigns

This query returns campaign and site usage data for campaigns that led to more than one purchase through your site.

query \
    .metrics('sessions', 'pageviews', 'session duration', 'bounces') \
    .dimensions('source', 'medium') \
    .users(transactions__gt=1)

Users

New vs Returning Sessions

This query returns the number of new sessions vs returning sessions.

query \
    .metrics('sessions') \
    .dimensions('user type')

Sessions by Country

This query returns a breakdown of your sessions by country, sorted by number of sessions.

query \
    .metrics('sessions') \
    .dimensions('country') \
    .sort('-sessions')

dimensions=ga:country metrics=ga:sessions sort=-ga:sessions

Browser and Operating System

This query returns a breakdown of sessions by the Operating System, web browser, and browser version used.

query \
    .metrics('sessions') \
    .dimensions('operating system', 'operating system version', 'browser', 'browser version')

Time on Site

This query returns the number of sessions and total time on site, which can be used to calculate average time on site.

query \
    .metrics('sessions', 'session duration')

Traffic Sources

All Traffic Sources - Usage

This query returns the site usage data broken down by source and medium, sorted by sessions in descending order.

query \
    .metrics('sessions', 'pageviews', 'session duration', 'exits') \
    .dimensions('source', 'medium') \
    .sort('sessions', descending=True)

All Traffic Sources - Goals

This query returns data for the first and all goals defined, sorted by total goal completions in descending order.

query \
    .metrics('sessions',
        'goal 1 starts', 'goal 1 completions', 'goal 1 value',
        'goal starts', 'goal completions', 'goal value') \
    .dimensions('source', 'medium') \
    .sort('goal completions')

All Traffic Sources - E-Commerce

This query returns information on revenue generated through the site for the given time span, sorted by sessions in descending order.

query \
    .metrics('sessions', 'transaction revenue', 'transactions', 'unique purchases') \
    .dimensions('source', 'medium') \
    .sort('sessions', descending=True)

Referring Sites

This query returns a list of domains and how many sessions each referred to your site, sorted by pageviews in descending order.

query \
    .metrics('pageviews', 'session duration', 'exits') \
    .dimensions('source') \
    .filter(medium='referral') \
    .sort('pageviews', descending=True)

Search Engines

This query returns site usage data for all traffic by search engine, sorted by pageviews in descending order.

# multiple keyword arguments in a single filter are ORed together
# multiple values for a keyword argument are ORed together
# multiple calls to `filter` are ANDed together
query \
    .metrics('pageviews', 'session duration', 'exits') \
    .dimensions('source') \
    .filter(medium=['cpa', 'cpc', 'cpm', 'cpp', 'cpv', 'organic', 'ppc']) \
    .sort('pageviews', descending=True)

Search Engines - Organic Search

This query returns site usage data for organic traffic by search engine, sorted by pageviews in descending order.

query \
    .metrics('pageviews', 'session duration', 'exits') \
    .dimensions('source') \
    .filter(medium='organic') \
    .sort('pageviews', descending=True)

Search Engines - Paid Search

This query returns site usage data for paid traffic by search engine, sorted by pageviews in descending order.

query \
    .metrics('pageviews', 'session duration', 'exits') \
    .dimensions('source') \
    .filter(medium=['cpa', 'cpc', 'cpm', 'cpp', 'cpv', 'ppc']) \
    .sort('pageviews', descending=True)

Keywords

This query returns sessions broken down by search engine keywords used, sorted by sessions in descending order.

query \
    .metrics('sessions') \
    .dimensions('keyword') \
    .sort('sessions', descending=True)

Content

Top Content

This query returns your most popular content, sorted by most pageviews.

query \
    .metrics('pageviews', 'unique pageviews', 'time on page', 'bounces', 'entrances', 'exits') \
    .dimensions('page path') \
    .sort('pageviews', descending=True)

Top Landing Pages

This query returns your most popular landing pages, sorted by entrances in descending order.

query \
    .metrics('entrances', 'bounces') \
    .dimensions('landing page') \
    .sort('entrances', descending=True)

Top Exit Pages

This query returns your most common exit pages, sorted by exits in descending order.

query \
    .metrics('pageviews', 'exits') \
    .dimensions('exit page path') \
    .sort('exits', descending=True)

Site Search - Search Terms

This query returns the number of sessions broken down by internal site search, sorted by number of unique searches for a keyword in descending order.

query \
    .metrics('total unique searches') \
    .dimensions('search keyword') \
    .sort('total unique searches', descending=True)