How to use the Google Analytics API Reporting with Python

If you are using Google Analytics, you might want to retrieve data programmatically. I have used the Google Analytics API to get data from more than 100 properties. Alternatively, you might have a script that gets your data on a repeated basis and sends an email based on this data. Within this article, I will explain to you how to use the Google Analytics API Reporting with Python, what the Google Analytics API reporting is, how you can authenticate, and how you can retrieve some basic metrics.

Google Analytics API Reporting

Note that Google Analytics has multiple APIs

The Management API allows you to change settings and create accounts, properties, and views. I will only cover the Google Analytics API reporting in this article. This API allows you to retrieve data from your Google Analytics views, which you can then use to create custom dashboards, automate reporting, and integrate your Google Analytics data with any of your business applications.

How to use the Google Analytics API Reporting with Python header

Authorize your Google Analytics API

If you want to read data from your Google Analytics views, you will need to tell your script that you are allowed to see the data that you are requesting. Therefore, you will need to “authorize” the Google Analytics API. Without this process, anyone would be able to retrieve data from anyone. Google allows you to authorize using OAuth 2.0 in four different ways. Only the two last ones are relevant for us:

Web Server

This authorization method is mostly relevant if you are creating an application using for example Flask. As we are trying to build a simple script, I will not go into detail about this method. You can find more information with example codes by going to Google’s documentation.

Client-side

This method is really similar to web server, although as the name indicates this happens client-side. This also means that you will only be able to authorize this way using JavaScript. Once again, as we are trying to build a simple script using Python, I will not go into detail about this method. You can find more information with examples of the JavaScript code by going to Google’s documentation.

Create Service Account

If you are not familiar with service accounts, you should see them as an extra email address that are associated with your project. You can give grant this email address access to the relevant resources in your GCP, thereby letting any application know that this service account can access a certain resource. You can create a service account with the following steps:

  1. Go to the Service accounts page.
  2. Assuming you already have a project, you can now select one. Or create a new one.
  3. Click add Create service account.
  4. Type a name, and description for the service account Service account details
  5. Click Create, then click Done.

Optionally you can add specific IAM roles under Service account permissions, or add users/groups that can use and manage the service account under Gran users access to this service account.

After you have created your service account, you need to download the JSON file that contains all the relevant information about your service account (including its private key). Look for your service account and click on the three dots on the right (in the column Actions). Click on Manage keys to add a new key.

Select JSON to download the relevant file. Now that you have downloaded the file, you can open the file and you need to note the client-email address. Alternatively you can find the email address on the service account page where you also created the service account in the first place. It should look like this:

{service_account_name}@{gcp_project_id}.iam.gserviceaccount.com

You will need to grant this service account access to the relevant Google Analytics view(s). Now we have got the service account and we are ready to authorize our script using this file.

Create Client ID

Similar to the service account, we can also a client ID to authorize our Python script. These two methods are really similar, but the client secret still needs a user to authorize the script. Follow these steps to get the relevant Client Id file

  1. Go to the Credentials page.
  2. Click Create credentials > OAuth client ID.
  3. Select Desktop and give it a name.
  4. Click on Create

Now to get the client secret (similar to the private key in service account), you need to find your client ID and click on the download icon all the way on the right (next to the edit and delete icons). This is again a json file, which we can use in our script.

Do not forget to enable the right API by following these steps:

  1. Open the API Library in the Google API Console.
  2. Assuming you already have a project, you can now select one. Or create a new one.
  3. Search for “Google Analytics” and click on the first item.
  4. Now click on Enable and you are done

Get Total Sessions

We can now use one of the two files (service account or client ID) to authorize our script. Depending on the method that you have chosen your script will look slightly different. First you need to install the right module

$pip install --upgrade google-api-python-client

Using Service Account

See an example on how to get total sessions using the service account below. This code is copied from the following documentation.

"""A simple example of how to access the Google Analytics API."""

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


def get_service_using_client_id(api_name, api_version, scopes, key_file_location):
    """Get a service that communicates to a Google API.

    Args:
        api_name: The name of the api to connect to.
        api_version: The api version to connect to.
        scopes: A list auth scopes to authorize for the application.
        key_file_location: The path to a valid service account JSON key file.

    Returns:
        A service that is connected to the specified API.
    """

    credentials = ServiceAccountCredentials.from_json_keyfile_name(
            key_file_location, scopes=scopes)

    # Build the service object.
    service = build(api_name, api_version, credentials=credentials)

    return service


def get_first_profile_id(service):
    # Use the Analytics service object to get the first profile id.

    # Get a list of all Google Analytics accounts for this user
    accounts = service.management().accounts().list().execute()

    if accounts.get('items'):
        # Get the first Google Analytics account.
        account = accounts.get('items')[0].get('id')

        # Get a list of all the properties for the first account.
        properties = service.management().webproperties().list(
                accountId=account).execute()

        if properties.get('items'):
            # Get the first property id.
            property = properties.get('items')[0].get('id')

            # Get a list of all views (profiles) for the first property.
            profiles = service.management().profiles().list(
                    accountId=account,
                    webPropertyId=property).execute()

            if profiles.get('items'):
                # return the first view (profile) id.
                return profiles.get('items')[0].get('id')

    return None


def get_results(service, profile_id):
    # Use the Analytics Service Object to query the Core Reporting API
    # for the number of sessions within the past seven days.
    return service.data().ga().get(
            ids='ga:' + profile_id,
            start_date='7daysAgo',
            end_date='today',
            metrics='ga:sessions').execute()


def print_results(results):
    # Print data nicely for the user.
    if results:
        print ('View (Profile):', results.get('profileInfo').get('profileName'))
        print ('Total Sessions:', results.get('rows')[0][0])

    else:
        print ('No results found')


def main():
    # Define the auth scopes to request.
    scope = 'https://www.googleapis.com/auth/analytics.readonly'
    key_file_location = {SERVICE_ACCOUNT_PATH}

    # Authenticate and construct service.
    service = get_service_using_client_id(
            api_name='analytics',
            api_version='v3',
            scopes=[scope],
            key_file_location=key_file_location)

    profile_id = get_first_profile_id(service)
    print_results(get_results(service, profile_id))


if __name__ == '__main__':
    main()

Using Client ID

Alternatively, you can use client ID to authorize your script and login with the user that has access to the relevant view.

"""A simple example of how to access the Google Analytics API."""

import argparse
from googleapiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools


def get_service(api_name, api_version, scope, client_secrets_path):
  """Get a service that communicates to a Google API.
  Args:
    api_name: string The name of the api to connect to.
    api_version: string The api version to connect to.
    scope: A list of strings representing the auth scopes to authorize for the
      connection.
    client_secrets_path: string A path to a valid client secrets file.

  Returns:
    A service that is connected to the specified API.
  """
    # Parse command-line arguments.
  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=[tools.argparser])
  flags = parser.parse_args([])

  # Set up a Flow object to be used if we need to authenticate.
  flow = client.flow_from_clientsecrets(
      client_secrets_path, scope=scope,
      message=tools.message_if_missing(client_secrets_path))

  # Prepare credentials, and authorize HTTP object with them.
  # If the credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # credentials will get written back to a file.
  storage = file.Storage(api_name + '.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
  http = credentials.authorize(http=httplib2.Http())

  # Build the service object.
  service = build(api_name, api_version, http=http)

  return service


def get_first_profile_id(service):
    # Use the Analytics service object to get the first profile id.

    # Get a list of all Google Analytics accounts for this user
    accounts = service.management().accounts().list().execute()

    if accounts.get('items'):
        # Get the first Google Analytics account.
        account = accounts.get('items')[0].get('id')

        # Get a list of all the properties for the first account.
        properties = service.management().webproperties().list(
                accountId=account).execute()

        if properties.get('items'):
            # Get the first property id.
            property = properties.get('items')[0].get('id')

            # Get a list of all views (profiles) for the first property.
            profiles = service.management().profiles().list(
                    accountId=account,
                    webPropertyId=property).execute()

            if profiles.get('items'):
                # return the first view (profile) id.
                return profiles.get('items')[0].get('id')

    return None


def get_results(service, profile_id):
    # Use the Analytics Service Object to query the Core Reporting API
    # for the number of sessions within the past seven days.
    return service.data().ga().get(
            ids='ga:' + profile_id,
            start_date='7daysAgo',
            end_date='today',
            metrics='ga:sessions').execute()


def print_results(results):
    # Print data nicely for the user.
    if results:
        print ('View (Profile):', results.get('profileInfo').get('profileName'))
        print ('Total Sessions:', results.get('rows')[0][0])

    else:
        print ('No results found')

def main():
    # Define the auth scopes to request.
    client_secret_variable = {CLIENT_SECRET_PATH}
    scope = ['https://www.googleapis.com/auth/analytics.readonly']
    service = get_service('analytics', 'v3', scope, client_secret_variable)

    # Authenticate and construct service.
    service = get_service(
        'analytics', 
        'v3', 
        scope, 
        client_secret_variable)

    profile_id = get_first_profile_id(service)
    print_results(get_results(service, profile_id))


if __name__ == '__main__':
    main()

I hope this helps you understand better how to use the Google Analytics API with Python. In the future, I will add some extra code snippets here to show you how to retrieve other metrics. If you are not sure how to run all of this code, check out my article where I explain how to install Python using Anaconda.

2 thoughts on “How to use the Google Analytics API Reporting with Python”

  1. Pingback: Google Analytics Automated Reports using Google Sheet - Automation Help

  2. Pingback: Google Analytics Data API (GA4) with Python [Extensive Guide] - Automation Help

Leave a Comment

Your email address will not be published.