1
0
Fork 0

[Fixes #101] Implement Analytics and DoNotTrack

merge-requests/110/head
Nikos Roussos 2015-04-09 15:13:05 +03:00
parent 2ff65aeb55
commit abb3c7338e
7 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,12 @@
from django.conf import settings
from django.template.loader import render_to_string
def analytics(request):
"""Returns analytics code."""
if settings.ENVIRONMENT == 'production':
return {'analytics_code': render_to_string('includes/analytics.html',
{'google_analytics_key': settings.GOOGLE_ANALYTICS_KEY,
'user': request.user })}
else:
return {'analytics_code': ''}

View File

@ -74,6 +74,8 @@ TEMPLATE_CONTEXT_PROCESSORS = (
'allauth.account.context_processors.account',
'allauth.socialaccount.context_processors.socialaccount',
'network.base.context_processors.analytics',
)
TEMPLATE_DIRS = (
path.join(BASE_DIR, 'templates'),

View File

@ -1,5 +1,6 @@
from base import *
ENVIRONMENT = 'dev'
# Debug
DEBUG = True

View File

@ -1,6 +1,7 @@
import os
from base import *
ENVIRONMENT = 'production'
# Apps
INSTALLED_APPS += ('djangosecure', )
@ -21,3 +22,6 @@ EMAIL_HOST = "localhost"
EMAIL_PORT = 25
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL', 'noreply@example.com')
SERVER_EMAIL = DEFAULT_FROM_EMAIL
# Analytics
GOOGLE_ANALYTICS_KEY = os.getenv('GOOGLE_ANALYTICS_KEY', None)

View File

@ -1,6 +1,8 @@
import os
from base import *
ENVIRONMENT = 'stage'
# Security
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '*')

View File

@ -96,5 +96,8 @@
<script src="{% static 'js/lib/bootstrap.min.js' %}"></script>
{% block javascript %}
{% endblock javascript %}
<!-- Google Analytics -->
{{ analytics_code }}
</body>
</html>

View File

@ -0,0 +1,14 @@
<script>
if (navigator.doNotTrack !== '1') {
(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_key }}', 'auto');
ga('send', 'pageview');
{% if user.is_active %}
ga('set', '&uid', {{user.id}});
{% endif %}
}
</script>