@@ -42,7 +42,8 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
42
42
this . _q = [ ] ; // queue for proxied functions before script load
43
43
this . _sending = false ;
44
44
this . _updateScheduled = false ;
45
- this . _onInit = [ ] ;
45
+ this . _onInitCallbacks = [ ] ;
46
+ this . _onNewSessionStartCallbacks = [ ] ;
46
47
47
48
// event meta data
48
49
this . _eventId = 0 ;
@@ -167,7 +168,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
167
168
null ;
168
169
169
170
var now = new Date ( ) . getTime ( ) ;
170
- if ( ! this . _sessionId || ! this . _lastEventTime || now - this . _lastEventTime > this . options . sessionTimeout ) {
171
+ const startNewSession =
172
+ ! this . _sessionId || ! this . _lastEventTime || now - this . _lastEventTime > this . options . sessionTimeout ;
173
+ if ( startNewSession ) {
171
174
if ( this . options . unsetParamsReferrerOnNewSession ) {
172
175
this . _unsetUTMParams ( ) ;
173
176
}
@@ -197,11 +200,15 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
197
200
198
201
this . _sendEventsIfReady ( ) ; // try sending unsent events
199
202
200
- for ( let i = 0 ; i < this . _onInit . length ; i ++ ) {
201
- this . _onInit [ i ] ( this ) ;
203
+ for ( let i = 0 ; i < this . _onInitCallbacks . length ; i ++ ) {
204
+ this . _onInitCallbacks [ i ] ( this ) ;
202
205
}
203
- this . _onInit = [ ] ;
206
+ this . _onInitCallbacks = [ ] ;
204
207
this . _isInitialized = true ;
208
+
209
+ if ( startNewSession ) {
210
+ this . _runNewSessionStartCallbacks ( ) ;
211
+ }
205
212
} ;
206
213
207
214
if ( this . options . saveEvents ) {
@@ -212,6 +219,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
212
219
. map ( ( event ) => ( { event } ) )
213
220
. concat ( this . _unsentIdentifys ) ;
214
221
}
222
+ if ( opt_config && opt_config . onNewSessionStart ) {
223
+ this . onNewSessionStart ( this . options . onNewSessionStart ) ;
224
+ }
215
225
initFromStorage ( ) ;
216
226
this . runQueuedFunctions ( ) ;
217
227
if ( type ( opt_callback ) === 'function' ) {
@@ -250,6 +260,12 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
250
260
}
251
261
} ;
252
262
263
+ AmplitudeClient . prototype . _runNewSessionStartCallbacks = function ( ) {
264
+ for ( let i = 0 ; i < this . _onNewSessionStartCallbacks . length ; i ++ ) {
265
+ this . _onNewSessionStartCallbacks [ i ] ( this ) ;
266
+ }
267
+ } ;
268
+
253
269
AmplitudeClient . prototype . deleteLowerLevelDomainCookies = function ( ) {
254
270
const host = getHost ( ) ;
255
271
@@ -470,14 +486,22 @@ AmplitudeClient.prototype.isNewSession = function isNewSession() {
470
486
* Add callbacks to call after init. Useful for users who load Amplitude through a snippet.
471
487
* @public
472
488
*/
473
- AmplitudeClient . prototype . onInit = function ( callback ) {
489
+ AmplitudeClient . prototype . onInit = function onInit ( callback ) {
474
490
if ( this . _isInitialized ) {
475
491
callback ( this ) ;
476
492
} else {
477
- this . _onInit . push ( callback ) ;
493
+ this . _onInitCallbacks . push ( callback ) ;
478
494
}
479
495
} ;
480
496
497
+ /**
498
+ * Add callbacks to call after new session start.
499
+ * @public
500
+ */
501
+ AmplitudeClient . prototype . onNewSessionStart = function onNewSessionStart ( callback ) {
502
+ this . _onNewSessionStartCallbacks . push ( callback ) ;
503
+ } ;
504
+
481
505
/**
482
506
* Returns the id of the current session.
483
507
* @public
@@ -878,6 +902,7 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId, startNewSession
878
902
}
879
903
this . _newSession = true ;
880
904
this . _sessionId = new Date ( ) . getTime ( ) ;
905
+ this . _runNewSessionStartCallbacks ( ) ;
881
906
882
907
// only capture UTM params and referrer if new session
883
908
if ( this . options . saveParamsReferrerOncePerSession ) {
0 commit comments