使用Django部署Google Analytics(分析) [英] Deploying Google Analytics With Django

查看:281
本文介绍了使用Django部署Google Analytics(分析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们即将部署一个新的Django网站,我们希望使用Google Analytics(分析)来跟踪网站上的流量。但是,我们不希望开发实例上的所有匹配都对Google Analytics(分析)统计数据做出贡献。



有几种方法可以解决这个问题: p>


  • 在settings.py中有一个配置选项,基本模板用于决定是否包含适当的脚本> 元素

  • 在部署到生产服务器之前维护一个我们进入的分支,我们确保包含< script> ; 元素,

  • 使用Google Analytics(分析)执行某些操作,以阻止点击127.0.0.1或localhost,或

  • 其他



第一个选项似乎是最明智的,但我不知道是否是。例如,我们必须开始将 google_analytics 变量传递到我们所有的视图中?



你的想法是什么

解决方案

首先,创建一种让开发和生产服务器从不同文件中提取设置的方法,如dev.py和prod的.py。有很多方法可以做到这一点。



然后,创建一个设置, GOOGLE_ANALYTICS_KEY 。在dev.py中将其设置为空字符串。在prod.py中,将其设置为您的密钥,如UA-124465-1。创建一个上下文处理器要将此设置添加到所有模板上下文中,或者作为 GOOGLE_ANALYTICS_KEY ,或者直接添加您的设置模块。然后,在您的模板中,使用它有条件地包括您的分析代码:

  {%if settings.GOOGLE_ANALYTICS_KEY%} 
< script> blah blah {{settings.GOOGLE_ANALYTICS_KEY}} blah blah< / script>
{%endif%}


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?

解决方案

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 %}

这篇关于使用Django部署Google Analytics(分析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆