diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..5c51e8d --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +dnt-helper.js diff --git a/network/base/context_processors.py b/network/base/context_processors.py index 906b51d..f169bf8 100644 --- a/network/base/context_processors.py +++ b/network/base/context_processors.py @@ -5,9 +5,7 @@ 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})} + return {'analytics_code': render_to_string('includes/analytics.html')} else: return {'analytics_code': ''} diff --git a/network/settings/production.py b/network/settings/production.py index 25e24f5..9b75c9c 100644 --- a/network/settings/production.py +++ b/network/settings/production.py @@ -37,4 +37,3 @@ OPBEAT = { 'APP_ID': os.getenv('OPBEAT_APPID', None), 'SECRET_TOKEN': os.getenv('OPBEAT_SECRET', None), } -GOOGLE_ANALYTICS_KEY = os.getenv('GOOGLE_ANALYTICS_KEY', None) diff --git a/network/static/js/dnt-helper.js b/network/static/js/dnt-helper.js new file mode 100644 index 0000000..abf646b --- /dev/null +++ b/network/static/js/dnt-helper.js @@ -0,0 +1,46 @@ +/** + * Returns true or false based on whether doNotTack is enabled. It also takes into account the + * anomalies, such as !bugzilla 887703, which effect versions of Fx 31 and lower. It also handles + * IE versions on Windows 7, 8 and 8.1, where the DNT implementation does not honor the spec. + * @see https://bugzilla.mozilla.org/show_bug.cgi?id=1217896 for more details + * @params {string} [dnt] - An optional mock doNotTrack string to ease unit testing. + * @params {string} [userAgent] - An optional mock userAgent string to ease unit testing. + * @returns {boolean} true if enabled else false + */ +function _dntEnabled(dnt, userAgent) { + + 'use strict'; + + // for old version of IE we need to use the msDoNotTrack property of navigator + // on newer versions, and newer platforms, this is doNotTrack but, on the window object + // Safari also exposes the property on the window object. + var dntStatus = dnt || navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack; + var ua = userAgent || navigator.userAgent; + + // List of Windows versions known to not implement DNT according to the standard. + var anomalousWinVersions = ['Windows NT 6.1', 'Windows NT 6.2', 'Windows NT 6.3']; + + var fxMatch = ua.match(/Firefox\/(\d+)/); + var ieRegEx = /MSIE|Trident/i; + var isIE = ieRegEx.test(ua); + // Matches from Windows up to the first occurance of ; un-greedily + // http://www.regexr.com/3c2el + var platform = ua.match(/Windows.+?(?=;)/g); + + // With old versions of IE, DNT did not exist so we simply return false; + if (isIE && typeof Array.prototype.indexOf !== 'function') { + return false; + } else if (fxMatch && parseInt(fxMatch[1], 10) < 32) { + // Can't say for sure if it is 1 or 0, due to Fx bug 887703 + dntStatus = 'Unspecified'; + } else if (isIE && platform && anomalousWinVersions.indexOf(platform.toString()) !== -1) { + // default is on, which does not honor the specification + dntStatus = 'Unspecified'; + } else { + // sets dntStatus to Disabled or Enabled based on the value returned by the browser. + // If dntStatus is undefined, it will be set to Unspecified + dntStatus = { '0': 'Disabled', '1': 'Enabled' }[dntStatus] || 'Unspecified'; + } + + return dntStatus === 'Enabled' ? true : false; +} diff --git a/network/static/js/ga.js b/network/static/js/ga.js new file mode 100644 index 0000000..a2f54f6 --- /dev/null +++ b/network/static/js/ga.js @@ -0,0 +1,13 @@ +/* global ga _dntEnabled */ + +$(document).ready(function() { + if (!_dntEnabled()) { + (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', 'UA-54920939-7', 'auto'); + ga('send', 'pageview'); + } +}); diff --git a/network/templates/base.html b/network/templates/base.html index c787733..e4a1de2 100644 --- a/network/templates/base.html +++ b/network/templates/base.html @@ -123,9 +123,8 @@ {% block javascript %} {% endblock javascript %} - {% endcompress %} - - {{ analytics_code }} + {{ analytics_code }} + {% endcompress %} diff --git a/network/templates/includes/analytics.html b/network/templates/includes/analytics.html index d323cec..92836c8 100644 --- a/network/templates/includes/analytics.html +++ b/network/templates/includes/analytics.html @@ -1,14 +1,2 @@ - + +