diff options
author | Kohei Yoshino <kohei.yoshino@gmail.com> | 2017-11-28 17:27:54 +0100 |
---|---|---|
committer | Dylan William Hardison <dylan@hardison.net> | 2017-11-28 17:27:54 +0100 |
commit | 064427551b0dfd6a383f59367d1efd16a86d5251 (patch) | |
tree | 24788f56412d8f16d36207f119b1fcd16e0bd5b0 /extensions/GoogleAnalytics/web | |
parent | 5d70ba4413fde42de842105a76f95cd4b54c1825 (diff) | |
download | bugzilla-064427551b0dfd6a383f59367d1efd16a86d5251.tar.gz bugzilla-064427551b0dfd6a383f59367d1efd16a86d5251.tar.xz |
Bug 1379607 - Reimplement Google Analytics on bugzilla.mozilla.org
Diffstat (limited to 'extensions/GoogleAnalytics/web')
-rw-r--r-- | extensions/GoogleAnalytics/web/js/analytics.js | 21 | ||||
-rw-r--r-- | extensions/GoogleAnalytics/web/js/dnt-helper.js | 54 |
2 files changed, 75 insertions, 0 deletions
diff --git a/extensions/GoogleAnalytics/web/js/analytics.js b/extensions/GoogleAnalytics/web/js/analytics.js new file mode 100644 index 000000000..25f7d7527 --- /dev/null +++ b/extensions/GoogleAnalytics/web/js/analytics.js @@ -0,0 +1,21 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This Source Code Form is "Incompatible With Secondary Licenses", as + * defined by the Mozilla Public License, v. 2.0. */ + +$(function() { + var meta = $('meta[name="google-analytics"]'); + + if (typeof Mozilla.dntEnabled === 'function' && !Mozilla.dntEnabled() && meta.length) { + // Activate Google Analytics + window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; + ga('create', meta.attr('content'), 'auto'); + ga('set', 'anonymizeIp', true); + ga('set', 'location', meta.data('location')); + ga('set', 'title', meta.data('title')); + // Track page view + ga('send', 'pageview'); + } +}); diff --git a/extensions/GoogleAnalytics/web/js/dnt-helper.js b/extensions/GoogleAnalytics/web/js/dnt-helper.js new file mode 100644 index 000000000..828fdc7ea --- /dev/null +++ b/extensions/GoogleAnalytics/web/js/dnt-helper.js @@ -0,0 +1,54 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// create namespace +if (typeof Mozilla === 'undefined') { + var Mozilla = {}; +} + +/** + * Returns true or false based on whether doNotTrack 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} [ua] - An optional mock userAgent string to ease unit testing. + * @returns {boolean} true if enabled else false + */ +Mozilla.dntEnabled = function(dnt, ua) { + '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 userAgent = ua || 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 = userAgent.match(/Firefox\/(\d+)/); + var ieRegEx = /MSIE|Trident/i; + var isIE = ieRegEx.test(userAgent); + // Matches from Windows up to the first occurance of ; un-greedily + // http://www.regexr.com/3c2el + var platform = userAgent.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; +}; |