Skip to content

Commit

Permalink
AdagioRtdProvider: change session lastActivityTime to session to simp…
Browse files Browse the repository at this point in the history
…lify logic of session users
  • Loading branch information
Abyfall committed Dec 10, 2024
1 parent dd2ced6 commit 8c8bfd5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
66 changes: 42 additions & 24 deletions modules/adagioRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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({
Expand Down Expand Up @@ -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;
},
Expand All @@ -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;
}
};

Expand Down Expand Up @@ -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.
*/

/**
Expand Down
6 changes: 4 additions & 2 deletions test/spec/modules/adagioRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -138,6 +139,7 @@ describe('Adagio Rtd Provider', function () {

const expected = {
session: {
v: 2,
new: true,
id: utils.generateUUID(),
rnd: Math.random(),
Expand Down

0 comments on commit 8c8bfd5

Please sign in to comment.