Skip to content

Commit a08931b

Browse files
committed
Add opt out setting to disable logging for a user.
1 parent 12c8a92 commit a08931b

File tree

5 files changed

+105
-10
lines changed

5 files changed

+105
-10
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ To add properties that are tracked in every event, you can set properties for a
5858
userProperties.key = "value";
5959
amplitude.setUserProperties(userProperties);
6060

61+
62+
# Opting User Out of Logging #
63+
64+
You can turn off logging for a given user:
65+
66+
amplitude.setOptOut(true);
67+
68+
No events will be saved or sent to the server while opt out is enabled. The opt out
69+
setting will persist across page loads. Calling
70+
71+
setOptOut(false)
72+
73+
will reenable logging.
74+
6175
# Configuration Options #
6276

6377
You can configure Amplitude by passing an object as the third argument to the `init`:

amplitude.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ var DEFAULT_OPTIONS = {
136136
sessionTimeout: 30 * 60 * 1000,
137137
platform: 'Web',
138138
language: language.language,
139-
includeUtm: false
139+
includeUtm: false,
140+
optOut: false
140141
};
141142
var LocalStorageKeys = {
142143
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -258,14 +259,18 @@ var _loadCookieData = function(scope) {
258259
if (cookieData.globalUserProperties) {
259260
scope.options.userProperties = cookieData.globalUserProperties;
260261
}
262+
if (cookieData.optOut !== undefined) {
263+
scope.options.optOut = cookieData.optOut;
264+
}
261265
}
262266
};
263267

264268
var _saveCookieData = function(scope) {
265269
Cookie.set(scope.options.cookieName, {
266270
deviceId: scope.options.deviceId,
267271
userId: scope.options.userId,
268-
globalUserProperties: scope.options.userProperties
272+
globalUserProperties: scope.options.userProperties,
273+
optOut: scope.options.optOut
269274
});
270275
};
271276

@@ -335,6 +340,16 @@ Amplitude.prototype.setUserId = function(userId) {
335340
}
336341
};
337342

343+
Amplitude.prototype.setOptOut = function(enable) {
344+
try {
345+
this.options.optOut = enable;
346+
_saveCookieData(this);
347+
//log('set optOut=' + enable);
348+
} catch (e) {
349+
log(e);
350+
}
351+
}
352+
338353
Amplitude.prototype.setDeviceId = function(deviceId) {
339354
try {
340355
if (deviceId) {
@@ -370,7 +385,7 @@ Amplitude.prototype.setVersionName = function(versionName) {
370385
};
371386

372387
Amplitude.prototype.logEvent = function(eventType, eventProperties) {
373-
if (!eventType) {
388+
if (!eventType || this.options.optOut) {
374389
return;
375390
}
376391
try {
@@ -425,7 +440,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
425440
};
426441

427442
Amplitude.prototype.sendEvents = function() {
428-
if (!this._sending) {
443+
if (!this._sending && !this.options.optOut) {
429444
this._sending = true;
430445
var url = ('https:' == window.location.protocol ? 'https' : 'http') + '://' +
431446
this.options.apiEndpoint + '/';

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var DEFAULT_OPTIONS = {
2626
sessionTimeout: 30 * 60 * 1000,
2727
platform: 'Web',
2828
language: language.language,
29-
includeUtm: false
29+
includeUtm: false,
30+
optOut: false
3031
};
3132
var LocalStorageKeys = {
3233
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -148,14 +149,18 @@ var _loadCookieData = function(scope) {
148149
if (cookieData.globalUserProperties) {
149150
scope.options.userProperties = cookieData.globalUserProperties;
150151
}
152+
if (cookieData.optOut !== undefined) {
153+
scope.options.optOut = cookieData.optOut;
154+
}
151155
}
152156
};
153157

154158
var _saveCookieData = function(scope) {
155159
Cookie.set(scope.options.cookieName, {
156160
deviceId: scope.options.deviceId,
157161
userId: scope.options.userId,
158-
globalUserProperties: scope.options.userProperties
162+
globalUserProperties: scope.options.userProperties,
163+
optOut: scope.options.optOut
159164
});
160165
};
161166

@@ -225,6 +230,16 @@ Amplitude.prototype.setUserId = function(userId) {
225230
}
226231
};
227232

233+
Amplitude.prototype.setOptOut = function(enable) {
234+
try {
235+
this.options.optOut = enable;
236+
_saveCookieData(this);
237+
//log('set optOut=' + enable);
238+
} catch (e) {
239+
log(e);
240+
}
241+
}
242+
228243
Amplitude.prototype.setDeviceId = function(deviceId) {
229244
try {
230245
if (deviceId) {
@@ -260,7 +275,7 @@ Amplitude.prototype.setVersionName = function(versionName) {
260275
};
261276

262277
Amplitude.prototype.logEvent = function(eventType, eventProperties) {
263-
if (!eventType) {
278+
if (!eventType || this.options.optOut) {
264279
return;
265280
}
266281
try {
@@ -315,7 +330,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
315330
};
316331

317332
Amplitude.prototype.sendEvents = function() {
318-
if (!this._sending) {
333+
if (!this._sending && !this.options.optOut) {
319334
this._sending = true;
320335
var url = ('https:' == window.location.protocol ? 'https' : 'http') + '://' +
321336
this.options.apiEndpoint + '/';

test/amplitude.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,57 @@ describe('Amplitude', function() {
187187
});
188188
});
189189

190+
describe('optOut', function() {
191+
beforeEach(function() {
192+
amplitude.init(apiKey);
193+
});
194+
195+
afterEach(function() {
196+
reset();
197+
});
198+
199+
it('should not send events while enabled', function() {
200+
amplitude.setOptOut(true);
201+
amplitude.logEvent('Event Type 1');
202+
assert.lengthOf(server.requests, 0);
203+
});
204+
205+
it('should not send saved events while enabled', function() {
206+
amplitude.logEvent('Event Type 1');
207+
assert.lengthOf(server.requests, 1);
208+
209+
amplitude._sending = false;
210+
amplitude.setOptOut(true);
211+
amplitude.init(apiKey);
212+
assert.lengthOf(server.requests, 1);
213+
});
214+
215+
it('should start sending events again when disabled', function() {
216+
amplitude.setOptOut(true);
217+
amplitude.logEvent('Event Type 1');
218+
assert.lengthOf(server.requests, 0);
219+
220+
amplitude.setOptOut(false);
221+
amplitude.logEvent('Event Type 1');
222+
assert.lengthOf(server.requests, 1);
223+
224+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
225+
assert.lengthOf(events, 1);
226+
});
227+
228+
it('should have state be persisted in the cookie', function() {
229+
var amplitude = new Amplitude();
230+
amplitude.init(apiKey);
231+
assert.strictEqual(amplitude.options.optOut, false);
232+
233+
amplitude.setOptOut(true);
234+
235+
var amplitude2 = new Amplitude();
236+
amplitude2.init(apiKey);
237+
assert.strictEqual(amplitude2.options.optOut, true);
238+
});
239+
});
240+
190241
describe('gatherUtm', function() {
191242
beforeEach(function() {
192243
amplitude.init(apiKey);

0 commit comments

Comments
 (0)