Skip to content

Commit

Permalink
Add google analytics.
Browse files Browse the repository at this point in the history
To make it work, please add GOOGLE_ANALYTICS_PROPERTY_ID with your
tracking ID to settings file.

Signed-off-by: Mariusz Fik <mariusz@fidano.pl>
  • Loading branch information
Fisiu committed Mar 8, 2016
1 parent 31c58eb commit 7bc2b5a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion webapp/calendars/context_processors.py
@@ -1,7 +1,17 @@
from django.conf import settings
from .mixins import HasAccessMixin


def can_add_new_event(request):
obj = HasAccessMixin()
return {
'can_add_event': obj.has_access(request.user, action='add')[0]
}
}

def google_analytics(request):
ga_prop_id = getattr(settings, 'GOOGLE_ANALYTICS_PROPERTY_ID', False)
if not settings.DEBUG and ga_prop_id:
return {
'GOOGLE_ANALYTICS_PROPERTY_ID': ga_prop_id,
}
return {}
4 changes: 4 additions & 0 deletions webapp/mayzla/settings.py
Expand Up @@ -75,6 +75,7 @@
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'calendars.context_processors.can_add_new_event',
'calendars.context_processors.google_analytics',
],
},
},
Expand Down Expand Up @@ -136,3 +137,6 @@

# We already use jquery in admin, so don't load again
SELECT2_USE_BUNDLED_JQUERY = False

# Google Analytics - obtain your tracking ID from GA account
GOOGLE_ANALYTICS_PROPERTY_ID = ''
3 changes: 3 additions & 0 deletions webapp/templates/base.html
Expand Up @@ -78,5 +78,8 @@
</footer>
{% block extra_js %}
{% endblock %}
{% if GOOGLE_ANALYTICS_PROPERTY_ID %}
{% include "ga.html" %}
{% endif %}
</body>
</html>
9 changes: 9 additions & 0 deletions webapp/templates/ga.html
@@ -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}}', 'auto');
ga('send', 'pageview');
</script>

0 comments on commit 7bc2b5a

Please sign in to comment.