<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alex Marginean</title>
    <description>The latest articles on DEV Community by Alex Marginean (@alexmarginean16).</description>
    <link>https://dev.to/alexmarginean16</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F472932%2Ffa9692ed-7312-4b6c-bbf2-6f8157926ca1.jpeg</url>
      <title>DEV Community: Alex Marginean</title>
      <link>https://dev.to/alexmarginean16</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexmarginean16"/>
    <language>en</language>
    <item>
      <title>How to get website metrics from Google Analytics with Flask Python</title>
      <dc:creator>Alex Marginean</dc:creator>
      <pubDate>Thu, 11 Mar 2021 12:59:47 +0000</pubDate>
      <link>https://dev.to/alexmarginean16/how-to-get-website-metrics-from-google-analytics-with-flask-python-13m4</link>
      <guid>https://dev.to/alexmarginean16/how-to-get-website-metrics-from-google-analytics-with-flask-python-13m4</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnivfey164xqwtt6pge8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnivfey164xqwtt6pge8k.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h1&gt;
  
  
  Steps
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. Creating a simple Flask app
&lt;/h2&gt;

&lt;p&gt;(You can skip this step if you’re implementing it on a website you’ve already made)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;main.py&lt;/code&gt; file with the basic flask structure&lt;/li&gt;
&lt;li&gt;Create a virtual environment and install the requirements&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pip freeze&lt;/code&gt; the requirements into a text file&lt;/li&gt;
&lt;li&gt;Upload your files to a hosting provider&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Creating a Google Analytics property
&lt;/h2&gt;

&lt;p&gt;(You can skip this step if you had already done this.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to here &amp;gt; Admin &amp;gt; Create property&lt;/li&gt;
&lt;li&gt;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”&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Link the Google Analytics property to your website
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;From the “Tracking Info” &amp;gt; “Tracking Code” section of the property copy the “Global Site Tag (gtag.js)” script&lt;/li&gt;
&lt;li&gt;Place the script in the head of your html file (place it in layout.html if you want tracking across every page)&lt;/li&gt;
&lt;li&gt;Upload the changes to the server&lt;/li&gt;
&lt;li&gt;Make sure the Realtime Users are updating when you access your website.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;! Keep in mind that it will take longer for the analytics for the past few days to show up&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Set up the Analytics Reporting API on Google Cloud Platform
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open the GCP &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Console&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;On Home &amp;gt; Dashboard click on &lt;a href="https://console.cloud.google.com/projectcreate" rel="noopener noreferrer"&gt;Create project&lt;/a&gt; and give it a suggestive name&lt;/li&gt;
&lt;li&gt;Go to APIs &amp;amp; Services &amp;gt; &lt;a href="https://console.cloud.google.com/apis/library" rel="noopener noreferrer"&gt;Library&lt;/a&gt;, search for “Google Analytics Reporting API” and click on Enable&lt;/li&gt;
&lt;li&gt;Open the &lt;a href="https://console.developers.google.com/iam-admin/serviceaccounts" rel="noopener noreferrer"&gt;Service&lt;/a&gt; page, select your project, click on Create service account and input a name and a description(the next two steps are not needed)&lt;/li&gt;
&lt;li&gt;After you created your service account click the three dots icon then Manage keys&lt;/li&gt;
&lt;li&gt;Click on Add Key and select JSON(a JSON file will be downloaded)&lt;/li&gt;
&lt;li&gt;Place the downloaded file into your python project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Add the GCP Service Account to the Google Analytics account
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;From the previously downloaded JSON file copy the “client email” field (it should look something like this &lt;code&gt;serviceaccount@gcpprojectid.iam.gserviceaccount.com&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;On the Google Analytics &amp;gt; Admin page &amp;gt; 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 &amp;amp; Analyze” is checked&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Retrieve the data from the API using python and Flask
&lt;/h2&gt;

&lt;p&gt;Make sure that you are running the python app in virtual environment.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the necessary libraries inside the virtual environment and add them to the requirements file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;google-api-python-client
pip &lt;span class="nb"&gt;install &lt;/span&gt;oauth2client
pip freeze &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Flask app that retrieves the number of visitors from the past 30 days and displays them on a page.&lt;/li&gt;
&lt;li&gt;Replace &lt;code&gt;KEY_FILE_LOCATION&lt;/code&gt; with the name of the JSON file we downloaded previously and &lt;code&gt;VIEW_ID&lt;/code&gt; with the one found in Admin &amp;gt; Property &amp;gt; View &amp;gt; View settings. The function &lt;code&gt;get_visitors()&lt;/code&gt; 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 &lt;a href="https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py" rel="noopener noreferrer"&gt;Google Analytics Response API documentation&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A basic structure of &lt;code&gt;layout.html&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Visitors&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
         &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;The number of visitors from the past 30 days: &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"color: Cyan"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ visitors }}&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out my &lt;a href="https://alexmarginean.me" rel="noopener noreferrer"&gt;website&lt;/a&gt;!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
