26
loading...
This website collects cookies to deliver better user experience
ids
and metrics
. First of them is so-called table ID, which is the ID of your analytics profile. To find it, go to your analytics dashboard, click Admin in bottom left, then choose View Settings, where you will find the ID in View ID field. For this API you need to provide the ID formatted as ga:<TABLE_ID>
.rt:activeUsers
or rt:pageviews
.gcloud projects create $PROJECT_ID
. After a few seconds you will see new project in the list.[email protected]
) as user in Google Analytics with Read & Analyse access - a guide for adding users can be found here.pip install google-auth-oauthlib
pip install google-api-python-client
import os
from googleapiclient.discovery import build
from google.oauth2 import service_account
KEY_PATH = os.getenv('SA_KEY_PATH', 'path-to-secrets.json')
TABLE_ID = os.getenv('TABLE_ID', '123456789')
credentials = service_account.Credentials.from_service_account_file(KEY_PATH)
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/analytics.readonly'])
with build('analytics', 'v3', credentials=credentials) as service:
realtime_data = service.data().realtime().get(
ids=f'ga:{TABLE_ID}', metrics='rt:pageviews', dimensions='rt:pagePath').execute()
print(realtime_data)
build
function takes name of the API, it's version and previously created credentials object. If you want to access different API, then see this list for the available names and versions.ids
, metrics
and optionally dimensions
as we did with API explorer earlier. You might be wondering where did I find the methods of service
object (.data().realtime().get(...)
) - they're all documented here.print(...)
will show us something like this (trimmed for readability):{
"query": {
"ids": "ga:<TABLE_ID>",
"dimensions": "rt:pagePath",
"metrics": [
"rt:pageviews"
]
},
"profileInfo": {
"profileName": "All Web Site Data",
...
},
"totalsForAllResults": {
"rt:pageviews": "23"
},
"rows": [
["/", "2"],
["/404", "1"],
["/blog/18", "1"],
["/blog/25", "3"],
["/blog/28", "2"],
["/blog/3", "3"],
["/blog/51", "2"],
...
]
}
print(realtime_data["profileInfo"]["profileName"])
# All Web Site Data
print(realtime_data["query"]["metrics"])
# ['rt:pageviews']
print(realtime_data["query"]["dimensions"])
# rt:pagePath
print(realtime_data["totalResults"])
# 23
realtime()
method of the API, but there are 2 more we can make use of. First of them is ga()
:with build('analytics', 'v3', credentials=credentials) as service:
ga_data = service.data().ga().get(
ids=f'ga:{TABLE_ID}',
metrics='ga:sessions', dimensions='ga:country',
start_date='yesterday', end_date='today').execute()
print(ga_data)
# 'totalsForAllResults': {'ga:sessions': '878'}, 'rows': [['Angola', '1'], ['Argentina', '5']]
start_date
and end_date
.ga:
, instead of rt:
earlier..mcf()
is for Multi-Channel Funnels data, which is beyond scope of this article. If it sounds useful for you, check out the docs.with build('analytics', 'v3', credentials=credentials) as service:
ga_data = service.data().ga().get(
ids=f'ga:{TABLE_ID}',
metrics='ga:sessions', dimensions='ga:country',
start_index='1', max_results='2',
start_date='yesterday', end_date='today').execute()
print(f'Items per page = {ga_data["itemsPerPage"]}')
# Items per page = 2
print(f'Total results = {ga_data["totalResults"]}')
# Total results = 73
# These only have values if other result pages exist.
if ga_data.get('previousLink'):
print(f'Previous Link = {ga_data["previousLink"]}')
if ga_data.get('nextLink'):
print(f'Next Link = {ga_data["nextLink"]}')
# Next Link = https://www.googleapis.com/analytics/v3/data/ga?ids=ga:<TABLE_ID>&dimensions=ga:country&metrics=ga:sessions&start-date=yesterday&end-date=today&start-index=3&max-results=2
start_index='1'
and max_results='2'
to force pagination. This causes the previousLink
and nextLink
to get populated which can be used to request previous and next pages, respectively. This however doesn't work for realtime analytics using realtime()
method, as it lacks the needed arguments.metrics
and dimensions
. So, let's take a better look at all the arguments and their possible values to see how we can take full advantage of this API.rt:activeUsers
, rt:pageviews
and rt:screenViews
:rt:activeUsers
gives you number of users currently browsing your website as well as their attributesrt:pageviews
tells you which pages are being viewed by usersrt:screenViews
- same as page views, but only relevant within application, e.g. Android or iOSmetrics='rt:activeUsers', dimensions='rt:userType'
- Differentiate currently active users based on whether they're new or returning.metrics='rt:pageviews', dimensions='rt:pagePath'
- Current page views with breakdown by path.metrics='rt:pageviews', dimensions='rt:medium,rt:trafficType'
- Page views with breakdown by medium (e.g. email) and traffic type (e.g. organic).metrics='rt:pageviews', dimensions='rt:browser,rt:operatingSystem'
- Page views with breakdown by browser and operating system.metrics='rt:pageviews', dimensions='rt:country,rt:city'
- Page views with breakdown by country and city.filters
argument can be used. The syntax is quite flexible and supports arithmetic and logical operators as well as regex queries. Let's look at some examples:rt:medium==ORGANIC
- show only page visits from organic searchrt:pageviews>2
- show only results that have more than 2 page viewsrt:country=~United.*,ga:country==Canada
- show only visits from countries starting with "United" (UK, US) or Canada (,
acts as OR
operator, for AND
use ;
).sort
argument. For ascending sorting use you can use e.g. sort=rt:pagePath
and for descending you will prepend -
, e.g. sort=-rt:pageTitle
.with build('analyticsreporting', 'v4', credentials=credentials) as service:
reports = service.reports().batchGet(body={
"reportRequests": [
{
"viewId": f"ga:{TABLE_ID}",
"dateRanges": [
{
"startDate": "yesterday",
"endDate": "today"
}],
"dimensions": [
{
"name": "ga:browser"
}],
"metrics": [
{
"expression": "ga:sessions"
}]
}]
}).execute()
print(reports)
body
argument, which takes request body with all the values that we've seen previously.google-api-python-client
library, Google also provides lightweight libraries for individual services and APIs at https://github.com/googleapis/google-cloud-python. At the time of writing the specific library for analytics is still in beta and lacks documentation, but when it becomes GA (or more stable), you should probably consider exploring it.