Google Analytics Reporting API Python Tutorial

• Download Python 2
Register your application for the Analytics API in Google Developers Console 
Download the Google Python API Sample Code
Google Analytics Query Explorer
Python Code to Output Google Analytics API Query Data to CSV *save file as .py

Check out this new post on how to pull over 1 million rows of unsampled data using Python & how to pull data from multiple profiles

Check out this new post on how to pull more than 10000 rows of unsampled data using the Google Analytics Sheets Add-on.

R users check out this tutorial on how to pull your first Google Analytics R v4 reporting API query.

This guide will go through step by step instructions on how to setup Python and pull your first query directly from the Google Analytics reporting API. I will show you how to install Python on Windows and add the Google API Python library. We will create a new project in the Google Developers Console and enable the Analytics API. Next we will use a prebuilt sample Python application to get data out of Google Analytics via the API. Then I’ll walk you through how to test your own query using the Google Analytics Query Explorer. Then we will edit the Python application code to create your very own query. And finally we will pull Google Analytics data directly into Excel using Python to write a CSV file containing the Google Analytics data.

1) Download the latest version of Python 2

Download the latest version of Python 2 for Windows by clicking on the yellow download button. At the time of this post the latest version was 2.7.10. Why Python 2 and not Python 3? The code example uses Python 2.

2) Install Python 2.7.10 on Windows

After you run the installer Python should be installed on your C drive in C:\Python27

3) Install the the Google API Python Client Library using pip

We are going to use the command line to install the Google API Python library. To access the command line click Start and then in the Search or Run type cmd then press enter. This will launch the command line.

Copy and paste the line below into the command line and press enter.

C:\Python27\Scripts\pip install --upgrade google-api-python-client

Note you will have to right click and paste in the command line because control v will not work. If everything installed properly you should see text saying “Successfully installed google-api-python-client-1.4.1” at the bottom on the screen like the image below.

pip install google analytics python
We are done using the command line so you can breathe a sigh of relief or let out a cheer.

With Python version 2.7.9 and later pip is included by default. What is pip? pip is a package management system used to install and manage software packages for Python.

4) Create a new Google Analytics API Project

Make sure you are logged into the Google Analytics user account that has access to the view (profile) you want to pull your API report from. Use this link to create a new Google Analytics project in the Google Developer console. This link will automatically enable the Google Analytics API for your project.

The video below goes through the steps.

Select Create a new project from the drop down and click continue

The default name will be My Project. Choose “My Project” from the top drop down

In the left nav under APIs & auth click on Credentials

Click on the OAuth consent screen just under the My Projects drop down at the top of the screen

The Product name field is required so add the name “Google Analytics Python Reporting API” then click Save

Click on the Add credentials blue dropdown in the center of the screen and click on OAuth 2.0 client ID in the drop down

Click on the Other radio button and add the name “Python Reporting API” then click create

Your client ID and client secret will be shown in an overlay on the screen click ok

Download the client_secret json file by clicking on the download icon on the far right

5) Download the Google Analytics API Python Client Samples

Download the Zip file for the full Google API Python client. I saved the folder on the desktop. Here is the link to the full code repository on GitHub  There are lots of different samples within the Google API Python client. We are using the Analytics sample files: https://github.com/google/google-api-python-client/tree/master/samples/analytics

Unzip and save the file. I saved the file on the desktop.

6) Add Your Google Analytics API Client Secret Credentials

In the Google API client samples folder you just saved, open the analytics sample folder.

\google-api-python-client-master\samples\analytics

Open the client_secrets.json file in a text editor. I use NotePad++. Right click edit with Notepad++. Delete all the text in this sample client_secret.json file.

Find the client_secret.json file that you created and downloaded from the Google Developer console at the end of Step 4. Open your client_secret.json file in NotePad++ and copy the text in your file (this has your API credentials) and paste the text into the sample client_secrets.json file that was in the analytics sample folder. Make sure to save the file.

7) Run your first Google Analytics Report Query using Python

In the analytics sample folder the path to the folder for me is C:\Users\ryan\Desktop\google-api-python-client-master\samples\analytics Right click hello_analytics_api_v3.py file and choose to Edit with IDLE. Do not double click the hello_analytics_api_v3.py file.

IDLE is the integrated development environment for Python

In the top nav menu go to Run > Run Modulerun hello_analytics_api_v3_module

The Python Shell should open and you should see a message in the Shell that says “Your browser has been opened to visit: https://accounts.google.com/o/oauth2/…”run hello_analytics_api_v3_shell_authenticate

Your browser will open asking you to Allow access to View your Google Analytics data for your Google Analytics Python API application. If you are logged in as multiple Google users and the application asks to authenticate for the wrong user, you can take the URL and copy and paste it in a browser window with the correct user logged in.

give access to python api project to view ga data

After clicking Allow you will see a web page with the message “The authentication flow has completed”

Back in the Python Shell you’ll now see the results of your query. You should see visits (sessions) to the top 25 organic keywords from January 1, 2012 to January 15, 2012 for the first view your Google Analytics user account has access to. If this query has no data for this time period or for these dimensions have no fear, in the next steps I’ll show you how to build your own query.

python_shell_base_query_results

8) Build your Own Google Analytics Query Using the Google Analytics Core Reporting API Query Explorer

We will get back to creating our own query in Python, but first let’s use the Google Analytics Query Explorer to create a query that we know will return data. Choose the Account, Property and View you’d like to get data from. Make sure that this is a view that has sessions data for the last 30 days. When you select a view the ids field will be populated with the unique Google Analytics view (profile) identifier.

You can also find your view (profile) ID in the Admin section of Google Analytics web interface in the View Settings for your specific view.

google_analytics_view_settings_view_id

We will run a very simple query. The default start-date is 30daysAgo and the default end-date is yesterday. Both these fields should be pre populated. In the metrics field type in sessions and choose Sessions from the drop down. This will populate ga:sessions in the metrics field. In the dimensions field type in date and choose Date from the drop down. This will populate ga:date in the dimensions field. Leave all the other fields blank and click the blue Run Query button.

google_analytics_query_explorer

The result of the query will be a report with sessions for the last 30 days shown by day. Leave this page open because we will use these fields in our Python query in the next step.

google_analytics_query_explorer_results

At the bottom of the page you will see the API Query URI. If you click to include the current access_token in the Query URI this is the actual API URL of your query.

google_analytics_query_explorer_api_query_url

You can copy this URL and paste into your browser and click enter to see the results of your query in the raw JSON format.

google_analytics_query_raw_json_result_browser

9) Use the Google Analytics Query Explorer Parameters in the Python Query Code

Now we are going to take the query we built using the query explorer and use this in Python. For those of you who didn’t get data in the previous Python query we know this query will return data because it returned data using query explorer. Make sure you have the query explorer with the results open because we will use the query parameters in the Python query.

In the analytics sample folder C:\Users\ryan\Desktop\google-api-python-client-master\samples\analytics Right click the hello_analytics_api_v3 file and open with NotePad++. Go to line 133 or search for “return service” in the code

return service.data().ga().get(
      ids='ga:' + profile_id,
      start_date='2012-01-01',
      end_date='2012-01-15',
      metrics='ga:visits',
      dimensions='ga:source,ga:keyword',
      sort='-ga:visits',
      filters='ga:medium==organic',
      start_index='1',
      max_results='25').execute()

The new query that we ran in the query explorer is below. Paste this query over the old query on line 133. Note that the fields are taken directly from the query explorer i.e. metrics=’ga:sessions’ and dimensions=’ga:date’

return service.data().ga().get(
      ids='ga:' + profile_id,
      start_date='30daysAgo',
      end_date='yesterday',
      metrics='ga:sessions',
      dimensions='ga:date').execute()

 

Next rather than choosing the first profile that your Google Analytics account has access to, we are going to use the view (profile) id that we used in the query explorer. Go to line 63 or search for  “first_profile_id = get_first_profile_id(service)”. Take the ids field value excluding the ga: and paste it into line 63 for the value of the first_profile_id variable. Make sure to put quotes around the profile id. See the example below.

first_profile_id = ‘12345789’

Save this file in NotePad++ with a new file name like hello_analytics_api_v3_2.py in the analytics samples folder C:\Users\ryan\Desktop\google-api-python-client-master\samples\analytics

Open this folder C:\Users\ryan\Desktop\google-api-python-client-master\samples\analytics and right click on the hello_analytics_api_v3_2.py file that we just create and choose Edit with IDLE.

In the top nav menu go to Run > Run Module

We already authenticated our Python application when we ran the previous query in Python so the browser won’t open this time.

In the Python Shell you’ll see the results of your new query, a report with sessions for the last 30 days shown by day.shell_sessions_past30days_data_python_query

10) Google Analytics API Python Query Data Output to Excel

You can now build your own queries and pull data directly from the Google Analytics API using Python, but you may be asking how can I get this data into Excel? Luckily, there is a csv module for Python which gives you convenient tools for reading and writing to CSV files.

The original Python file has been updated to include the necessary changes to output the data to a csv file. Open the new file. Save this file in the analytics sample folder (the file is a .txt file type, make sure to change the extension to a .py file type) C:\Users\ryan\Desktop\google-api-python-client-master\samples\analytics

Right click on the hello_analytics_api_v3_csv.py file and open with NotePad++.

Go to line 63 or search for  “first_profile_id =”. Take the ids field value (from the query explorer or your previous Python file) excluding the ga: and paste it into line 63 for the value of the first_profile_id variable.

Go to line 157 or search for “filepath =” and update the variable to your actual file path. I created a folder call Python Google Analytics Data on my desktop with the file path below:

C:\\Users\\ryan\\Desktop\\Python Google Analytics Data

Note you have to escape the \ in the file path thus the \\ you see in the filepath variable.

Go to line 158 or search for “filename =” and update the variable with what you want to call the file. The current name of the file is gapythondata.csv

Save this file in NotePad++ in the analytics samples folder C:\Users\ryan\Desktop\google-api-python-client-master\samples\analytics

Open this folder C:\Users\ryan\Desktop\google-api-python-client-master\samples\analytics and right click on the hello_analytics_api_v3_csv.py file that we just saved and choose Edit with IDLE.

In the top nav menu go to Run > Run Module

In the Python Shell you’ll see the results of your new query, a report with sessions for the last 30 days shown by day, but you’ll also see a new message printed at the bottom of the shell that says:

Success Data Written to CSV File
filepath = C:\Users\ryan\Desktop\Python Google Analytics Data
filename = gapythondata.csv

Go to the folder and open your csv file in Excel. Your data in Excel should look like the screenshot below:
gapythondata_csv_excel

***Make sure you change the filename in the Python code for each new query you run, otherwise Python will overwrite your previous csv file***

Summary

Congratulations, you are now ready to start writing your own queries! You can pull data directly from the Google Analytics Reporting API using Python. Even better, you can pull Google Analytics data directly into Excel using Python to write a CSV with the Google Analytics data. While you are getting the hang of the dimension and metrics names and syntax I urge you to use the Google Analytics Query Explorer to test all  your queries. You can have up to 7 dimensions and 10 metrics in any Google Analytics Reporting API query. You can see valid dimension and metric combinations here.

If you have any questions please let me know in the comments or on Twitter @ryanpraski. Thanks to Nick Mihailovski from Google for originally sharing this Python code and thanks to my teammate Ryan Miller for helping with all my Python questions.