This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
/
bespoke-analytics.js
129 lines (99 loc) · 4.79 KB
/
bespoke-analytics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*global module:true, require:true, window:true, document:true */
"use strict";
var pluginName = "analytics",
// Hack to get around having to write all browser code with require().
browserGlobal = (function(f) {
return f("return this")();
}(Function)),
convenient = ((browserGlobal.bespoke && browserGlobal.bespoke.plugins && browserGlobal.bespoke.plugins.convenient) || require("bespoke-convenient")),
cv = convenient.builder(pluginName),
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
// http://www.w3.org/TR/tracking-dnt/#js-dom
// http://www.w3.org/TR/tracking-dnt/#dnt-header-field
var isDNT = window.doNotTrack === "yes" || window.doNotTrack === "1" ||
window.msDoNotTrack === "1" || navigator.doNotTrack === "yes" || navigator.doNotTrack === "1" ||
navigator.msDoNotTrack === "1" || false;
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) {
var decker = function(deck) {
var cvBoundToDeck = cv.activateDeck(deck),
off = {},
slideActivations = 0,
unboundDeckMethods = {
// Plugin functions expect to be executed in a deck context
tracker: function() {
var args = Array.prototype.slice.call(arguments, 0),
trackerFunction = window.ga || function() {
cvBoundToDeck.log("Tracking disabled", args);
};
trackerFunction.apply(null, args);
},
trackActiveSlide: function() {
if (!isTrackingEnabled()) {
return;
}
// Explicitly include the hash, as bespoke-hash might be in use.
var page = document.location.pathname + document.location.search + document.location.hash;
// Doesn't handle multiple presentations on a single page.
// Then again, the tracker probably doesn't either.
// TODO: should a "screenview" be used instead of "pageview"?
// https://developers.google.com/analytics/devguides/collection/analyticsjs/screens
this.tracker("send", "pageview", page);
}
},
publicDeckMethods = ["tracker", "trackActiveSlide"],
original = {},
registerDeckExtensions = function() {
// Bind fire to the deck instance, so it doesn't have to be passed all the time.
// Works in the browser, but doesn't pass jasmine tests.
// Could be another problem with phantom-polyfill.js.
// TODO: re-test bound fire in the future.
//original.fire = cv.fire;
//cv.fire = cv.fire.bind(cv, deck);
publicDeckMethods.forEach(function(methodName) {
original[methodName] = deck[methodName];
deck[methodName] = unboundDeckMethods[methodName].bind(deck);
});
},
aslideActivateListener = function(e) {
if (slideActivations > 0) {
deck.trackActiveSlide();
}
slideActivations++;
},
enable = function() {
off["activate"] = deck.on("activate", aslideActivateListener);
},
init = function() {
registerDeckExtensions();
enable();
};
init();
};
return decker;
};
module.exports = plugin;