Skip to content

Commit b6184e2

Browse files
authored
AdagioRtdProvider: add support for AB Testing (prebid#11935)
1 parent 42e55d0 commit b6184e2

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

modules/adagioRtdProvider.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,34 @@ const _SESSION = (function() {
7373

7474
storage.getDataFromLocalStorage('adagio', (storageValue) => {
7575
// session can be an empty object
76-
const { rnd, new: isNew, vwSmplg, vwSmplgNxt, lastActivityTime } = _internal.getSessionFromLocalStorage(storageValue);
76+
const { rnd, new: isNew = false, vwSmplg, vwSmplgNxt, lastActivityTime, id, testName, testVersion, initiator } = _internal.getSessionFromLocalStorage(storageValue);
77+
78+
// isNew can be `true` if the session has been initialized by the A/B test snippet (external)
79+
const isNewSess = (initiator === 'snippet') ? isNew : isNewSession(lastActivityTime);
7780

7881
data.session = {
7982
rnd,
80-
new: isNew || false, // legacy: `new` was used but the choosen name is not good.
83+
new: isNewSess, // legacy: `new` was used but the choosen name is not good.
8184
// Don't use values if they are not defined.
8285
...(vwSmplg !== undefined && { vwSmplg }),
8386
...(vwSmplgNxt !== undefined && { vwSmplgNxt }),
84-
...(lastActivityTime !== undefined && { lastActivityTime })
87+
...(lastActivityTime !== undefined && { lastActivityTime }),
88+
...(id !== undefined && { id }),
89+
...(testName !== undefined && { testName }),
90+
...(testVersion !== undefined && { testVersion }),
91+
...(initiator !== undefined && { initiator }),
8592
};
8693

87-
if (isNewSession(lastActivityTime)) {
94+
// `initiator` is a pseudo flag used to know if the session has been initialized by the A/B test snippet (external).
95+
// If the AB Test snippet has not been used, then `initiator` value is `adgjs` or `undefined`.
96+
// The check on `testName` is used to ensure that the A/B test values are removed.
97+
if (initiator !== 'snippet' && (isNewSess || testName)) {
8898
data.session.new = true;
99+
data.session.id = generateUUID();
89100
data.session.rnd = Math.random();
101+
// Ensure that the A/B test values are removed.
102+
delete data.session.testName;
103+
delete data.session.testVersion;
90104
}
91105

92106
_internal.getAdagioNs().queue.push({

test/spec/modules/adagioRtdProvider_spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ describe('Adagio Rtd Provider', function () {
118118
describe('store session data in localStorage', function () {
119119
const session = {
120120
lastActivityTime: 1714116520700,
121+
id: 'uid-1234',
121122
rnd: 0.5697,
122123
vwSmplg: 0.1,
123124
vwSmplgNxt: 0.1
@@ -128,6 +129,7 @@ describe('Adagio Rtd Provider', function () {
128129
sandbox.stub(storage, 'getDataFromLocalStorage').callsArgWith(1, storageValue);
129130
sandbox.stub(Date, 'now').returns(1714116520710);
130131
sandbox.stub(Math, 'random').returns(0.8);
132+
sandbox.stub(utils, 'generateUUID').returns('uid-1234');
131133

132134
const spy = sandbox.spy(_internal.getAdagioNs().queue, 'push')
133135

@@ -136,6 +138,7 @@ describe('Adagio Rtd Provider', function () {
136138
const expected = {
137139
session: {
138140
new: true,
141+
id: utils.generateUUID(),
139142
rnd: Math.random()
140143
}
141144
}
@@ -176,6 +179,7 @@ describe('Adagio Rtd Provider', function () {
176179
sandbox.stub(Date, 'now').returns(1715679344351);
177180
sandbox.stub(storage, 'getDataFromLocalStorage').callsArgWith(1, storageValue);
178181
sandbox.stub(Math, 'random').returns(0.8);
182+
sandbox.stub(utils, 'generateUUID').returns('uid-5678');
179183

180184
const spy = sandbox.spy(_internal.getAdagioNs().queue, 'push')
181185

@@ -185,6 +189,7 @@ describe('Adagio Rtd Provider', function () {
185189
session: {
186190
...session,
187191
new: true,
192+
id: utils.generateUUID(),
188193
rnd: Math.random(),
189194
}
190195
}
@@ -196,6 +201,73 @@ describe('Adagio Rtd Provider', function () {
196201
}).calledOnce).to.be.true;
197202
});
198203
});
204+
205+
describe('store session data in localStorage when used with external AB Test snippet', function () {
206+
const sessionWithABTest = {
207+
lastActivityTime: 1714116520700,
208+
id: 'uid-1234',
209+
rnd: 0.5697,
210+
vwSmplg: 0.1,
211+
vwSmplgNxt: 0.1,
212+
testName: 'adg-test',
213+
testVersion: 'srv',
214+
initiator: 'snippet'
215+
};
216+
217+
it('store new session data instancied by the AB Test snippet for further usage', function () {
218+
const sessionWithNewFlag = { ...sessionWithABTest, new: true };
219+
const storageValue = JSON.stringify({session: sessionWithNewFlag});
220+
sandbox.stub(storage, 'getDataFromLocalStorage').callsArgWith(1, storageValue);
221+
sandbox.stub(Date, 'now').returns(1714116520710);
222+
sandbox.stub(Math, 'random').returns(0.8);
223+
224+
const spy = sandbox.spy(_internal.getAdagioNs().queue, 'push')
225+
226+
adagioRtdSubmodule.init(config);
227+
228+
const expected = {
229+
session: {
230+
...sessionWithNewFlag
231+
}
232+
}
233+
234+
expect(spy.withArgs({
235+
action: 'session',
236+
ts: Date.now(),
237+
data: expected,
238+
}).calledOnce).to.be.true;
239+
});
240+
241+
it('store new session data after removing AB Test props when initiator is not the snippet', function () {
242+
const sessionWithNewFlag = { ...sessionWithABTest, new: false, initiator: 'adgjs' };
243+
const storageValue = JSON.stringify({session: sessionWithNewFlag});
244+
sandbox.stub(storage, 'getDataFromLocalStorage').callsArgWith(1, storageValue);
245+
sandbox.stub(Date, 'now').returns(1714116520710);
246+
sandbox.stub(Math, 'random').returns(0.8);
247+
sandbox.stub(utils, 'generateUUID').returns('uid-5678');
248+
249+
const spy = sandbox.spy(_internal.getAdagioNs().queue, 'push')
250+
251+
adagioRtdSubmodule.init(config);
252+
253+
const expected = {
254+
session: {
255+
...sessionWithNewFlag,
256+
new: true,
257+
id: utils.generateUUID(),
258+
rnd: Math.random(),
259+
}
260+
}
261+
delete expected.session.testName;
262+
delete expected.session.testVersion;
263+
264+
expect(spy.withArgs({
265+
action: 'session',
266+
ts: Date.now(),
267+
data: expected,
268+
}).calledOnce).to.be.true;
269+
});
270+
});
199271
});
200272

201273
describe('submodule `getBidRequestData`', function () {

0 commit comments

Comments
 (0)