@@ -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 */
918932AmplitudeClient . 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+ ) ;
0 commit comments