Google Analytics API Request (Python)

Google Analytics Reporting Api gives you a lot of possibilities like downloading your data without sampling, exporting it to databases or just ability to work with raw data (for example in Jupyter Notebooks).
The easiest way I`ve found to make a API request to Google Analytics:

1. Create a Google Cloud Platform project and enable the Google Analytics Reporting API:
https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com&credential=client_key

2. Create the service account, create and download a private key:
https://console.developers.google.com/iam-admin/serviceaccounts

3. Add the email of your servise account as a user to your Google Analytics account.

4. Install the client library:
pip install --upgrade google-api-python-client

5. Run the code (replace ‘your_private_key.json’ and ‘Google Analytics view ID’ with your values)

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'your_private_key.json'
VIEW_ID = 'Google Analytics view ID'

def initialize_analyticsreporting():
    credentials = service_account.Credentials.from_service_account_file(KEY_FILE_LOCATION)
    analytics = build('analyticsreporting', 'v4', credentials=credentials)
    return analytics

def get_report(analytics):
    return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:sessions'}],
          'dimensions': [{'name': 'ga:date'}]
        }]
      }
  ).execute()

response = get_report(initialize_analyticsreporting())
print(response)

This request will return you data in json format, but you can transform it into the Pandas dataframe for easier manipulation. I`ve found the great function that would do that here:
https://www.themarketingtechnologist.co/getting-started-with-the-google-analytics-reporting-api-in-python/

import numpy as np
import pandas as pd
def print_response(response):
    list = []
    for report in response.get('reports', []):
        columnHeader = report.get('columnHeader', {})
        dimensionHeaders = columnHeader.get('dimensions', [])
        metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
        rows = report.get('data', {}).get('rows', [])
        for row in rows:
            dict = {}
            dimensions = row.get('dimensions', [])
            dateRangeValues = row.get('metrics', [])
            for header, dimension in zip(dimensionHeaders, dimensions):
                dict[header] = dimension
            for i, values in enumerate(dateRangeValues):
                for metric, value in zip(metricHeaders, values.get('values')):
                    if ',' in value or '.' in value:
                        dict[metric.get('name')] = float(value)
                    else:
                        dict[metric.get('name')] = int(value)
            list.append(dict)
        df = pd.DataFrame(list)
        return df
print(print_response(response))
Look at me I'm the hacker now

More info:
https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Amanda
Amanda
4 years ago

I have looked forever and have seen multiple very long responses to this problem and yours was short, quick and easy to understand/follow. Thank you so much!

Serhii Puzyrov
Reply to  Amanda
4 years ago

Hi, Amanda
Iā€™m glad that it helped you šŸ™‚
Kind Regards,
Serhii