Script for accessing Google Analytics API via Jupyter Notebooks

rotags47

New member
I was scratching around for a while to figure out how to do this. I'm still learning so any feedback would be welcome. If you're still learning too and have any questions, feel free to ask!

First, go here and follow these instructions from Google:
https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py

Then here's the code:

Code:
# coding: utf-8

# In[1]:


# Install Google API Client
get_ipython().system('pip install --upgrade google-api-python- client')


# In[2]:


"""Hello Analytics Reporting API V4."""

import argparse

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

import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools


SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
KEY_FILE_LOCATION = ''
SERVICE_ACCOUNT_EMAIL = ''
VIEW_ID = ''


# In[8]:


def initialize_analyticsreporting():
  """Initializes an Analytics Reporting API V4 service object.

  Returns:
    An authorized Analytics Reporting API V4 service object.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  # Build the service object.
  analytics = build('analyticsreporting', 'v4', credentials=credentials)

  return analytics


def get_report(analytics):
  """Queries the Analytics Reporting API V4.

  Args:
    analytics: An authorized Analytics Reporting API V4 service object.
  Returns:
    The Analytics Reporting API V4 response.
  """
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:sessions'}],
          'dimensions': [{'name': 'ga:country'}]
        }]
      }
  ).execute()


def print_response(response):
  """Parses and prints the Analytics Reporting API V4 response.

  Args:
    response: An Analytics Reporting API V4 response.
  """
  for report in response.get('reports', []):
    columnHeader = report.get('columnHeader', {})
    dimensionHeaders = columnHeader.get('dimensions', [])
    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

    for row in report.get('data', {}).get('rows', []):
      dimensions = row.get('dimensions', [])
      dateRangeValues = row.get('metrics', [])

      for header, dimension in zip(dimensionHeaders, dimensions):
        print(header + ': ' + dimension)

      for i, values in enumerate(dateRangeValues):
        print('Date range: ' + str(i))
        for metricHeader, value in zip(metricHeaders, values.get('values')):
          print(metricHeader.get('name') + ': ' + value)


def main():
  analytics = initialize_analyticsreporting()
  response = get_report(analytics)
  print_response(response)

if __name__ == '__main__':
  main()


# In[12]:


analytics = initialize_analyticsreporting()
response = get_report(analytics)


# In[13]:


response


# In[36]:


response["reports"][0]["data"]["rows"]


# In[34]:


data_rows = response["reports"][0]["data"]["rows"]


# In[35]:


import pandas as pd
pd.DataFrame(data_rows)


# In[37]:


countries = pd.DataFrame(data_rows)


# In[75]:


def cleanParens(x):
    x = x.replace('[', '')
    x = x.replace(']', '')
    x = x.replace("{'values': '", '')
    x = x.replace("'}", '')
    x = x.replace("'", '')
    return x


# In[76]:


countries["dimensions"] = countries["dimensions"].astype('str')
countries["dimensions"] = 
countries["dimensions"].apply(cleanParens)

countries["metrics"] = countries["metrics"].astype('str')
countries["metrics"] = countries["metrics"].apply(cleanParens)


# In[77]:


countries.head()


# In[78]:


countries["metrics"] = countries["metrics"].astype("int")


 # In[79]:


countries.describe()


# In[96]:


dimension = response["reports"][0]["columnHeader"]["dimensions"][0]
metric = response["reports"][0]["columnHeader"]["metricHeader"]["metricHeaderEntries"][0]["name"]

countries.columns = [dimension, metric]


# In[97]:


countries.head()
 

Similar threads

Back
Top