Llewellyn Hattingh
Llewellyn Hattingh

Reputation: 326

Python, Google Service Object in Pandas dataframe?

We have an online company. The website contains many features and we would like to analyse which customers visit which sites, and how many times.

PROBLEM:

I am trying to write a program that should use certain Google Analytics data to create an HTML table (using pandas), that can be viewed anytime with the most recent Google Analytics data.

WHAT I HAVE DONE:

I have managed to get authenticated and have all permissions (I believe so because I haven't received a permission error message yet) and get in return a service object, which I don't know how to use/open?

#!/usr/bin/env python3

"""Script that does the following:
 1) Initialise a Google Analytics Reporting API service object
"""
import os
import argparse
from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
import yaml
import pandas as pd

scopes = ['https://www.googleapis.com/auth/analytics.readonly']
# Path to client_secrets.json file.
client_secrets_path = 'credentials/client_secret_xx.apps.googleusercontent.com.json'


def initialise_analyticsreporting():
    """Initializes the analyticsreporting service object.

  Returns:
    an authorized analyticsreporting service object.
  """
    # 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(
        "credentials/client_secret_xx.apps.googleusercontent.com.json",
        scope='https://www.googleapis.com/auth/analytics.readonly',
        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('credentials/analyticsreporting.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.
    analytics = build('analyticsreporting', 'v4', http=http)

    return analytics

This returns analytics, looking like this <googleapiclient.discovery.Resource object at 0x00000XOXOXOXOX>

At the end of the day, I would just want to have the Google Analytics data in a pandas data frame so that I can manipulate and work with it. I am no expert with Google Analytics. This is crucial for our business, any help would be appreciated. I am really scratching my head.

EXPECTED OUTPUT (to serve as a guideline to what I want to achieve, I am fairly skilled with Pandas. The problem is to get the data from GA):

user_id  site               visits
123      abc.com/something  12
234      abc.com/smthngelse 7

Thanks, I am happy to answer questions

Upvotes: 0

Views: 221

Answers (1)

Eike Pierstorff
Eike Pierstorff

Reputation: 32760

Your analytics object is just a service object - you can use it to access the methods that return the data, but it does not, by itself, contain Google Analytics data. As you are using version 4 of the core reporting API, you can just look at this example from the documentation:

def get_report(analytics):
  # Use the Analytics Service Object to query the Analytics Reporting API V4.
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:sessions'}]
        }]
      }
  ).execute()

Change the metrics and add dimensions to your liking (not every combination works or makes sense, though), enter your view id and you should be good to go.

Upvotes: 2

Related Questions