If you don’t know what Google Analytics does, it is a powerful tool created by Google which uses some scripts to determine all sorts of information about the users accessing your app. This app can be a website, mobile app, script or others that can track information such as location, the number of page loads, browser type, device type etc.
I’m going to show you how to retrieve those metrics from Google Analytics into your Flask app. The purpose of this task would be if you would like to process this data even further or just to display it on your app by making a custom interface/admin panel.
Steps
1. Creating a simple Flask app
(You can skip this step if you’re implementing it on a website you’ve already made)
- Create a
main.py
file with the basic flask structure - Create a virtual environment and install the requirements
-
pip freeze
the requirements into a text file - Upload your files to a hosting provider
2. Creating a Google Analytics property
(You can skip this step if you had already done this.)
- Go to here > Admin > Create property
- Give it a suggestive name and fill in the other requested information about your app, click on advanced options and toggle “Create a Universal Analytics property”, input the URL and select “Create a Universal Analytics property only”
3. Link the Google Analytics property to your website
- From the “Tracking Info” > “Tracking Code” section of the property copy the “Global Site Tag (gtag.js)” script
- Place the script in the head of your html file (place it in layout.html if you want tracking across every page)
- Upload the changes to the server
- Make sure the Realtime Users are updating when you access your website.
! Keep in mind that it will take longer for the analytics for the past few days to show up
4. Set up the Analytics Reporting API on Google Cloud Platform
- Open the GCP Console
- On Home > Dashboard click on Create project and give it a suggestive name
- Go to APIs & Services > Library, search for “Google Analytics Reporting API” and click on Enable
- Open the Service page, select your project, click on Create service account and input a name and a description(the next two steps are not needed)
- After you created your service account click the three dots icon then Manage keys
- Click on Add Key and select JSON(a JSON file will be downloaded)
- Place the downloaded file into your python project
5. Add the GCP Service Account to the Google Analytics account
- From the previously downloaded JSON file copy the “client email” field (it should look something like this
serviceaccount@gcpprojectid.iam.gserviceaccount.com
) - On the Google Analytics > Admin page > View User Management(under the view section) click on the “+” button and Add user. Paste the email address that you copied and make sure “Read & Analyze” is checked
6. Retrieve the data from the API using python and Flask
Make sure that you are running the python app in virtual environment.
- Install the necessary libraries inside the virtual environment and add them to the requirements file
pip install google-api-python-client
pip install oauth2client
pip freeze > requirements.txt
- Flask app that retrieves the number of visitors from the past 30 days and displays them on a page.
- Replace
KEY_FILE_LOCATION
with the name of the JSON file we downloaded previously andVIEW_ID
with the one found in Admin > Property > View > View settings. The functionget_visitors()
can be changed to get other types of metrics such as browser type, country or others. You can find more info about this in the Google Analytics Response API documentation
- A basic structure of
layout.html
<!DOCTYPE html>
<html>
<head>
<title>Visitors</title>
</head>
<body>
<h1>The number of visitors from the past 30 days: <span style="color: Cyan">{{ visitors }}</span></h1>
</body>
</html>
Check out my website!
Top comments (2)
Great post .. I am getting No module named 'oauth2client' I tried pip install can you suggest something about it
Did you run
pip install oauth2client
?