Skip to content

Commit 57848d3

Browse files
authored
[CSL 2370] Persist new beacon status for beacon and clientjs scenarios (#39)
* Persist new beacon status for beacon and clientjs scenarios * Change variable name * Change name * Add tests * Add tests
1 parent 5f8dd9d commit 57848d3

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

spec/001-initialization.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ describe('ConstructorioID', function () {
286286
expect(session.session_id).to.equal(43);
287287
expect(session.session_is_new).to.be.a('boolean');
288288
expect(session.session_is_new).to.equal(true);
289+
expect(session.new_to_beacon).to.equal(null);
289290
});
290291

291292
it('should set session_is_new to false if the session is not new and storage location is set to cookie', function () {
@@ -302,18 +303,21 @@ describe('ConstructorioID', function () {
302303
expect(session.session_id).to.equal(43);
303304
expect(session.session_is_new).to.be.a('boolean');
304305
expect(session.session_is_new).to.equal(true);
306+
expect(session.new_to_beacon).to.equal(null);
305307
});
306308

307309
it('should set session_is_new to true if there is no local storage data', function () {
308310
var session = new ConstructorioID();
309311
expect(session.session_is_new).to.be.a('boolean');
310312
expect(session.session_is_new).to.equal(true);
313+
expect(session.new_to_beacon).to.equal(null);
311314
});
312315

313316
it('should set session_is_new to true if there is no cookie data and storage location is set to cookie', function () {
314317
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
315318
expect(session.session_is_new).to.be.a('boolean');
316319
expect(session.session_is_new).to.equal(true);
320+
expect(session.new_to_beacon).to.equal(null);
317321
});
318322

319323
it('should set the user agent', function () {
@@ -364,5 +368,10 @@ describe('ConstructorioID', function () {
364368
var session = new ConstructorioID();
365369
expect(session.on_node).to.be.true;
366370
});
371+
372+
it('should not set new_to_beacon', function () {
373+
var session = new ConstructorioID();
374+
expect(session.new_to_beacon).to.be.null;
375+
});
367376
});
368377
});

spec/005-storage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ describe('ConstructorioID', function () {
107107
expect(set_local_object.getCall(0).args[1]).to.equal(43);
108108
expect(set_local_object.getCall(1).args[1].sessionId).to.equal(43);
109109
expect(set_local_object.getCall(1).args[1].lastTime).to.be.at.least(now);
110+
expect(set_local_object.getCall(1).args[1].newToBeacon).to.be.true;
110111

111112
set_local_object.restore();
112113
});
@@ -126,6 +127,7 @@ describe('ConstructorioID', function () {
126127
expect(JSON.parse(set_cookie.getCall(0).args[1])).to.equal(43);
127128
expect(JSON.parse(set_cookie.getCall(1).args[1]).sessionId).to.equal(43);
128129
expect(JSON.parse(set_cookie.getCall(1).args[1]).lastTime).to.be.at.least(now);
130+
expect(JSON.parse(set_cookie.getCall(1).args[1]).newToBeacon).to.be.true;
129131

130132
set_cookie.restore();
131133
});

src/constructorio-id.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
local_name_session_data: '_constructorio_search_session',
2020
on_node: typeof window === 'undefined',
2121
session_is_new: null,
22+
new_to_beacon: null,
2223
client_id_storage_location: 'cookie',
2324
session_id_storage_location: 'local'
2425
};
@@ -245,20 +246,29 @@
245246
this.session_id = sessionId;
246247
this.session_is_new = sessionData && sessionDataId === sessionId ? false : true;
247248

249+
// persist new status for when ciojs-client is instantiated before beacon
250+
if (sessionData && sessionData.newToBeacon) {
251+
this.new_to_beacon = true;
252+
}
253+
254+
const storedData = {
255+
sessionId: sessionId,
256+
lastTime: now
257+
};
258+
259+
// persist new status for when ciojs-client is instantiated before beacon
260+
if (this.session_is_new) {
261+
storedData.newToBeacon = true;
262+
}
263+
248264
if (this.session_id_storage_location === 'local') {
249265
this.set_local_object(this.local_name_session_id, sessionId);
250-
this.set_local_object(this.local_name_session_data, {
251-
sessionId: sessionId,
252-
lastTime: now
253-
});
266+
this.set_local_object(this.local_name_session_data, storedData);
254267
}
255268

256269
if (this.session_id_storage_location === 'cookie') {
257270
this.set_cookie(this.cookie_name_session_id, sessionId);
258-
this.set_cookie(this.cookie_name_session_data, JSON.stringify({
259-
sessionId: sessionId,
260-
lastTime: now
261-
}));
271+
this.set_cookie(this.cookie_name_session_data, JSON.stringify(storedData));
262272
}
263273

264274
return sessionId;

0 commit comments

Comments
 (0)