1
0
Fork 0

Add google analytics.

merge-requests/41/head
John Giannelos 2014-09-18 16:07:43 +03:00
parent ddfc260f99
commit 121a3ba31b
5 changed files with 42 additions and 5 deletions

View File

@ -0,0 +1,18 @@
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 {}

View File

@ -157,7 +157,9 @@ class Common(Configuration):
'django.core.context_processors.tz', 'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request', 'django.core.context_processors.request',
# Your stuff: custom template context processers go here # Your stuff: custom template context processers go here
'base.context_processors.google_analytics'
) )
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs

View File

@ -43,3 +43,7 @@ class Production(Common):
)), )),
) )
# END TEMPLATE CONFIGURATION # END TEMPLATE CONFIGURATION
# GA configuration
GOOGLE_ANALYTICS_PROPERTY_ID = values.Value()
GOOGLE_ANALYTICS_DOMAIN = values.Value()

View File

@ -16,6 +16,11 @@
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script> <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]--> <![endif]-->
{% if GOOGLE_ANALYTICS_PROPERTY_ID %}
<!-- GA code -->
{% include "includes/google_analytics.html" %}
{% endif %}
<!-- Latest compiled and minified CSS --> <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
@ -124,4 +129,3 @@
{% endblock javascript %} {% endblock javascript %}
</body> </body>
</html> </html>

View File

@ -0,0 +1,9 @@
<script>
(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');
</script>