<p>This time we will explain how to implement google analytics to Django framework. For a case for google analytics implementation to React app, <a href="https://datak.biz/blog/detail/27/" target="_blank">see the previous article here</a>. Today we will mainly focus on pageview in django framework.</p><p>Google analytics lets you easily analyze user action, visualizing key indicators on user action such as traffic, page view, and conversion rate. What's more all of these are free, so this is strong tool for those who has their own web page if they would like to measure ROI on a certain advertisement there.</p><p><img src="/media/django-summernote/2021-01-15/a31d2989-4b00-4228-97c4-61deb617703d.png" style="width: 100%;"></p><p><br></p><p>For an initial google analytics account set up, please <a href="https://datak.biz/blog/detail/27/" target="_blank">refer previous article</a>. We will assume these are in production as we are turning off debug mode when we turn on google analytics.</p><p><br></p><h3>Update django files to embed google analytics tracking ID</h3><p>Once you got tracking ID and grobal site tag from google analytics site, we will create new file called "context_processors.py" right under directory of an application which we would like to monitor.</p><pre> #<your project name>/<your app name>/context_processors.py from django.conf import settings def google_analytics(request): """ get tracking ID if debug is false """ ga_tracking_id = getattr(settings, 'GOOGLE_ANALYTICS_TRACKING_ID', False) """ enabel 'GOOGLE_ANALYTICS_TRACKING_ID' variable in teamplate tag """ if not settings.DEBUG and ga_tracking_id: return { 'GOOGLE_ANALYTICS_TRACKING_ID': ga_tracking_id, } return {}</pre><p>then connect this file into settings.py which is right under project directory</p><pre># <your project name>/<your project name>/settings.py DEBUG = False # make sure debug mode is false! TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', '<your app name>.context_processors.google_analytics', # ADDED! ], }, }, ] ... ... GOOGLE_ANALYTICS_TRACKING_ID = '<your tracking ID>' #tracking info</pre><p>Now we are ready to set tracking ID into django framing. Next step is to embed global site tag.</p><p><br></p><h3>Prepare global site tag in html files</h3><p>Copy all global site tag information from google analytics page and paste it into new html filed called "ga.html"</p><pre><!-- <your project name>/<your app name>/templates/<your app name>/ga.html --> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=<your tracking ID>"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '<your tracking ID>'); </script></pre><p>then passing this file to parent html file</p><pre><!-- <your project name>/<your app name>/templates/<your app name>/<your parent html file>.html --> <!doctype html> <html> <head> <meta charset="utf-8"> <title>site name</title> {% if GOOGLE_ANALYTICS_TRACKING_ID %} {% include '<your app name>/ga.html' %} {% endif %} </head> .... </html></pre><p><br></p><p>That's it!</p>
<< Back to Blog Posts
Back to Home