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:

2. Create the service account, create and download a private key:

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 = ['']
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(
        'reportRequests': [
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:sessions'}],
          'dimensions': [{'name': 'ga:date'}]

response = get_report(initialize_analyticsreporting())

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:

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)
                        dict[metric.get('name')] = int(value)
        df = pd.DataFrame(list)
        return df
Look at me I'm the hacker now

More info:

0 0 vote
Article Rating
Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
7 months 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!