Skip to content

Commit 57989ae

Browse files
committed
only send referrer once per session and not with events
1 parent a2838f7 commit 57989ae

File tree

4 files changed

+109
-101
lines changed

4 files changed

+109
-101
lines changed

amplitude.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ var LocalStorageKeys = {
154154
USER_ID: 'amplitude_userId',
155155
OPT_OUT: 'amplitude_optOut',
156156

157-
INITIAL_REFERRER: 'amplitude_initialReferrer'
157+
REFERRER: 'amplitude_referrer'
158158
};
159159

160160
/*
@@ -246,7 +246,7 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
246246
}
247247

248248
if (this.options.includeReferrer) {
249-
this._saveInitialReferrer(this._getReferrer());
249+
this._saveReferrer(this._getReferrer());
250250
}
251251

252252
this._lastEventTime = parseInt(localStorage.getItem(LocalStorageKeys.LAST_EVENT_TIME)) || null;
@@ -440,29 +440,38 @@ Amplitude.prototype._getReferrer = function() {
440440
};
441441

442442
Amplitude.prototype._getReferringDomain = function(referrer) {
443+
if (referrer === null || referrer === undefined || referrer === '') {
444+
return null;
445+
}
443446
var parts = referrer.split('/');
444447
if (parts.length >= 3) {
445448
return parts[2];
446449
}
447-
return '';
450+
return null;
448451
};
449452

450-
Amplitude.prototype._saveInitialReferrer = function(initialReferrer) {
451-
if (initialReferrer === '') {
453+
// since user properties are propagated on the server, only send once per session, don't need to send with every event
454+
Amplitude.prototype._saveReferrer = function(referrer) {
455+
if (referrer === null || referrer === undefined || referrer === '') {
452456
return;
453457
}
454-
var hasSessionStorage = window.sessionStorage ? true : false;
455-
if (hasSessionStorage && !window.sessionStorage.getItem(LocalStorageKeys.INITIAL_REFERRER)) {
456-
window.sessionStorage.setItem(LocalStorageKeys.INITIAL_REFERRER, initialReferrer);
457-
}
458-
};
459458

460-
Amplitude.prototype._getInitialReferrer = function() {
459+
// always setOnce initial referrer
460+
var referring_domain = this._getReferringDomain(referrer);
461+
var identify = new Identify().setOnce('initial_referrer', referrer);
462+
identify.setOnce('initial_referring_domain', referring_domain);
463+
464+
// only save referrer if not already in session storage or if storage disabled
461465
var hasSessionStorage = window.sessionStorage ? true : false;
462-
if (hasSessionStorage) {
463-
return window.sessionStorage.getItem(LocalStorageKeys.INITIAL_REFERRER) || '';
466+
if ((hasSessionStorage && !window.sessionStorage.getItem(LocalStorageKeys.REFERRER)) || !hasSessionStorage) {
467+
identify.set('referrer', referrer).set('referring_domain', referring_domain);
468+
469+
if (hasSessionStorage) {
470+
window.sessionStorage.setItem(LocalStorageKeys.REFERRER, referrer);
471+
}
464472
}
465-
return '';
473+
474+
this.identify(identify);
466475
};
467476

468477
Amplitude.prototype.saveEvents = function() {
@@ -620,18 +629,6 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
620629
// Only add utm properties to user properties for events
621630
if (eventType !== IDENTIFY_EVENT) {
622631
object.merge(userProperties, this._utmProperties);
623-
624-
// Add referral info onto the user properties
625-
if (this.options.includeReferrer) {
626-
var referrer = this._getReferrer();
627-
var initialReferrer = this._getInitialReferrer();
628-
object.merge(userProperties, {
629-
'referrer': referrer,
630-
'referring_domain': this._getReferringDomain(referrer),
631-
'initial_referrer': initialReferrer,
632-
'initial_referring_domain': this._getReferringDomain(initialReferrer)
633-
});
634-
}
635632
}
636633

637634
apiProperties = apiProperties || {};

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: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var LocalStorageKeys = {
4848
USER_ID: 'amplitude_userId',
4949
OPT_OUT: 'amplitude_optOut',
5050

51-
INITIAL_REFERRER: 'amplitude_initialReferrer'
51+
REFERRER: 'amplitude_referrer'
5252
};
5353

5454
/*
@@ -140,7 +140,7 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
140140
}
141141

142142
if (this.options.includeReferrer) {
143-
this._saveInitialReferrer(this._getReferrer());
143+
this._saveReferrer(this._getReferrer());
144144
}
145145

146146
this._lastEventTime = parseInt(localStorage.getItem(LocalStorageKeys.LAST_EVENT_TIME)) || null;
@@ -334,29 +334,38 @@ Amplitude.prototype._getReferrer = function() {
334334
};
335335

336336
Amplitude.prototype._getReferringDomain = function(referrer) {
337+
if (referrer === null || referrer === undefined || referrer === '') {
338+
return null;
339+
}
337340
var parts = referrer.split('/');
338341
if (parts.length >= 3) {
339342
return parts[2];
340343
}
341-
return '';
344+
return null;
342345
};
343346

344-
Amplitude.prototype._saveInitialReferrer = function(initialReferrer) {
345-
if (initialReferrer === '') {
347+
// since user properties are propagated on the server, only send once per session, don't need to send with every event
348+
Amplitude.prototype._saveReferrer = function(referrer) {
349+
if (referrer === null || referrer === undefined || referrer === '') {
346350
return;
347351
}
348-
var hasSessionStorage = window.sessionStorage ? true : false;
349-
if (hasSessionStorage && !window.sessionStorage.getItem(LocalStorageKeys.INITIAL_REFERRER)) {
350-
window.sessionStorage.setItem(LocalStorageKeys.INITIAL_REFERRER, initialReferrer);
351-
}
352-
};
353352

354-
Amplitude.prototype._getInitialReferrer = function() {
353+
// always setOnce initial referrer
354+
var referring_domain = this._getReferringDomain(referrer);
355+
var identify = new Identify().setOnce('initial_referrer', referrer);
356+
identify.setOnce('initial_referring_domain', referring_domain);
357+
358+
// only save referrer if not already in session storage or if storage disabled
355359
var hasSessionStorage = window.sessionStorage ? true : false;
356-
if (hasSessionStorage) {
357-
return window.sessionStorage.getItem(LocalStorageKeys.INITIAL_REFERRER) || '';
360+
if ((hasSessionStorage && !window.sessionStorage.getItem(LocalStorageKeys.REFERRER)) || !hasSessionStorage) {
361+
identify.set('referrer', referrer).set('referring_domain', referring_domain);
362+
363+
if (hasSessionStorage) {
364+
window.sessionStorage.setItem(LocalStorageKeys.REFERRER, referrer);
365+
}
358366
}
359-
return '';
367+
368+
this.identify(identify);
360369
};
361370

362371
Amplitude.prototype.saveEvents = function() {
@@ -514,18 +523,6 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
514523
// Only add utm properties to user properties for events
515524
if (eventType !== IDENTIFY_EVENT) {
516525
object.merge(userProperties, this._utmProperties);
517-
518-
// Add referral info onto the user properties
519-
if (this.options.includeReferrer) {
520-
var referrer = this._getReferrer();
521-
var initialReferrer = this._getInitialReferrer();
522-
object.merge(userProperties, {
523-
'referrer': referrer,
524-
'referring_domain': this._getReferringDomain(referrer),
525-
'initial_referrer': initialReferrer,
526-
'initial_referring_domain': this._getReferringDomain(initialReferrer)
527-
});
528-
}
529526
}
530527

531528
apiProperties = apiProperties || {};

test/amplitude.js

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,79 +1349,93 @@ describe('Amplitude', function() {
13491349
assert.equal(events[0].user_properties.referring_domain, undefined);
13501350
});
13511351

1352-
it('should send referrer data when the includeReferrer flag is true', function() {
1352+
it('should only send referrer via identify call when the includeReferrer flag is true', function() {
13531353
reset();
1354-
amplitude.init(apiKey, undefined, {includeReferrer: true});
1355-
1354+
amplitude.init(apiKey, undefined, {includeReferrer: true, batchEvents: true, eventUploadThreshold: 2});
13561355
amplitude.logEvent('Referrer Test Event', {});
13571356
assert.lengthOf(server.requests, 1);
13581357
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
1359-
assert.deepEqual(events[0].user_properties, {
1360-
initial_referrer: 'https://amplitude.com/contact',
1361-
initial_referring_domain: 'amplitude.com',
1362-
referrer: 'https://amplitude.com/contact',
1363-
referring_domain: 'amplitude.com'
1364-
});
1365-
});
1366-
1367-
it('should add referrer data to the user properties on events only', function() {
1368-
reset();
1369-
amplitude.init(apiKey, undefined, {includeReferrer: true});
1358+
assert.lengthOf(events, 2);
13701359

1371-
amplitude.setUserProperties({user_prop: true});
1372-
assert.lengthOf(server.requests, 1);
1373-
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
1360+
// first event should be identify with initial_referrer and referrer
1361+
assert.equal(events[0].event_type, '$identify');
13741362
assert.deepEqual(events[0].user_properties, {
1375-
$set: {
1376-
'user_prop': true
1363+
'$set': {
1364+
'referrer': 'https://amplitude.com/contact',
1365+
'referring_domain': 'amplitude.com'
1366+
},
1367+
'$setOnce': {
1368+
'initial_referrer': 'https://amplitude.com/contact',
1369+
'initial_referring_domain': 'amplitude.com'
13771370
}
13781371
});
1379-
server.respondWith('success');
1380-
server.respond();
13811372

1382-
amplitude.logEvent('Referrer test event');
1383-
assert.lengthOf(server.requests, 2);
1384-
var events = JSON.parse(querystring.parse(server.requests[1].requestBody).e);
1385-
assert.deepEqual(events[0].user_properties, {
1386-
initial_referrer: 'https://amplitude.com/contact',
1387-
initial_referring_domain: 'amplitude.com',
1388-
referrer: 'https://amplitude.com/contact',
1389-
referring_domain: 'amplitude.com'
1390-
});
1373+
// second event should be the test event with no referrer information
1374+
assert.equal(events[1].event_type, 'Referrer Test Event');
1375+
assert.deepEqual(events[1].user_properties, {});
1376+
1377+
// referrer should be propagated to session storage
1378+
assert.equal(sessionStorage.getItem('amplitude_referrer'), 'https://amplitude.com/contact');
13911379
});
13921380

1393-
it('should grab initial referrer data from session storage', function() {
1381+
it('should not set referrer if referrer data already in session storage', function() {
13941382
reset();
1395-
sessionStorage.setItem('amplitude_initialReferrer', 'https://www.google.com/search?');
1396-
amplitude.init(apiKey, undefined, {includeReferrer: true});
1397-
1383+
sessionStorage.setItem('amplitude_referrer', 'https://www.google.com/search?');
1384+
amplitude.init(apiKey, undefined, {includeReferrer: true, batchEvents: true, eventUploadThreshold: 2});
13981385
amplitude.logEvent('Referrer Test Event', {});
13991386
assert.lengthOf(server.requests, 1);
14001387
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
1388+
assert.lengthOf(events, 2);
1389+
1390+
// first event should be identify with initial_referrer and NO referrer
1391+
assert.equal(events[0].event_type, '$identify');
14011392
assert.deepEqual(events[0].user_properties, {
1402-
initial_referrer: 'https://www.google.com/search?',
1403-
initial_referring_domain: 'www.google.com',
1404-
referrer: 'https://amplitude.com/contact',
1405-
referring_domain: 'amplitude.com'
1393+
'$setOnce': {
1394+
'initial_referrer': 'https://amplitude.com/contact',
1395+
'initial_referring_domain': 'amplitude.com'
1396+
}
14061397
});
1398+
1399+
// second event should be the test event with no referrer information
1400+
assert.equal(events[1].event_type, 'Referrer Test Event');
1401+
assert.deepEqual(events[1].user_properties, {});
14071402
});
14081403

14091404
it('should not override any existing initial referrer values in session storage', function() {
14101405
reset();
1411-
sessionStorage.setItem('amplitude_initialReferrer', 'https://www.google.com/search?');
1412-
amplitude.init(apiKey, undefined, {includeReferrer: true});
1413-
amplitude._saveInitialReferrer('https://amplitude.com/contact');
1414-
1406+
sessionStorage.setItem('amplitude_referrer', 'https://www.google.com/search?');
1407+
amplitude.init(apiKey, undefined, {includeReferrer: true, batchEvents: true, eventUploadThreshold: 3});
1408+
amplitude._saveReferrer('https://facebook.com/contact');
14151409
amplitude.logEvent('Referrer Test Event', {});
14161410
assert.lengthOf(server.requests, 1);
14171411
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
1412+
assert.lengthOf(events, 3);
1413+
1414+
// first event should be identify with initial_referrer and NO referrer
1415+
assert.equal(events[0].event_type, '$identify');
14181416
assert.deepEqual(events[0].user_properties, {
1419-
initial_referrer: 'https://www.google.com/search?',
1420-
initial_referring_domain: 'www.google.com',
1421-
referrer: 'https://amplitude.com/contact',
1422-
referring_domain: 'amplitude.com'
1417+
'$setOnce': {
1418+
'initial_referrer': 'https://amplitude.com/contact',
1419+
'initial_referring_domain': 'amplitude.com'
1420+
}
14231421
});
1424-
})
1422+
1423+
// second event should be another identify but with the new referrer
1424+
assert.equal(events[1].event_type, '$identify');
1425+
assert.deepEqual(events[1].user_properties, {
1426+
'$setOnce': {
1427+
'initial_referrer': 'https://facebook.com/contact',
1428+
'initial_referring_domain': 'facebook.com'
1429+
}
1430+
});
1431+
1432+
// third event should be the test event with no referrer information
1433+
assert.equal(events[2].event_type, 'Referrer Test Event');
1434+
assert.deepEqual(events[2].user_properties, {});
1435+
1436+
// existing value persists
1437+
assert.equal(sessionStorage.getItem('amplitude_referrer'), 'https://www.google.com/search?');
1438+
});
14251439
});
14261440

14271441
describe('logRevenue', function() {

0 commit comments

Comments
 (0)