How to Get Your Google Analytics Report from Python – Part 2

The idea was to make my blog-life easier by quickly scripting some code to pull my Google Analytics and Adsense data for me at the end of each day. Easy being the key idea there. Auto-magic. The bane of any Data Scientist’s existence. If you haven’t read Use Python to Automatically get your Google Analytics Report – Part 1, it may be helpful before reading today’s post.

Words Every Data Scientist Will Hear

Please Mr. Data Scientist, build me a data model that can extract all necessary features and predict the color of this unicorn. Simple in concept but not so simple when you’ve never seen one before. Lucky for you, I found that dodgy bugger.

What Is Most Important To You?

After writing Use Python to Automagically get your Google Analytics Report – Part 1, I realized that the Google Adsense API had less information than what I really wanted (not to mention painful documentation). Even though it has total website earnings, the Google Analytics Report API has more detailed information on readers. For the purposes of this post, I’ll be happy just extracting total sessions and pageviews in a given time period.

"""--------------The Process - Part 2------------
0. See Part 1 of this tutorial -
1. Get the Analytics service
2. Get certain fields from the Google Analytics Report API
3. Print out those pesky fields
4. Drink an espresso
5. Make a main function to execute all this
Oauth2 2.0 Explained:
Analytics Reporting API:

Get to Coding Already

I kept the get_credentials() function from the Adsense code in Part 1,but created a new function called get_Analytics_service(). Once we have the get_credentials() method we can use and abuse the Google API services.

def get_Analytics_service():
"""Returns Google Analytics Report API"""
credentials = get_credentials()
http = httplib2.Http()
http = credentials.authorize(http)
service ='analytics', 'v4', http=http)
print "Got Analytics service"
return service

After getting the Analytics service object, we will need a way to get all the data from the object. Let’s create a new function called get_report(analytics) to analyze the service object.

def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.
response: An Analytics Reporting API V4 response object
#fyi this is not my code, i grabbed it from github
#forgot to copy the url though
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

Now that we have a way to analyze all the data, we can create a main function. The runAnalytics() function should give you an idea from a high-level how Google APIs work from OAuth to printing the service. The VIEW_ID variable is basically your Analytics ID that is found from your Google API console.

def runAnalytics():
"""easy function to run everything"""
#gets OAuth from the API
analytics = get_Analytics_service()
#get the object return from the API
#send that object to print out useful fields
response = get_report(analytics)
VIEW_ID = 'Your View ID here'
global VIEW_ID

What Now?

If you wanted to use a different Google API, you would change the scopes variable in get_credentials() then add a new function to interact with the Gmail service.

Happy Coding


Leave a Reply

Your email address will not be published. Required fields are marked *