From 8c8bfd57a3256961ce3834d0af341ae212dcf006 Mon Sep 17 00:00:00 2001 From: Michel Chretien Date: Thu, 28 Nov 2024 11:48:28 +0100 Subject: [PATCH] AdagioRtdProvider: change session lastActivityTime to session to simplify logic of session users --- modules/adagioRtdProvider.js | 66 +++++++++++++-------- test/spec/modules/adagioRtdProvider_spec.js | 6 +- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/modules/adagioRtdProvider.js b/modules/adagioRtdProvider.js index 691ad6eb44f..ee81e1337dd 100644 --- a/modules/adagioRtdProvider.js +++ b/modules/adagioRtdProvider.js @@ -37,7 +37,7 @@ const SUBMODULE_NAME = 'adagio'; const ADAGIO_BIDDER_CODE = 'adagio'; const GVLID = 617; const SCRIPT_URL = 'https://script.4dex.io/a/latest/adagio.js'; -const SESS_DURATION = 30 * 60 * 1000; +const LATEST_ABTEST_VERSION = 2; export const PLACEMENT_SOURCES = { ORTB: 'ortb', // implicit default, not used atm. ADUNITCODE: 'code', @@ -66,27 +66,30 @@ const _SESSION = (function() { return { init: () => { // helper function to determine if the session is new. - const isNewSession = (lastActivity) => { - const now = Date.now(); - return (!isNumber(lastActivity) || (now - lastActivity) > SESS_DURATION); + const isNewSession = (expiry) => { + return (!isNumber(expiry) || Date.now() > expiry); }; storage.getDataFromLocalStorage('adagio', (storageValue) => { // session can be an empty object - const { rnd, vwSmplg, vwSmplgNxt, lastActivityTime, id, pages } = _internal.getSessionFromLocalStorage(storageValue); + const { rnd, vwSmplg, vwSmplgNxt, expiry, lastActivityTime, id, pages, testName: legacyTestName, testVersion: legacyTestVersion } = _internal.getSessionFromLocalStorage(storageValue); - const isNewSess = isNewSession(lastActivityTime); + const isNewSess = isNewSession(expiry); + + // if lastActivityTime is defined it means that the website is using the original version of the snippet + const v = !lastActivityTime ? LATEST_ABTEST_VERSION : undefined; data.session = { + v, rnd, pages: pages || 1, new: isNewSess, // legacy: `new` was used but the choosen name is not good. // Don't use values if they are not defined. ...(vwSmplg !== undefined && { vwSmplg }), ...(vwSmplgNxt !== undefined && { vwSmplgNxt }), - ...(lastActivityTime !== undefined && { lastActivityTime }), + ...(expiry !== undefined && { expiry }), + ...(lastActivityTime !== undefined && { lastActivityTime }), // legacy: used by older version of the snippet ...(id !== undefined && { id }), - }; if (isNewSess) { @@ -95,10 +98,15 @@ const _SESSION = (function() { data.session.rnd = Math.random(); } - const { testName, testVersion, expiry, sessionId } = _internal.getAbTestFromLocalStorage(storageValue); - if (expiry && expiry > Date.now() && (!sessionId || sessionId === data.session.id)) { // if AbTest didn't set a session id, it's probably because it's a new one and it didn't retrieve it yet, assume it's okay to get test Name and Version. - data.session.testName = testName; - data.session.testVersion = testVersion; + const { testName, testVersion, expiry: abTestExpiry, sessionId } = _internal.getAbTestFromLocalStorage(storageValue); + if (v === LATEST_ABTEST_VERSION) { + if (abTestExpiry && abTestExpiry > Date.now() && (!sessionId || sessionId === data.session.id)) { // if AbTest didn't set a session id, it's probably because it's a new one and it didn't retrieve it yet, assume it's okay to get test Name and Version. + data.session.testName = testName; + data.session.testVersion = testVersion; + } + } else { + data.session.testName = legacyTestName; + data.session.testVersion = legacyTestVersion; } _internal.getAdagioNs().queue.push({ @@ -193,11 +201,7 @@ export const _internal = { rnd: Math.random() }; - const obj = JSON.parse(storageValue, function(name, value) { - if (name.charAt(0) !== '_' || name === '') { - return value; - } - }); + const obj = this.getObjFromStorageValue(storageValue); return (!obj || !obj.session) ? _default : obj.session; }, @@ -209,13 +213,23 @@ export const _internal = { * @returns {AbTest} */ getAbTestFromLocalStorage: function(storageValue) { - const obj = JSON.parse(storageValue, function(name, value) { + const obj = this.getObjFromStorageValue(storageValue); + + return (!obj || !obj.abTest) ? {} : obj.abTest; + }, + + /** + * Returns the parsed data from the localStorage. + * + * @param {string} storageValue - The value stored in the localStorage. + * @returns {Object} + */ + getObjFromStorageValue: function(storageValue) { + return JSON.parse(storageValue, function(name, value) { if (name.charAt(0) !== '_' || name === '') { return value; } }); - - return (!obj || !obj.abTest) ? {} : obj.abTest; } }; @@ -694,20 +708,24 @@ function registerEventsForAdServers(config) { /** * @typedef {Object} Session + * @property {string} id - uuid of the session. * @property {boolean} new - True if the session is new. * @property {number} rnd - Random number used to determine if the session is new. * @property {number} vwSmplg - View sampling rate. * @property {number} vwSmplgNxt - Next view sampling rate. + * @property {number} expiry - Timestamp after which session should be considered expired. * @property {number} lastActivityTime - Last activity time. * @property {number} pages - current number of pages seen. + * @property {string} testName - The test name defined by the publisher. Legacy only present for websites with older abTest snippet. + * @property {string} testVersion - 'clt', 'srv'. Legacy only present for websites with older abTest snippet. */ /** * @typedef {Object} AbTest - * @property {number} pages - current number of pages seen. - * @property {string} testName - 'adg-pbs', 'adg-discrepancies' - * @property {string} testVersion - 'clt', 'srv' - * @property {string} sessionId - uuid of the session + * @property {string} testName - The test name defined by the publisher. + * @property {string} testVersion - 'clt', 'srv'. + * @property {string} sessionId - uuid of the session. + * @property {number} expiry - Timestamp after which session should be considered expired. */ /** diff --git a/test/spec/modules/adagioRtdProvider_spec.js b/test/spec/modules/adagioRtdProvider_spec.js index 30d9c34f7ab..7b9fec595b1 100644 --- a/test/spec/modules/adagioRtdProvider_spec.js +++ b/test/spec/modules/adagioRtdProvider_spec.js @@ -117,12 +117,13 @@ describe('Adagio Rtd Provider', function () { describe('store session data in localStorage', function () { const session = { - lastActivityTime: 1714116520700, + expiry: 1714116530700, id: 'uid-1234', rnd: 0.5697, vwSmplg: 0.1, vwSmplgNxt: 0.1, - pages: 1 + pages: 1, + v: 2 }; it('store new session data for further usage', function () { @@ -138,6 +139,7 @@ describe('Adagio Rtd Provider', function () { const expected = { session: { + v: 2, new: true, id: utils.generateUUID(), rnd: Math.random(),