Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Google Analytics With Django

We're about to deploy a new Django website, and we want to use Google Analytics to keep track of traffic on the site. However, we don't want all of the hits on development instances to contribute to the Google Analytics statistics.

There are a few ways we could deal with this:

  • have a configuration option in settings.py which the base template uses to decide whether or not to include the appropriate <script> elements,
  • maintain a branch which we pull into before deploying to the production server, which we ensure includes the <script> elements,
  • do something with Google Analytics to block hits to 127.0.0.1 or localhost, or
  • something else.

The first option seems the most sensible, but I'm not sure if it is. For example, would we have to start passing a google_analytics variable into all of our views?

What are your thoughts?

like image 553
Daniel Watkins Avatar asked Mar 10 '09 11:03

Daniel Watkins


People also ask

Is Google Analytics free?

Google Analytics gives you the tools, free of charge, to understand the customer journey and improve marketing ROI.

What can Google Analytics do?

Google Analytics includes features that can help users identify trends and patterns in how visitors engage with their websites. Features enable data collection, analysis, monitoring, visualization, reporting and integration with other applications.

Can you add Google Analytics to Webflow?

And you can integrate Google Analytics right into your Webflow project. You can set it up right from your Project Settings under Integrations. To visit the Google Analytics site, all you have to do is press the link to the right. You'll need to be logged in to the Google account you want associated with Analytics.


2 Answers

First, create a way to have your development and production servers pull settings from different files, say dev.py and prod.py. There are lots of ways to do this.

Then, create a setting, GOOGLE_ANALYTICS_KEY. In dev.py set it to the empty string. In prod.py, set it to your key, something like "UA-124465-1". Create a context processor to add this setting to all your template contexts, either as GOOGLE_ANALYTICS_KEY, or just go ahead and add your settings module. Then, in your template, use it to conditionally include your analytics code:

{% if settings.GOOGLE_ANALYTICS_KEY %} <script> blah blah {{settings.GOOGLE_ANALYTICS_KEY}} blah blah </script> {% endif %} 
like image 132
Ned Batchelder Avatar answered Sep 22 '22 22:09

Ned Batchelder


A little late to the party, but there's a reusable Django app called django-google-analytics. The easiest way to use it is:

  1. Add the google_analytics application to your INSTALLED_APPS section of your settings.py.
  2. In your base template, usually a base.html, insert this tag at the very top: {% load analytics %}
  3. In the same template, insert the following code right before the closing body tag: {% analytics "UA-xxxxxx-x" %} the UA-xxxxxx-x is a unique Google Analytics code for your domain.
like image 32
mikl Avatar answered Sep 21 '22 22:09

mikl