forked from nimiq/safe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracking-consensus.js
More file actions
70 lines (57 loc) · 2.33 KB
/
Copy pathtracking-consensus.js
File metadata and controls
70 lines (57 loc) · 2.33 KB
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
class TrackingConsensus {
static init() {
const settings = this._readStorage();
if (typeof settings.allowsBrowserData === 'undefined') {
// Not yet set, ask user
document.getElementById('tracking-consensus').classList.remove('display-none');
} else {
if (this.allowsTracking(settings)) this._appendScript();
}
}
static allowsBrowserData() {
return this._readStorage().allowsBrowserData;
}
static allowsUsageData() {
return this._readStorage().allowsUsageData;
}
static allowsTracking(settings) {
console.debug("Tracking settings:", settings);
return settings.allowsBrowserData || settings.allowsUsageData;
}
/**
* @params {{allowsBrowserData?: boolean, allowsUsageData?: boolean}} obj
*/
static update(obj) {
document.getElementById('tracking-consensus').classList.add('display-none');
const settings = Object.assign(this._readStorage(), obj);
localStorage.setItem(TrackingConsensus.STORAGE_KEY, JSON.stringify(settings));
// If at least one of the settings is true, start the tracking script
if (this.allowsTracking(settings)) this._appendScript();
}
static initPaq() {
window._paq = [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
}
static _readStorage() {
const storage = localStorage.getItem(TrackingConsensus.STORAGE_KEY);
if (!storage) return {}
return JSON.parse(storage);
}
static _appendScript() {
if (this._appendedScript) return;
console.debug('Appending Tracking Script');
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
(function () {
var u = "//stats.nimiq-network.com/";
_paq.push(['setTrackerUrl', u + 'nimiq.php']);
_paq.push(['setSiteId', '3']);
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.type = 'text/javascript'; g.async = true; g.defer = true; g.src = u + 'nimiq.js'; s.parentNode.insertBefore(g, s);
})();
this._appendedScript = true;
}
}
TrackingConsensus._appendedScript = false;
TrackingConsensus.STORAGE_KEY = 'tracking-consensus';
export default TrackingConsensus;