Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove usages of Persistence First/Last Seen Times #876

Open
wants to merge 2 commits into
base: refactor/SQDSDKS-6347-migrate-persistence-to-store
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -1238,10 +1238,10 @@ export default function Identity(mpInstance) {
return isLoggedIn;
},
getLastSeenTime: function() {
return mpInstance._Persistence.getLastSeenTime(mpid);
return mpInstance._Store.getLastSeenTime(mpid);
},
getFirstSeenTime: function() {
return mpInstance._Persistence.getFirstSeenTime(mpid);
return mpInstance._Store.getFirstSeenTime(mpid);
},
/**
* Get user audiences
Expand Down Expand Up @@ -1494,15 +1494,15 @@ export default function Identity(mpInstance) {

if (prevUser) {
// https://go.mparticle.com/work/SQDSDKS-6329
mpInstance._Persistence.setLastSeenTime(previousMPID);
mpInstance._Store.setLastSeenTime(previousMPID);
}

const mpidIsNotInCookies = !mpInstance._Persistence.getFirstSeenTime(
const mpidIsNotInCookies = !mpInstance._Store.getFirstSeenTime(
identityApiResult.mpid
);

// https://go.mparticle.com/work/SQDSDKS-6329
mpInstance._Persistence.setFirstSeenTime(identityApiResult.mpid);
mpInstance._Store.setFirstSeenTime(identityApiResult.mpid);

if (xhr.status === 200) {
if (getFeatureFlag(CacheIdentity)) {
Expand Down Expand Up @@ -1543,7 +1543,7 @@ export default function Identity(mpInstance) {
prevUser &&
identityApiResult.mpid === prevUserMPID
) {
mpInstance._Persistence.setFirstSeenTime(
mpInstance._Store.setFirstSeenTime(
identityApiResult.mpid
);
}
Expand Down
4 changes: 0 additions & 4 deletions src/persistence.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ export interface IPersistence {
savePersistence(persistance: IPersistenceMinified): void;
getPersistence(): IPersistenceMinified;
getConsentState(mpid: MPID): ConsentState | null;
getFirstSeenTime(mpid: MPID): string | null;
setFirstSeenTime(mpid: MPID, time: number): void;
getLastSeenTime(mpid: MPID): number | null;
setLastSeenTime(mpid: MPID, time: number): void;
getDeviceId(): string;
setDeviceId(guid: string): void;
resetPersistence(): void;
Expand Down
72 changes: 0 additions & 72 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,78 +981,6 @@ export default function _Persistence(mpInstance) {
}
};

this.getFirstSeenTime = function(mpid) {
if (!mpid) {
return null;
}
var persistence = self.getPersistence();
if (persistence && persistence[mpid] && persistence[mpid].fst) {
return persistence[mpid].fst;
} else {
return null;
}
};

/**
* set the "first seen" time for a user. the time will only be set once for a given
* mpid after which subsequent calls will be ignored
*/
this.setFirstSeenTime = function(mpid, time) {
if (!mpid) {
return;
}
// https://go.mparticle.com/work/SQDSDKS-6329
if (!time) {
time = new Date().getTime();
}
var persistence = self.getPersistence();
if (persistence) {
if (!persistence[mpid]) {
persistence[mpid] = {};
}
if (!persistence[mpid].fst) {
persistence[mpid].fst = time;
self.savePersistence(persistence);
}
}
};

/**
* returns the "last seen" time for a user. If the mpid represents the current user, the
* return value will always be the current time, otherwise it will be to stored "last seen"
* time
*/
this.getLastSeenTime = function(mpid) {
if (!mpid) {
return null;
}
if (mpid === mpInstance.Identity.getCurrentUser().getMPID()) {
//if the mpid is the current user, its last seen time is the current time
return new Date().getTime();
} else {
var persistence = self.getPersistence();
if (persistence && persistence[mpid] && persistence[mpid].lst) {
return persistence[mpid].lst;
}
return null;
}
};

this.setLastSeenTime = function(mpid, time) {
if (!mpid) {
return;
}
// https://go.mparticle.com/work/SQDSDKS-6329
if (!time) {
time = new Date().getTime();
}
var persistence = self.getPersistence();
if (persistence && persistence[mpid]) {
persistence[mpid].lst = time;
self.savePersistence(persistence);
}
};

this.getDeviceId = function() {
return mpInstance._Store.deviceId;
};
Expand Down
12 changes: 11 additions & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,24 @@ export default function Store(
this.getFirstSeenTime = (mpid: MPID) =>
this._getFromPersistence<number>(mpid, 'fst');

// set the "first seen" time for a user. the time will only be set once for a given
// mpid after which subsequent calls will be ignored
this.setFirstSeenTime = (mpid: MPID, _time?: number) => {
if (!mpid) {
return;
}

const time = _time || new Date().getTime();

this._setPersistence(mpid, 'fst', time);
this.syncPersistenceData();

if(!this.persistenceData[mpid]) {
this.persistenceData[mpid] = {};
}
if(!this.persistenceData[mpid].fst) {
this.persistenceData[mpid].fst = time;
this._setPersistence(mpid, 'fst', time);
}
};

this.getLastSeenTime = (mpid: MPID): number => {
Expand Down
48 changes: 48 additions & 0 deletions test/src/tests-identity.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Constants from '../../src/constants';
import Utils from './config/utils';
import sinon from 'sinon';
import { expect } from 'chai';
import fetchMock from 'fetch-mock/esm/client';
import { urls, apiKey,
testMPID,
MPConfig,
workspaceCookieName,
mParticle,
} from './config/constants';

const getLocalStorage = Utils.getLocalStorage,
Expand Down Expand Up @@ -3084,6 +3086,52 @@ describe('identity', function() {
done();
});

describe('#identify', function() {
it("should set fst when the user does not change, after an identify request", function(done) {
mParticle._resetForTests(MPConfig);

const cookies = JSON.stringify({
gs: {
sid: 'fst Test',
les: new Date().getTime(),
},
current: {},
cu: 'current',
});

mockServer.respondWith(urls.identify, [
200,
{},
JSON.stringify({ mpid: 'current', is_logged_in: false }),
]);

// We set the cookies because the init process will load cookies into
// our in-memory store, which we will check before and after the identify request
setCookie(workspaceCookieName, cookies, true);
mParticle.config.useCookieStorage = true;


// As part of init, there is a call to Identity.Identify. However, in this test case,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// we are starting with the assumption that there is a current user, and that user has
// both a sessionId (sid) and lastEventSent (les), which causes that initial identify call
// to be skipped. By manually forcing an identify call below, we can test that
// identify correctly calls setFirstSeenTime to set a value;
mParticle.init(apiKey, mParticle.config);

expect(
mParticle.getInstance()._Store.getFirstSeenTime('current')
).to.equal(null);
Comment on lines +3121 to +3123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try the following? Comment out sid and les from the cookies above (line 3095-3096 in this commit)

Does the test in 3115 fail?

I think it would. Because normally init will call identify, and the reason it is not calling it here, and you have to manually call it below this line, is because there is an sid, and/or les. If you step through, I think it will skip the identify call because of that.

If the above is in fact the case, we should add a comment above mParticle.init to explain why it isn't called, to help future developers out.


mParticle.Identity.identify();

expect(
mParticle.getInstance()._Store.getFirstSeenTime('current')
).to.not.equal(null);

done();
});
});

describe('identity caching', function() {
afterEach(function() {
sinon.restore();
Expand Down
Loading
Loading