【发布时间】: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/
- 我已将
GOOGLE_ANALYTICS_PROPERTY_ID和GOOGLE_ANALYTICS_DOMAIN添加到helloworld/settings.py - 我已将以下内容添加到
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 {}
然后,我将
'helloworld.context_processors.google_analytics.google_analytics'添加到helloworld/settings.py-
我在模板中添加了 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('发送', '浏览量'); -
我已经确保我的 django 项目 acn 看到了模板目录
模板 = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], “APP_DIRS”:是的,
-
我想将以下内容添加到 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