Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Don't run analytics on localhost/127.0.0.1, except during test runs
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Feb 24, 2015
1 parent 78d138a commit 77ff23e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
31 changes: 29 additions & 2 deletions dist/bespoke-analytics.js
Expand Up @@ -19,7 +19,7 @@ var pluginName = "analytics",
convenient = ((browserGlobal.bespoke && browserGlobal.bespoke.plugins && browserGlobal.bespoke.plugins.convenient) || _dereq_("bespoke-convenient")),
cv = convenient.builder(pluginName),

isTrackingEnabled = function() {
isDNTEnabled = function() {
// This code has been duplicated elsewhere in this project.
// http://stackoverflow.com/questions/23933650/javascript-only-detection-of-do-not-track-settings-in-ie11
// http://stackoverflow.com/questions/16947459/is-it-possible-to-check-the-value-of-firefox-dnt-with-javascript/16947583#16947583
Expand All @@ -29,7 +29,34 @@ var pluginName = "analytics",
window.msDoNotTrack === "1" || navigator.doNotTrack === "yes" || navigator.doNotTrack === "1" ||
navigator.msDoNotTrack === "1" || false;

return !isDNT;
return isDNT;
},

isTestRun = function() {
var ua = navigator.userAgent || "",
isPhantomJs = (ua.toLowerCase().indexOf("phantomjs") !== -1);

return isPhantomJs;
},

isTrackableDomain = function() {
var hostname = document.location.hostname;

// A test run check should not be here, but changing the default bespoke
// plugin test setup to not use "localhost" or "127." seems harder.
if (isTestRun()) {
return true;
}

if ((hostname === "localhost" || hostname.startsWith("127."))) {
return false;
}

return true;
},

isTrackingEnabled = function() {
return isTrackableDomain() && !isDNTEnabled();
},

plugin = function(options) {
Expand Down
2 changes: 1 addition & 1 deletion dist/bespoke-analytics.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions lib/bespoke-analytics.js
Expand Up @@ -10,7 +10,7 @@ var pluginName = "analytics",
convenient = ((browserGlobal.bespoke && browserGlobal.bespoke.plugins && browserGlobal.bespoke.plugins.convenient) || require("bespoke-convenient")),
cv = convenient.builder(pluginName),

isTrackingEnabled = function() {
isDNTEnabled = function() {
// This code has been duplicated elsewhere in this project.
// http://stackoverflow.com/questions/23933650/javascript-only-detection-of-do-not-track-settings-in-ie11
// http://stackoverflow.com/questions/16947459/is-it-possible-to-check-the-value-of-firefox-dnt-with-javascript/16947583#16947583
Expand All @@ -20,7 +20,34 @@ var pluginName = "analytics",
window.msDoNotTrack === "1" || navigator.doNotTrack === "yes" || navigator.doNotTrack === "1" ||
navigator.msDoNotTrack === "1" || false;

return !isDNT;
return isDNT;
},

isTestRun = function() {
var ua = navigator.userAgent || "",
isPhantomJs = (ua.toLowerCase().indexOf("phantomjs") !== -1);

return isPhantomJs;
},

isTrackableDomain = function() {
var hostname = document.location.hostname;

// A test run check should not be here, but changing the default bespoke
// plugin test setup to not use "localhost" or "127." seems harder.
if (isTestRun()) {
return true;
}

if ((hostname === "localhost" || hostname.startsWith("127."))) {
return false;
}

return true;
},

isTrackingEnabled = function() {
return isTrackableDomain() && !isDNTEnabled();
},

plugin = function(options) {
Expand Down

0 comments on commit 77ff23e

Please sign in to comment.