Deploying Google Analytics With Django

后端 未结 10 1225
遇见更好的自我
遇见更好的自我 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:09

    Here's the dead simple way that I solved it:

    Create an app in your project called 'utils' if you haven't already. See these directions.

    However, follow this approach to include all global context processers in addition to the project settings. It's seems to be a better practice. Here are the instructions.

    So, after you create your 'utils' app, create a a file called context_processors.py in /utils that looks like this:

    from django.conf import settings
    
    def googleanalytics(request):
        return {
            'google_tracking_id' : settings.GOOGLE_TRACKING_ID,
        }
    

    In you settings.py file, add this:

    import django.conf.global_settings as DEFAULT_SETTINGS
    
    TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
         'utils.context_processors.googleanalytics',
    

    )

    Hope this helps!

提交回复
热议问题