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.
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.
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.
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.
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.