How to get website metrics from Google Analytics with Flask Python

Steps

1. Creating a simple Flask app

  • 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

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

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

  • 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_LOCATIONwith the name of the JSON file we downloaded previously and VIEW_ID with the one found in Admin > Property > View > View settings. The function get_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>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Marginean

Alex Marginean

20 Followers

I’m a Romanian🇷🇴 Computer Science student who likes programming and design. Here you can read about either my life experiences or different tutorials.