@@ -572,16 +572,15 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
572
572
this . _sessionId = now ;
573
573
574
574
// 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 ( ) ;
583
577
}
584
578
}
579
+
580
+ if ( ! this . options . saveParamsReferrerOncePerSession ) {
581
+ this . _trackParamsAndReferrer ( ) ;
582
+ }
583
+
585
584
this . _lastEventTime = now ;
586
585
_saveCookieData ( this ) ;
587
586
@@ -616,6 +615,21 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
616
615
}
617
616
} ;
618
617
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
+
619
633
/**
620
634
* Parse and validate user specified config values and overwrite existing option value
621
635
* 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) {
916
930
* @private
917
931
*/
918
932
AmplitudeClient . prototype . _initUtmData = function _initUtmData ( queryParams , cookieParams ) {
919
- queryParams = queryParams || location . search ;
933
+ queryParams = queryParams || this . _getUrlParams ( ) ;
920
934
cookieParams = cookieParams || this . cookieStorage . get ( '__utmz' ) ;
921
935
var utmProperties = getUtmData ( cookieParams , queryParams ) ;
922
- _sendUserPropertiesOncePerSession ( this , Constants . UTM_PROPERTIES , utmProperties ) ;
936
+ _sendParamsReferrerUserProperties ( this , utmProperties ) ;
923
937
} ;
924
938
925
939
/**
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.
927
942
* @private
928
943
*/
929
- var _sendUserPropertiesOncePerSession = function _sendUserPropertiesOncePerSession ( scope , storageKey , userProperties ) {
944
+ var _sendParamsReferrerUserProperties = function _sendParamsReferrerUserProperties ( scope , userProperties ) {
930
945
if ( type ( userProperties ) !== 'object' || Object . keys ( userProperties ) . length === 0 ) {
931
946
return ;
932
947
}
@@ -936,20 +951,7 @@ var _sendUserPropertiesOncePerSession = function _sendUserPropertiesOncePerSessi
936
951
for ( var key in userProperties ) {
937
952
if ( userProperties . hasOwnProperty ( key ) ) {
938
953
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 ] ) ;
953
955
}
954
956
}
955
957
@@ -963,19 +965,24 @@ AmplitudeClient.prototype._getReferrer = function _getReferrer() {
963
965
return document . referrer ;
964
966
} ;
965
967
968
+ /**
969
+ * @private
970
+ */
971
+ AmplitudeClient . prototype . _getUrlParams = function _getUrlParams ( ) {
972
+ return location . search ;
973
+ } ;
974
+
966
975
/**
967
976
* Try to fetch Google Gclid from url params.
968
977
* @private
969
978
*/
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 ) ;
974
981
if ( utils . isEmptyString ( gclid ) ) {
975
982
return ;
976
983
}
977
984
var gclidProperties = { 'gclid' : gclid } ;
978
- _sendUserPropertiesOncePerSession ( this , Constants . GCLID , gclidProperties ) ;
985
+ _sendParamsReferrerUserProperties ( this , gclidProperties ) ;
979
986
} ;
980
987
981
988
/**
@@ -1006,7 +1013,7 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
1006
1013
'referrer' : referrer ,
1007
1014
'referring_domain' : this . _getReferringDomain ( referrer )
1008
1015
} ;
1009
- _sendUserPropertiesOncePerSession ( this , Constants . REFERRER , referrerInfo ) ;
1016
+ _sendParamsReferrerUserProperties ( this , referrerInfo ) ;
1010
1017
} ;
1011
1018
1012
1019
/**
@@ -1650,10 +1657,7 @@ module.exports = {
1650
1657
LAST_EVENT_TIME : 'amplitude_lastEventTime' ,
1651
1658
LAST_IDENTIFY_ID : 'amplitude_lastIdentifyId' ,
1652
1659
LAST_SEQUENCE_NUMBER : 'amplitude_lastSequenceNumber' ,
1653
- REFERRER : 'amplitude_referrer' ,
1654
1660
SESSION_ID : 'amplitude_sessionId' ,
1655
- UTM_PROPERTIES : 'amplitude_utm_properties' ,
1656
- GCLID : 'amplitude_gclid' ,
1657
1661
1658
1662
// Used in cookie as well
1659
1663
DEVICE_ID : 'amplitude_deviceId' ,
@@ -2580,12 +2584,12 @@ var regexp = /[a-z0-9][a-z0-9\-]*[a-z0-9]\.[a-z\.]{2,6}$/i;
2580
2584
2581
2585
/**
2582
2586
* Get the top domain.
2583
- *
2587
+ *
2584
2588
* Official Grammar: http://tools.ietf.org/html/rfc883#page-56
2585
2589
* Look for tlds with up to 2-6 characters.
2586
- *
2590
+ *
2587
2591
* Example:
2588
- *
2592
+ *
2589
2593
* domain('http://localhost:3000/baz');
2590
2594
* // => ''
2591
2595
* 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;
2594
2598
* // => ''
2595
2599
* domain('http://segment.io/baz');
2596
2600
* // => 'segment.io'
2597
- *
2601
+ *
2598
2602
* @param {String } url
2599
2603
* @return {String }
2600
2604
* @api public
@@ -3288,7 +3292,7 @@ module.exports = Identify;
3288
3292
*
3289
3293
* Licensed under the MIT license:
3290
3294
* http://www.opensource.org/licenses/MIT
3291
- *
3295
+ *
3292
3296
* Based on
3293
3297
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
3294
3298
* Digest Algorithm, as defined in RFC 1321.
@@ -4972,6 +4976,7 @@ module.exports = {
4972
4976
eventUploadPeriodMillis : 30 * 1000 , // 30s
4973
4977
forceHttps : false ,
4974
4978
includeGclid : false ,
4979
+ saveParamsReferrerOncePerSession : true
4975
4980
} ;
4976
4981
4977
4982
} , { "./language" :29 } ] ,
@@ -4986,4 +4991,4 @@ module.exports = {
4986
4991
} ;
4987
4992
4988
4993
} , { } ] } , { } , { "1" :"" } )
4989
- ) ;
4994
+ ) ;
0 commit comments