Skip to content

Commit 1809499

Browse files
committed
add saveParamsReferrerOncePerSession option, clean up session logic, fix tests
1 parent 63b80bf commit 1809499

File tree

7 files changed

+164
-371
lines changed

7 files changed

+164
-371
lines changed

amplitude.js

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,15 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
572572
this._sessionId = now;
573573

574574
// only capture UTM params and referrer if new session
575-
if (this.options.includeUtm) {
576-
this._initUtmData();
577-
}
578-
if (this.options.includeReferrer) {
579-
this._saveReferrer(this._getReferrer());
580-
}
581-
if (this.options.includeGclid) {
582-
this._saveGclid();
575+
if (this.options.saveParamsReferrerOncePerSession) {
576+
this._trackParamsAndReferrer();
583577
}
584578
}
579+
580+
if (!this.options.saveParamsReferrerOncePerSession) {
581+
this._trackParamsAndReferrer();
582+
}
583+
585584
this._lastEventTime = now;
586585
_saveCookieData(this);
587586

@@ -616,6 +615,21 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
616615
}
617616
};
618617

618+
/**
619+
* @private
620+
*/
621+
AmplitudeClient.prototype._trackParamsAndReferrer = function _trackParamsAndReferrer() {
622+
if (this.options.includeUtm) {
623+
this._initUtmData();
624+
}
625+
if (this.options.includeReferrer) {
626+
this._saveReferrer(this._getReferrer());
627+
}
628+
if (this.options.includeGclid) {
629+
this._saveGclid(this._getUrlParams());
630+
}
631+
};
632+
619633
/**
620634
* Parse and validate user specified config values and overwrite existing option value
621635
* DEFAULT_OPTIONS provides list of all config keys that are modifiable, as well as expected types for values
@@ -916,17 +930,18 @@ var _saveCookieData = function _saveCookieData(scope) {
916930
* @private
917931
*/
918932
AmplitudeClient.prototype._initUtmData = function _initUtmData(queryParams, cookieParams) {
919-
queryParams = queryParams || location.search;
933+
queryParams = queryParams || this._getUrlParams();
920934
cookieParams = cookieParams || this.cookieStorage.get('__utmz');
921935
var utmProperties = getUtmData(cookieParams, queryParams);
922-
_sendUserPropertiesOncePerSession(this, Constants.UTM_PROPERTIES, utmProperties);
936+
_sendParamsReferrerUserProperties(this, utmProperties);
923937
};
924938

925939
/**
926-
* Since user properties are propagated on server, only send once per session, don't need to send with every event
940+
* The calling function should determine when it is appropriate to send these user properties. This function
941+
* will no longer contain any session storage checking logic.
927942
* @private
928943
*/
929-
var _sendUserPropertiesOncePerSession = function _sendUserPropertiesOncePerSession(scope, storageKey, userProperties) {
944+
var _sendParamsReferrerUserProperties = function _sendParamsReferrerUserProperties(scope, userProperties) {
930945
if (type(userProperties) !== 'object' || Object.keys(userProperties).length === 0) {
931946
return;
932947
}
@@ -936,20 +951,7 @@ var _sendUserPropertiesOncePerSession = function _sendUserPropertiesOncePerSessi
936951
for (var key in userProperties) {
937952
if (userProperties.hasOwnProperty(key)) {
938953
identify.setOnce('initial_' + key, userProperties[key]);
939-
}
940-
}
941-
942-
// only save userProperties if not already in sessionStorage under key or if storage disabled
943-
var hasSessionStorage = utils.sessionStorageEnabled();
944-
if ((hasSessionStorage && !(scope._getFromStorage(sessionStorage, storageKey))) || !hasSessionStorage) {
945-
for (var property in userProperties) {
946-
if (userProperties.hasOwnProperty(property)) {
947-
identify.set(property, userProperties[property]);
948-
}
949-
}
950-
951-
if (hasSessionStorage) {
952-
scope._setInStorage(sessionStorage, storageKey, JSON.stringify(userProperties));
954+
identify.set(key, userProperties[key]);
953955
}
954956
}
955957

@@ -963,19 +965,24 @@ AmplitudeClient.prototype._getReferrer = function _getReferrer() {
963965
return document.referrer;
964966
};
965967

968+
/**
969+
* @private
970+
*/
971+
AmplitudeClient.prototype._getUrlParams = function _getUrlParams() {
972+
return location.search;
973+
};
974+
966975
/**
967976
* Try to fetch Google Gclid from url params.
968977
* @private
969978
*/
970-
AmplitudeClient.prototype._saveGclid = function _saveGclid(queryParams) {
971-
queryParams = queryParams || location.search;
972-
debugger;
973-
var gclid = utils.getQueryParam('gclid', queryParams);
979+
AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) {
980+
var gclid = utils.getQueryParam('gclid', urlParams);
974981
if (utils.isEmptyString(gclid)) {
975982
return;
976983
}
977984
var gclidProperties = {'gclid': gclid};
978-
_sendUserPropertiesOncePerSession(this, Constants.GCLID, gclidProperties);
985+
_sendParamsReferrerUserProperties(this, gclidProperties);
979986
};
980987

981988
/**
@@ -1006,7 +1013,7 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
10061013
'referrer': referrer,
10071014
'referring_domain': this._getReferringDomain(referrer)
10081015
};
1009-
_sendUserPropertiesOncePerSession(this, Constants.REFERRER, referrerInfo);
1016+
_sendParamsReferrerUserProperties(this, referrerInfo);
10101017
};
10111018

10121019
/**
@@ -1650,10 +1657,7 @@ module.exports = {
16501657
LAST_EVENT_TIME: 'amplitude_lastEventTime',
16511658
LAST_IDENTIFY_ID: 'amplitude_lastIdentifyId',
16521659
LAST_SEQUENCE_NUMBER: 'amplitude_lastSequenceNumber',
1653-
REFERRER: 'amplitude_referrer',
16541660
SESSION_ID: 'amplitude_sessionId',
1655-
UTM_PROPERTIES: 'amplitude_utm_properties',
1656-
GCLID: 'amplitude_gclid',
16571661

16581662
// Used in cookie as well
16591663
DEVICE_ID: 'amplitude_deviceId',
@@ -2580,12 +2584,12 @@ var regexp = /[a-z0-9][a-z0-9\-]*[a-z0-9]\.[a-z\.]{2,6}$/i;
25802584

25812585
/**
25822586
* Get the top domain.
2583-
*
2587+
*
25842588
* Official Grammar: http://tools.ietf.org/html/rfc883#page-56
25852589
* Look for tlds with up to 2-6 characters.
2586-
*
2590+
*
25872591
* Example:
2588-
*
2592+
*
25892593
* domain('http://localhost:3000/baz');
25902594
* // => ''
25912595
* domain('http://dev:3000/baz');
@@ -2594,7 +2598,7 @@ var regexp = /[a-z0-9][a-z0-9\-]*[a-z0-9]\.[a-z\.]{2,6}$/i;
25942598
* // => ''
25952599
* domain('http://segment.io/baz');
25962600
* // => 'segment.io'
2597-
*
2601+
*
25982602
* @param {String} url
25992603
* @return {String}
26002604
* @api public
@@ -3288,7 +3292,7 @@ module.exports = Identify;
32883292
*
32893293
* Licensed under the MIT license:
32903294
* http://www.opensource.org/licenses/MIT
3291-
*
3295+
*
32923296
* Based on
32933297
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
32943298
* Digest Algorithm, as defined in RFC 1321.
@@ -4972,6 +4976,7 @@ module.exports = {
49724976
eventUploadPeriodMillis: 30 * 1000, // 30s
49734977
forceHttps: false,
49744978
includeGclid: false,
4979+
saveParamsReferrerOncePerSession: true
49754980
};
49764981

49774982
}, {"./language":29}],
@@ -4986,4 +4991,4 @@ module.exports = {
49864991
};
49874992

49884993
}, {}]}, {}, {"1":""})
4989-
);
4994+
);

amplitude.min.js

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

src/amplitude-client.js

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,15 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
9191
this._sessionId = now;
9292

9393
// only capture UTM params and referrer if new session
94-
if (this.options.includeUtm) {
95-
this._initUtmData();
96-
}
97-
if (this.options.includeReferrer) {
98-
this._saveReferrer(this._getReferrer());
99-
}
100-
if (this.options.includeGclid) {
101-
this._saveGclid();
94+
if (this.options.saveParamsReferrerOncePerSession) {
95+
this._trackParamsAndReferrer();
10296
}
10397
}
98+
99+
if (!this.options.saveParamsReferrerOncePerSession) {
100+
this._trackParamsAndReferrer();
101+
}
102+
104103
this._lastEventTime = now;
105104
_saveCookieData(this);
106105

@@ -135,6 +134,21 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
135134
}
136135
};
137136

137+
/**
138+
* @private
139+
*/
140+
AmplitudeClient.prototype._trackParamsAndReferrer = function _trackParamsAndReferrer() {
141+
if (this.options.includeUtm) {
142+
this._initUtmData();
143+
}
144+
if (this.options.includeReferrer) {
145+
this._saveReferrer(this._getReferrer());
146+
}
147+
if (this.options.includeGclid) {
148+
this._saveGclid(this._getUrlParams());
149+
}
150+
};
151+
138152
/**
139153
* Parse and validate user specified config values and overwrite existing option value
140154
* DEFAULT_OPTIONS provides list of all config keys that are modifiable, as well as expected types for values
@@ -435,17 +449,18 @@ var _saveCookieData = function _saveCookieData(scope) {
435449
* @private
436450
*/
437451
AmplitudeClient.prototype._initUtmData = function _initUtmData(queryParams, cookieParams) {
438-
queryParams = queryParams || location.search;
452+
queryParams = queryParams || this._getUrlParams();
439453
cookieParams = cookieParams || this.cookieStorage.get('__utmz');
440454
var utmProperties = getUtmData(cookieParams, queryParams);
441-
_sendUserPropertiesOncePerSession(this, Constants.UTM_PROPERTIES, utmProperties);
455+
_sendParamsReferrerUserProperties(this, utmProperties);
442456
};
443457

444458
/**
445-
* Since user properties are propagated on server, only send once per session, don't need to send with every event
459+
* The calling function should determine when it is appropriate to send these user properties. This function
460+
* will no longer contain any session storage checking logic.
446461
* @private
447462
*/
448-
var _sendUserPropertiesOncePerSession = function _sendUserPropertiesOncePerSession(scope, storageKey, userProperties) {
463+
var _sendParamsReferrerUserProperties = function _sendParamsReferrerUserProperties(scope, userProperties) {
449464
if (type(userProperties) !== 'object' || Object.keys(userProperties).length === 0) {
450465
return;
451466
}
@@ -455,20 +470,7 @@ var _sendUserPropertiesOncePerSession = function _sendUserPropertiesOncePerSessi
455470
for (var key in userProperties) {
456471
if (userProperties.hasOwnProperty(key)) {
457472
identify.setOnce('initial_' + key, userProperties[key]);
458-
}
459-
}
460-
461-
// only save userProperties if not already in sessionStorage under key or if storage disabled
462-
var hasSessionStorage = utils.sessionStorageEnabled();
463-
if ((hasSessionStorage && !(scope._getFromStorage(sessionStorage, storageKey))) || !hasSessionStorage) {
464-
for (var property in userProperties) {
465-
if (userProperties.hasOwnProperty(property)) {
466-
identify.set(property, userProperties[property]);
467-
}
468-
}
469-
470-
if (hasSessionStorage) {
471-
scope._setInStorage(sessionStorage, storageKey, JSON.stringify(userProperties));
473+
identify.set(key, userProperties[key]);
472474
}
473475
}
474476

@@ -482,18 +484,24 @@ AmplitudeClient.prototype._getReferrer = function _getReferrer() {
482484
return document.referrer;
483485
};
484486

487+
/**
488+
* @private
489+
*/
490+
AmplitudeClient.prototype._getUrlParams = function _getUrlParams() {
491+
return location.search;
492+
};
493+
485494
/**
486495
* Try to fetch Google Gclid from url params.
487496
* @private
488497
*/
489-
AmplitudeClient.prototype._saveGclid = function _saveGclid(queryParams) {
490-
queryParams = queryParams || location.search;
491-
var gclid = utils.getQueryParam('gclid', queryParams);
498+
AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) {
499+
var gclid = utils.getQueryParam('gclid', urlParams);
492500
if (utils.isEmptyString(gclid)) {
493501
return;
494502
}
495503
var gclidProperties = {'gclid': gclid};
496-
_sendUserPropertiesOncePerSession(this, Constants.GCLID, gclidProperties);
504+
_sendParamsReferrerUserProperties(this, gclidProperties);
497505
};
498506

499507
/**
@@ -524,7 +532,7 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
524532
'referrer': referrer,
525533
'referring_domain': this._getReferringDomain(referrer)
526534
};
527-
_sendUserPropertiesOncePerSession(this, Constants.REFERRER, referrerInfo);
535+
_sendParamsReferrerUserProperties(this, referrerInfo);
528536
};
529537

530538
/**

src/constants.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ module.exports = {
1010
LAST_EVENT_TIME: 'amplitude_lastEventTime',
1111
LAST_IDENTIFY_ID: 'amplitude_lastIdentifyId',
1212
LAST_SEQUENCE_NUMBER: 'amplitude_lastSequenceNumber',
13-
REFERRER: 'amplitude_referrer',
1413
SESSION_ID: 'amplitude_sessionId',
15-
UTM_PROPERTIES: 'amplitude_utm_properties',
16-
GCLID: 'amplitude_gclid',
1714

1815
// Used in cookie as well
1916
DEVICE_ID: 'amplitude_deviceId',

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ module.exports = {
2222
eventUploadPeriodMillis: 30 * 1000, // 30s
2323
forceHttps: false,
2424
includeGclid: false,
25+
saveParamsReferrerOncePerSession: true
2526
};

0 commit comments

Comments
 (0)