Deploying Google Analytics With Django

后端 未结 10 1228
遇见更好的自我
遇见更好的自我 2020-12-23 11:29

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 developmen

10条回答
  •  余生分开走
    2020-12-23 12:22

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

提交回复
热议问题