-
Notifications
You must be signed in to change notification settings - Fork 62
Common Queries
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.
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'
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'
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)
This query returns the number of new sessions vs returning sessions.
query \
.metrics('sessions') \
.dimensions('user type')
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
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')
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')
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)
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')
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
- Authentication
- Querying
- Common Queries
- Working With Reports
- On The Command Line
- Python API documentation