【问题标题】:django finding ga.html to add Google Analyticsdjango 查找 ga.html 以添加 Google Analytics
【发布时间】:2016-04-01 15:41:56
【问题描述】:
django/
    db.sqlite3
    helloworld/
        __init__.py
        context_processors/
            __init__.py
            google_anlytics.py
        settings.py
        urls.py
        wsgi.py
    manage.py
    polls/
        __init__.py
        admin.py
        migrations/
        models.py
        templates/
            polls/
                home.html
        tests.py
        urls.py
        views.py
    templates/
        ga.html

我正在关注这篇文章,将谷歌分析添加到我的 django http://www.nomadblue.com/blog/django/google-analytics-tracking-code-into-django-project/

  1. 我已将 GOOGLE_ANALYTICS_PROPERTY_IDGOOGLE_ANALYTICS_DOMAIN 添加到 helloworld/settings.py
  2. 我已将以下内容添加到

hellowworld/context_processors/google_analytics.py

from django.conf import settings

def google_analytics(request):
    """
    Use the variables returned in this function to
    render your Google Analytics tracking code template.
    """
    ga_prop_id = getattr(settings, 'GOOGLE_ANALYTICS_PROPERTY_ID', False)
    ga_domain = getattr(settings, 'GOOGLE_ANALYTICS_DOMAIN', False)
    if not settings.DEBUG and ga_prop_id and ga_domain:
        return {
            'GOOGLE_ANALYTICS_PROPERTY_ID': ga_prop_id,
            'GOOGLE_ANALYTICS_DOMAIN': ga_domain,
        }
    return {}
  1. 然后,我将'helloworld.context_processors.google_analytics.google_analytics'添加到helloworld/settings.py

  2. 我在模板中添加了 ga.html

    (函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(窗口,文档,'脚本','//www.google-analytics.com/analytics.js','ga'); ga('创建', '{{ GOOGLE_ANALYTICS_PROPERTY_ID }}', '{{ GOOGLE_ANALYTICS_DOMAIN }}'); ga('发送', '浏览量');
  3. 我已经确保我的 django 项目 acn 看到了模板目录

    模板 = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], “APP_DIRS”:是的,

  4. 我想将以下内容添加到 polls/templates/polls/home.html

    {% 如果 GOOGLE_ANALYTICS_PROPERTY_ID %} {% 包括 "path/to/your/ga.html" %} {% endif %}

问题:

现在第二行写着"path/to/your/ga.html"
我不知道该放什么。

../../../templates/ga.html?

【问题讨论】:

    标签: python html django google-analytics


    【解决方案1】:

    我只是放了

    {% if GOOGLE_ANALYTICS_PROPERTY_ID %} 
        {% include "ga.html" %} 
    {% endif %}
    

    它找到了它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      相关资源
      最近更新 更多