Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/sessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { Messages } = Constants;

export interface ISessionManager {
initialize: () => void;
isIdentifyNeeded: () => boolean;
getSessionId: () => string;
startNewSession: () => void;
endSession: (override?: boolean) => void;
Expand Down Expand Up @@ -47,7 +48,11 @@ export default function SessionManager(
} else {
// https://go.mparticle.com/work/SQDSDKS-6045
const persistence: IPersistenceMinified = mpInstance._Persistence.getPersistence();
if (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,
Expand All @@ -62,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(
Expand Down