You are here

How to Consume the Google Analytics API Using Python

Google Analytics is the most widely used Web analytics service on the Internet. By tracking a huge range of metrics across the profiles you provide access to, Google Analytics (GA) can provide valuable insights into your Web traffic.

A common use of GA is to discover the location of your website’s best visitors. The dashboard provides information about geographical location, as well as demographics, time on site and the bounce rate of visitors from particular countries.

GA allows you to drill far deeper into the details by region, but this complexity of data is not always best presented on the Google dashboard. But, this tutorial by Ryan Praskievicz guides followers on how to get started with the Google Analytics Reporting API and pulling GA data into Excel using Python.

Followers require the latest version of Python 2 (not Python 3), and the Google API Python Client Library, as well as access to the view (profile) you wish to pull the API report from. The author installs the client library using Pip and creates a new project in the Google Developer console.

Downloading the Python client samples and adding your GA API Client Secret Credentials allows you to run a simple report query from the IDLE IDE that shows sessions to the top 25 organic keywords. Praskievicz then shows followers how to build custom queries using the Core Reporting API Query Explorer.

The relevant code is provided, including a link to the CSV module for Python that writes the pulled data to a CSV file. One commenter also provides useful links to the scripts he used to extend the tutorial to schedule reports and write the CSV file to a local database with email confirmation upon completion.

Be sure to read the next Analytics article: How to Overcome Google Analytic's Sampling Limitations

Original Article

Google Analytics Reporting API Python Tutorial