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:
<script>
elements,<script>
elements,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?
Google Analytics gives you the tools, free of charge, to understand the customer journey and improve marketing ROI.
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.
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.
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 %}
A little late to the party, but there's a reusable Django app called django-google-analytics. The easiest way to use it is:
google_analytics
application to your INSTALLED_APPS
section of your settings.py
.base.html
, insert this tag at the very top: {% load analytics %}
{% analytics "UA-xxxxxx-x" %}
the UA-xxxxxx-x
is a unique Google Analytics code for your domain.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With