-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApGaOptOut.js
163 lines (136 loc) · 4.78 KB
/
ApGaOptOut.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @author <[email protected]>
*
* This script allows user to disable Google analytics
*
*
*/
(function () {
'use strict';
var ApGaOptOut = function (options) {
options = this.objectMerge({}, ApGaOptOut.defaults, options);
this.debug = options.debug;
this.gaAppId = options.gaAppId;
this.gaOptOutCookiePrefix = options.gaOptOutCookiePrefix;
this.optOutCallEvent = options.optOutCallEvent;
this.elementSelector = options.elementSelector;
this.showAlterAfterDeactivate = options.showAlterAfterDeactivate;
this.alterMessage = options.alterMessage;
this.init();
};
ApGaOptOut.defaults = {
debug: false,
gaAppId: '',
gaOptOutCookiePrefix: 'ga-disable-',
optOutCallEvent: 'click',
elementSelector: '',
showAlterAfterDeactivate: false,
alterMessage: 'Google Analytics tracking has been deactivated in your browser for this website.'
};
ApGaOptOut.VERSION = '1.0.0';
ApGaOptOut.prototype = {
constructor: ApGaOptOut,
init: function () {
this._log('Init ApGaOptOut');
this._log('optOutCallEvent: ' + this.optOutCallEvent);
this._log('elementSelector: ' + this.elementSelector);
if (!this.gaAppId) {
console.error('gaAppId must be provided to work google analytics opt out');
this._log('Your provided Ga App ID is: ' + this.gaAppId);
return;
}
var self = this,
elements = document.querySelectorAll(self.elementSelector);
if (elements.length) {
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener(self.optOutCallEvent, function () {
self._log(self.optOutCallEvent + ' Triggered');
self._setGaOptOut();
})
}
}
this._checkGaOptOut();
},
/**
* If object.assign function is not available create it and merge objects
*
* @param target
* @param defaults
* @param options
*/
objectMerge: function (target, defaults, options) {
if (typeof Object.assign !== 'function') {
Object.assign = function(target) {
if (target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
return Object.assign(target, defaults, options);
},
/**
* Check if if opt out cookie exists and put it on window object
*
* @private
*/
_checkGaOptOut: function () {
if (this._checkOptOutCookie()) {
this._log('Google analytics already disabled.');
window[this.gaOptOutCookiePrefix + this.gaAppId] = true;
}
},
/**
* Disable Google Analytics for current user
*
* @private
*/
_setGaOptOut: function () {
this._log('Google analytics has disabled successfully.');
this._setOptOutCookie();
window[this.gaOptOutCookiePrefix + this.gaAppId] = true;
if(this.showAlterAfterDeactivate){
window.alert(this.alterMessage);
}
},
/**
* Check if opt out cookie already exists
*
* @returns {boolean}
* @private
*/
_checkOptOutCookie: function () {
return document.cookie.indexOf(this.gaOptOutCookiePrefix + this.gaAppId + '=true') > -1;
},
/**
* Set opt out cookie
*
* @private
*/
_setOptOutCookie: function () {
document.cookie = this.gaOptOutCookiePrefix + this.gaAppId + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
},
/**
*
*
* @private
*/
_log: function () {
if (this.debug === true) {
window.console.log.apply(this, arguments);
}
}
};
window.ApGaOptOut = ApGaOptOut;
})();