Reputation: 12462
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
I am following this article to add google analytics to my django http://www.nomadblue.com/blog/django/google-analytics-tracking-code-into-django-project/
GOOGLE_ANALYTICS_PROPERTY_ID
and GOOGLE_ANALYTICS_DOMAIN
to 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 {}
Then, I added 'helloworld.context_processors.google_analytics.google_analytics'
, to helloworld/settings.py
I added ga.html in templates
(function(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) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '{{ GOOGLE_ANALYTICS_PROPERTY_ID }}', '{{ GOOGLE_ANALYTICS_DOMAIN }}'); ga('send', 'pageview');I've made sure my django project acn see the templates dir
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True,
I want to add the following to polls/templates/polls/home.html
{% if GOOGLE_ANALYTICS_PROPERTY_ID %} {% include "path/to/your/ga.html" %} {% endif %}
QUESTION:
Now on the second line it says "path/to/your/ga.html"
I am not sure what to put there.
is it ../../../templates/ga.html?
Upvotes: 0
Views: 522
Reputation: 12462
I just put
{% if GOOGLE_ANALYTICS_PROPERTY_ID %}
{% include "ga.html" %}
{% endif %}
and it found it
Upvotes: 1