Skip to content

Commit e0fe3ac

Browse files
feat: session marker (#12634)
Co-authored-by: rufiange <sebastien@rufiange.com>
1 parent f14229a commit e0fe3ac

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

modules/contxtfulRtdProvider.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
isEmpty,
1616
buildUrl,
1717
isArray,
18+
generateUUID,
1819
} from '../src/utils.js';
1920
import { loadExternalScript } from '../src/adloader.js';
2021
import { getStorageManager } from '../src/storageManager.js';
@@ -26,13 +27,17 @@ const MODULE = `${MODULE_NAME}RtdProvider`;
2627
const CONTXTFUL_HOSTNAME_DEFAULT = 'api.receptivity.io';
2728
const CONTXTFUL_DEFER_DEFAULT = 0;
2829

30+
let _sm;
31+
function sm() {
32+
return _sm ??= generateUUID();
33+
}
34+
2935
const storageManager = getStorageManager({
3036
moduleType: MODULE_TYPE_RTD,
3137
moduleName: MODULE_NAME,
3238
});
3339

3440
let rxApi = null;
35-
let isFirstBidRequestCall = true;
3641

3742
/**
3843
* Return current receptivity value for the requester.
@@ -150,7 +155,7 @@ function initCustomer(config) {
150155

151156
addConnectorEventListener(customer, config);
152157

153-
const loadScript = () => loadExternalScript(CONNECTOR_URL, MODULE_TYPE_RTD, MODULE_NAME);
158+
const loadScript = () => loadExternalScript(CONNECTOR_URL, MODULE_TYPE_RTD, MODULE_NAME, undefined, undefined, { 'data-sm': sm() });
154159
// Optionally defer the loading of the script
155160
if (Number.isFinite(defer) && defer > 0) {
156161
setTimeout(loadScript, defer);
@@ -228,9 +233,6 @@ function getTargetingData(adUnits, config, _userConsent) {
228233
*/
229234
function getBidRequestData(reqBidsConfigObj, onDone, config, userConsent) {
230235
function onReturn() {
231-
if (isFirstBidRequestCall) {
232-
isFirstBidRequestCall = false;
233-
}
234236
onDone();
235237
}
236238

@@ -245,16 +247,10 @@ function getBidRequestData(reqBidsConfigObj, onDone, config, userConsent) {
245247
let fromStorage = prepareBatch(bidders, (bidder) => loadSessionReceptivity(`${config?.params?.customer}_${bidder}`));
246248

247249
let sources = [fromStorage, fromApi];
248-
if (isFirstBidRequestCall) {
249-
sources.reverse();
250-
}
251250

252251
let rxBatch = Object.assign(...sources);
253252

254-
let singlePointEvents;
255-
if (isEmpty(rxBatch)) {
256-
singlePointEvents = btoa(JSON.stringify({ ui: getUiEvents() }));
257-
}
253+
let singlePointEvents = btoa(JSON.stringify({ ui: getUiEvents() }));
258254

259255
bidders
260256
.forEach(bidderCode => {
@@ -266,6 +262,7 @@ function getBidRequestData(reqBidsConfigObj, onDone, config, userConsent) {
266262
ext: {
267263
rx: rxBatch[bidderCode],
268264
events: singlePointEvents,
265+
sm: sm(),
269266
params: {
270267
ev: config.params?.version,
271268
ci: config.params?.customer,

test/spec/modules/contxtfulRtdProvider_spec.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { loadExternalScriptStub } from 'test/mocks/adloaderStub.js';
44
import { getStorageManager } from '../../../src/storageManager.js';
55
import { MODULE_TYPE_UID } from '../../../src/activities/modules.js';
66
import * as events from '../../../src/events';
7+
import * as utils from 'src/utils.js';
78
import Sinon from 'sinon';
9+
import { deepClone } from '../../../src/utils.js';
810

911
const MODULE_NAME = 'contxtful';
1012

1113
const VERSION = 'v1';
1214
const CUSTOMER = 'CUSTOMER';
15+
const SM = 'SM';
1316
const CONTXTFUL_CONNECTOR_ENDPOINT = `https://api.receptivity.io/${VERSION}/prebid/${CUSTOMER}/connector/rxConnector.js`;
1417

1518
const RX_FROM_SESSION_STORAGE = { ReceptivityState: 'Receptive', test_info: 'rx_from_session_storage' };
@@ -62,6 +65,8 @@ describe('contxtfulRtdProvider', function () {
6265

6366
eventsEmitSpy = sandbox.spy(events, ['emit']);
6467

68+
sandbox.stub(utils, 'generateUUID').returns(SM);
69+
6570
let tagId = CUSTOMER;
6671
sessionStorage.clear();
6772
});
@@ -534,6 +539,7 @@ describe('contxtfulRtdProvider', function () {
534539
name: 'contxtful',
535540
ext: {
536541
rx: RX_FROM_API,
542+
sm: SM,
537543
params: {
538544
ev: config.params?.version,
539545
ci: config.params?.customer,
@@ -549,11 +555,40 @@ describe('contxtfulRtdProvider', function () {
549555

550556
expect(data.name).to.deep.equal(expectedData.name);
551557
expect(data.ext.rx).to.deep.equal(expectedData.ext.rx);
558+
expect(data.ext.sm).to.deep.equal(expectedData.ext.sm);
552559
expect(data.ext.params).to.deep.equal(expectedData.ext.params);
553560
done();
554561
}, TIMEOUT);
555562
});
556563

564+
it('does not change the sm', function (done) {
565+
let config = buildInitConfig(VERSION, CUSTOMER);
566+
contxtfulSubmodule.init(config);
567+
window.dispatchEvent(RX_CONNECTOR_IS_READY_EVENT);
568+
569+
let firstReqBidsConfigObj = {
570+
ortb2Fragments: {
571+
global: {},
572+
bidder: {},
573+
},
574+
};
575+
576+
let secondReqBidsConfigObj = deepClone(firstReqBidsConfigObj);
577+
578+
setTimeout(() => {
579+
const onDoneSpy = sinon.spy();
580+
contxtfulSubmodule.getBidRequestData(firstReqBidsConfigObj, onDoneSpy, config);
581+
contxtfulSubmodule.getBidRequestData(secondReqBidsConfigObj, onDoneSpy, config);
582+
583+
let firstData = firstReqBidsConfigObj.ortb2Fragments.bidder[config.params.bidders[0]].user.data[0];
584+
let secondData = secondReqBidsConfigObj.ortb2Fragments.bidder[config.params.bidders[0]].user.data[0];
585+
586+
expect(firstData.ext.sm).to.equal(secondData.ext.sm);
587+
588+
done();
589+
}, TIMEOUT);
590+
});
591+
557592
describe('before rxApi is loaded', function () {
558593
const moveEventTheories = [
559594
[
@@ -628,7 +663,7 @@ describe('contxtfulRtdProvider', function () {
628663
});
629664

630665
describe('after rxApi is loaded', function () {
631-
it('does not add event', function (done) {
666+
it('should add event', function (done) {
632667
let config = buildInitConfig(VERSION, CUSTOMER);
633668
contxtfulSubmodule.init(config);
634669
window.dispatchEvent(RX_CONNECTOR_IS_READY_EVENT);
@@ -648,7 +683,7 @@ describe('contxtfulRtdProvider', function () {
648683

649684
let events = ext.events;
650685

651-
expect(events).to.be.undefined;
686+
expect(events).to.be.not.undefined;
652687
done();
653688
}, TIMEOUT);
654689
});

0 commit comments

Comments
 (0)