From 5aa11363abdec96d1d6d21f782c9fd2a673b3226 Mon Sep 17 00:00:00 2001 From: Maxime Preaux Date: Tue, 1 Apr 2025 09:25:07 +0200 Subject: [PATCH 1/2] Fixed the identifyRequest intake when persistent data exists --- dist/mparticle.common.js | 8 ++++++-- dist/mparticle.esm.js | 8 ++++++-- dist/mparticle.js | 18 +++++++++++++++++- src/sessionManager.ts | 26 +++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/dist/mparticle.common.js b/dist/mparticle.common.js index 7d8a8faa4..23adeaeeb 100644 --- a/dist/mparticle.common.js +++ b/dist/mparticle.common.js @@ -508,8 +508,12 @@ if(!isEmpty(k)&&(isEmpty(o)||(i=!0),!(i&&d)))// If MPID is new to cookies, we sh return !b||new Date().getTime()>new Date(b).getTime()+a*DAYS_IN_MILLISECONDS;// Otherwise, compare the last sync date to determine if it should do a cookie sync again }; -var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){if(a._Store.sessionId){var c=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+c))b.endSession(),b.startNewSession();else {// https://go.mparticle.com/work/SQDSDKS-6045 -var d=a._Persistence.getPersistence();d&&!d.cu&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab +var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){var c;if(a._Store.sessionId){var d=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+d))b.endSession(),b.startNewSession();else {var e=a.Identity.getCurrentUser(),f=!1;if(e){// If userIdentities is an empty object but identifyRequest +// has at least a customerid, we will force the identify() +// call, regardless of whether any user data is already +// persisted. +var g=null===(c=e.getUserIdentities())||void 0===c?void 0:c.userIdentities,h=null!=g&&"string"==typeof g.customerid,i=a._Store.SDKConfig.identifyRequest,j=null!=i&&"string"==typeof i.userIdentities.customerid;!h&&j&&(f=!0);}// https://go.mparticle.com/work/SQDSDKS-6045 +var k=a._Persistence.getPersistence();(f||k&&!k.cu)&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab if(k.gs.sid&&a._Store.sessionId!==k.gs.sid&&(a._Store.sessionId=k.gs.sid),null===(g=null===k||void 0===k?void 0:k.gs)||void 0===g?void 0:g.les){i=6e4*a._Store.SDKConfig.sessionTimeout;var l=new Date().getTime();j=l-k.gs.les,jnew Date(b).getTime()+a*DAYS_IN_MILLISECONDS;// Otherwise, compare the last sync date to determine if it should do a cookie sync again }; -var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){if(a._Store.sessionId){var c=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+c))b.endSession(),b.startNewSession();else {// https://go.mparticle.com/work/SQDSDKS-6045 -var d=a._Persistence.getPersistence();d&&!d.cu&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab +var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){var c;if(a._Store.sessionId){var d=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+d))b.endSession(),b.startNewSession();else {var e=a.Identity.getCurrentUser(),f=!1;if(e){// If userIdentities is an empty object but identifyRequest +// has at least a customerid, we will force the identify() +// call, regardless of whether any user data is already +// persisted. +var g=null===(c=e.getUserIdentities())||void 0===c?void 0:c.userIdentities,h=null!=g&&"string"==typeof g.customerid,i=a._Store.SDKConfig.identifyRequest,j=null!=i&&"string"==typeof i.userIdentities.customerid;!h&&j&&(f=!0);}// https://go.mparticle.com/work/SQDSDKS-6045 +var k=a._Persistence.getPersistence();(f||k&&!k.cu)&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab if(k.gs.sid&&a._Store.sessionId!==k.gs.sid&&(a._Store.sessionId=k.gs.sid),null===(g=null===k||void 0===k?void 0:k.gs)||void 0===g?void 0:g.les){i=6e4*a._Store.SDKConfig.sessionTimeout;var l=new Date().getTime();j=l-k.gs.les,j new Date(mpInstance._Store.dateLastEventSent.getTime() + sessionTimeoutInMilliseconds)) { self.endSession(); self.startNewSession(); } else { + var user = mpInstance.Identity.getCurrentUser(); + var needToIdentify = false; + if (user) { + // If userIdentities is an empty object but identifyRequest + // has at least a customerid, we will force the identify() + // call, regardless of whether any user data is already + // persisted. + var storedUserIdentities = (_a = user.getUserIdentities()) === null || _a === void 0 ? void 0 : _a.userIdentities; + var hasStoredCustomerId = storedUserIdentities != null && typeof storedUserIdentities.customerid === "string"; + var identifyRequest = mpInstance._Store.SDKConfig.identifyRequest; + var identifyRequestHasCustomerId = identifyRequest != null && typeof identifyRequest.userIdentities.customerid === "string"; + if (!hasStoredCustomerId && identifyRequestHasCustomerId) { + needToIdentify = true; + } + } // https://go.mparticle.com/work/SQDSDKS-6045 var persistence = mpInstance._Persistence.getPersistence(); - if (persistence && !persistence.cu) { + if (needToIdentify || persistence && !persistence.cu) { // https://go.mparticle.com/work/SQDSDKS-6323 mpInstance.Identity.identify(mpInstance._Store.SDKConfig.identifyRequest, mpInstance._Store.SDKConfig.identityCallback); mpInstance._Store.identifyCalled = true; diff --git a/src/sessionManager.ts b/src/sessionManager.ts index cf96cfc79..d1c0ca8ef 100644 --- a/src/sessionManager.ts +++ b/src/sessionManager.ts @@ -45,9 +45,33 @@ export default function SessionManager( self.endSession(); self.startNewSession(); } else { + const user = mpInstance.Identity.getCurrentUser() + let needToIdentify = false + + if (user) { + // If userIdentities is an empty object but identifyRequest + // has at least a customerid, we will force the identify() + // call, regardless of whether any user data is already + // persisted. + const storedUserIdentities = user + .getUserIdentities()?.userIdentities + const hasStoredCustomerId = + storedUserIdentities != null && + typeof storedUserIdentities.customerid === "string" + + const identifyRequest = mpInstance._Store.SDKConfig.identifyRequest + const identifyRequestHasCustomerId = + identifyRequest != null && + typeof identifyRequest.userIdentities.customerid === "string" + + if (!hasStoredCustomerId && identifyRequestHasCustomerId) { + needToIdentify = true + } + } + // https://go.mparticle.com/work/SQDSDKS-6045 const persistence: IPersistenceMinified = mpInstance._Persistence.getPersistence(); - if (persistence && !persistence.cu) { + if (needToIdentify || (persistence && !persistence.cu)) { // https://go.mparticle.com/work/SQDSDKS-6323 mpInstance.Identity.identify( mpInstance._Store.SDKConfig.identifyRequest, From 03462171fc4b521129d63cefa7c84070dadce6c6 Mon Sep 17 00:00:00 2001 From: Maxime Preaux Date: Wed, 16 Apr 2025 10:12:37 +0200 Subject: [PATCH 2/2] Remove dist changes and refactor new code into a function --- dist/mparticle.common.js | 8 ++---- dist/mparticle.esm.js | 8 ++---- dist/mparticle.js | 18 +----------- src/sessionManager.ts | 59 +++++++++++++++++++++++----------------- 4 files changed, 39 insertions(+), 54 deletions(-) diff --git a/dist/mparticle.common.js b/dist/mparticle.common.js index 23adeaeeb..7d8a8faa4 100644 --- a/dist/mparticle.common.js +++ b/dist/mparticle.common.js @@ -508,12 +508,8 @@ if(!isEmpty(k)&&(isEmpty(o)||(i=!0),!(i&&d)))// If MPID is new to cookies, we sh return !b||new Date().getTime()>new Date(b).getTime()+a*DAYS_IN_MILLISECONDS;// Otherwise, compare the last sync date to determine if it should do a cookie sync again }; -var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){var c;if(a._Store.sessionId){var d=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+d))b.endSession(),b.startNewSession();else {var e=a.Identity.getCurrentUser(),f=!1;if(e){// If userIdentities is an empty object but identifyRequest -// has at least a customerid, we will force the identify() -// call, regardless of whether any user data is already -// persisted. -var g=null===(c=e.getUserIdentities())||void 0===c?void 0:c.userIdentities,h=null!=g&&"string"==typeof g.customerid,i=a._Store.SDKConfig.identifyRequest,j=null!=i&&"string"==typeof i.userIdentities.customerid;!h&&j&&(f=!0);}// https://go.mparticle.com/work/SQDSDKS-6045 -var k=a._Persistence.getPersistence();(f||k&&!k.cu)&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab +var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){if(a._Store.sessionId){var c=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+c))b.endSession(),b.startNewSession();else {// https://go.mparticle.com/work/SQDSDKS-6045 +var d=a._Persistence.getPersistence();d&&!d.cu&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab if(k.gs.sid&&a._Store.sessionId!==k.gs.sid&&(a._Store.sessionId=k.gs.sid),null===(g=null===k||void 0===k?void 0:k.gs)||void 0===g?void 0:g.les){i=6e4*a._Store.SDKConfig.sessionTimeout;var l=new Date().getTime();j=l-k.gs.les,jnew Date(b).getTime()+a*DAYS_IN_MILLISECONDS;// Otherwise, compare the last sync date to determine if it should do a cookie sync again }; -var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){var c;if(a._Store.sessionId){var d=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+d))b.endSession(),b.startNewSession();else {var e=a.Identity.getCurrentUser(),f=!1;if(e){// If userIdentities is an empty object but identifyRequest -// has at least a customerid, we will force the identify() -// call, regardless of whether any user data is already -// persisted. -var g=null===(c=e.getUserIdentities())||void 0===c?void 0:c.userIdentities,h=null!=g&&"string"==typeof g.customerid,i=a._Store.SDKConfig.identifyRequest,j=null!=i&&"string"==typeof i.userIdentities.customerid;!h&&j&&(f=!0);}// https://go.mparticle.com/work/SQDSDKS-6045 -var k=a._Persistence.getPersistence();(f||k&&!k.cu)&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab +var Messages$6=Constants.Messages;function SessionManager(a){var b=this;this.initialize=function(){if(a._Store.sessionId){var c=6e4*a._Store.SDKConfig.sessionTimeout;if(new Date>new Date(a._Store.dateLastEventSent.getTime()+c))b.endSession(),b.startNewSession();else {// https://go.mparticle.com/work/SQDSDKS-6045 +var d=a._Persistence.getPersistence();d&&!d.cu&&(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null);}}else b.startNewSession();},this.getSession=function(){return a.Logger.warning(generateDeprecationMessage("SessionManager.getSession()","SessionManager.getSessionId()")),this.getSessionId()},this.getSessionId=function(){return a._Store.sessionId},this.startNewSession=function(){if(a.Logger.verbose(Messages$6.InformationMessages.StartingNewSession),a._Helpers.canLog()){a._Store.sessionId=a._Helpers.generateUniqueId().toUpperCase();var c=a.Identity.getCurrentUser(),d=c?c.getMPID():null;if(d&&(a._Store.currentSessionMPIDs=[d]),!a._Store.sessionStartDate){var e=new Date;a._Store.sessionStartDate=e,a._Store.dateLastEventSent=e;}b.setSessionTimer(),a._Store.identifyCalled||(a.Identity.identify(a._Store.SDKConfig.identifyRequest,a._Store.SDKConfig.identityCallback),a._Store.identifyCalled=!0,a._Store.SDKConfig.identityCallback=null),a._Events.logEvent({messageType:Types.MessageType.SessionStart});}else a.Logger.verbose(Messages$6.InformationMessages.AbandonStartSession);},this.endSession=function(c){var d,e,f,g,h;if(a.Logger.verbose(Messages$6.InformationMessages.StartingEndSession),c)return a._Events.logEvent({messageType:Types.MessageType.SessionEnd}),a._Store.nullifySession(),void(null===(d=a._timeOnSiteTimer)||void 0===d?void 0:d.resetTimer());if(!a._Helpers.canLog())return a.Logger.verbose(Messages$6.InformationMessages.AbandonEndSession),void(null===(e=a._timeOnSiteTimer)||void 0===e?void 0:e.resetTimer());var i,j,k=a._Persistence.getPersistence();if(!k||k.gs&&!k.gs.sid)return a.Logger.verbose(Messages$6.InformationMessages.NoSessionToEnd),void(null===(f=a._timeOnSiteTimer)||void 0===f?void 0:f.resetTimer());// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab if(k.gs.sid&&a._Store.sessionId!==k.gs.sid&&(a._Store.sessionId=k.gs.sid),null===(g=null===k||void 0===k?void 0:k.gs)||void 0===g?void 0:g.les){i=6e4*a._Store.SDKConfig.sessionTimeout;var l=new Date().getTime();j=l-k.gs.les,j new Date(mpInstance._Store.dateLastEventSent.getTime() + sessionTimeoutInMilliseconds)) { self.endSession(); self.startNewSession(); } else { - var user = mpInstance.Identity.getCurrentUser(); - var needToIdentify = false; - if (user) { - // If userIdentities is an empty object but identifyRequest - // has at least a customerid, we will force the identify() - // call, regardless of whether any user data is already - // persisted. - var storedUserIdentities = (_a = user.getUserIdentities()) === null || _a === void 0 ? void 0 : _a.userIdentities; - var hasStoredCustomerId = storedUserIdentities != null && typeof storedUserIdentities.customerid === "string"; - var identifyRequest = mpInstance._Store.SDKConfig.identifyRequest; - var identifyRequestHasCustomerId = identifyRequest != null && typeof identifyRequest.userIdentities.customerid === "string"; - if (!hasStoredCustomerId && identifyRequestHasCustomerId) { - needToIdentify = true; - } - } // https://go.mparticle.com/work/SQDSDKS-6045 var persistence = mpInstance._Persistence.getPersistence(); - if (needToIdentify || persistence && !persistence.cu) { + if (persistence && !persistence.cu) { // https://go.mparticle.com/work/SQDSDKS-6323 mpInstance.Identity.identify(mpInstance._Store.SDKConfig.identifyRequest, mpInstance._Store.SDKConfig.identityCallback); mpInstance._Store.identifyCalled = true; diff --git a/src/sessionManager.ts b/src/sessionManager.ts index d1c0ca8ef..029896d64 100644 --- a/src/sessionManager.ts +++ b/src/sessionManager.ts @@ -10,6 +10,7 @@ const { Messages } = Constants; export interface ISessionManager { initialize: () => void; + isIdentifyNeeded: () => boolean; getSessionId: () => string; startNewSession: () => void; endSession: (override?: boolean) => void; @@ -45,33 +46,13 @@ export default function SessionManager( self.endSession(); self.startNewSession(); } else { - const user = mpInstance.Identity.getCurrentUser() - let needToIdentify = false - - if (user) { - // If userIdentities is an empty object but identifyRequest - // has at least a customerid, we will force the identify() - // call, regardless of whether any user data is already - // persisted. - const storedUserIdentities = user - .getUserIdentities()?.userIdentities - const hasStoredCustomerId = - storedUserIdentities != null && - typeof storedUserIdentities.customerid === "string" - - const identifyRequest = mpInstance._Store.SDKConfig.identifyRequest - const identifyRequestHasCustomerId = - identifyRequest != null && - typeof identifyRequest.userIdentities.customerid === "string" - - if (!hasStoredCustomerId && identifyRequestHasCustomerId) { - needToIdentify = true - } - } - // https://go.mparticle.com/work/SQDSDKS-6045 const persistence: IPersistenceMinified = mpInstance._Persistence.getPersistence(); - if (needToIdentify || (persistence && !persistence.cu)) { + + // If isIdentifyNeeded() returns `true`, we will force the + // identify() call, regardless of whether any user data is + // already persisted. + if (this.isIdentifyNeeded() || (persistence && !persistence.cu)) { // https://go.mparticle.com/work/SQDSDKS-6323 mpInstance.Identity.identify( mpInstance._Store.SDKConfig.identifyRequest, @@ -86,6 +67,34 @@ export default function SessionManager( } }; + /** + * Returns `true` if `userIdentities` is an empty object but the config's + * `identifyRequest` has at least a `customerid`. + */ + this.isIdentifyNeeded = function(): boolean { + const user = mpInstance.Identity.getCurrentUser() + let needsToIdentify = false + + if (user) { + const storedUserIdentities = user + .getUserIdentities()?.userIdentities + const hasStoredCustomerId = + storedUserIdentities != null && + typeof storedUserIdentities.customerid !== "undefined" + + const identifyRequest = mpInstance._Store.SDKConfig.identifyRequest + const identifyRequestHasCustomerId = + identifyRequest != null && + typeof identifyRequest.userIdentities.customerid !== "undefined" + + if (!hasStoredCustomerId && identifyRequestHasCustomerId) { + needsToIdentify = true + } + } + + return needsToIdentify + } + this.getSession = function(): string { mpInstance.Logger.warning( generateDeprecationMessage(