Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pairId userId submodule: initial commit for pairId submodule #9662

Merged
merged 4 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions modules/pairIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* This module adds PAIR Id to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/pairIdSystem
* @requires module:modules/userId
*/

import { submodule } from '../src/hook.js';
import {getStorageManager} from '../src/storageManager.js'
import { logError } from '../src/utils.js';

const MODULE_NAME = 'pairId';
const PAIR_ID_KEY = 'pairId';
const LIVERAMP_PAIR_ID_KEY = 'lr_pairId';

export const storage = getStorageManager()

function pairIdFromLocalStorage(key) {
return storage.localStorageIsEnabled ? storage.getDataFromLocalStorage(key) : null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storage.localStorageIsEnabled is a function, so this check will always evaluate to true.

This should be updated to storage.localStorageIsEnabled() which returns true or false.

Same with storage.cookiesAreEnabled

}

function pairIdFromCookie(key) {
return storage.cookiesAreEnabled ? storage.getCookie(key) : null;
}

/** @type {Submodule} */
export const pairIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* decode the stored id value for passing to bid requests
* @function
* @param { string | undefined } value
* @returns {{pairId:string} | undefined }
*/
decode(value) {
return value && Array.isArray(value) ? {'pairId': value} : undefined
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @returns {id: string | undefined }
*/
getId(config) {
const pairIdsString = pairIdFromLocalStorage(PAIR_ID_KEY) || pairIdFromCookie(PAIR_ID_KEY)
let ids = []
if (pairIdsString && typeof pairIdsString == 'string') {
try {
ids = ids.concat(JSON.parse(atob(pairIdsString)))
} catch (error) {
logError(error)
}
}

const configParams = (config && config.params) || {};
if (configParams && configParams.liveramp) {
const liverampValue = pairIdFromLocalStorage(LIVERAMP_PAIR_ID_KEY) || pairIdFromCookie(LIVERAMP_PAIR_ID_KEY)
patmmccann marked this conversation as resolved.
Show resolved Hide resolved
try {
const obj = JSON.parse(atob(liverampValue));
ids = ids.concat(obj.envelope);
} catch (error) {
logError(error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if some other code does not set the pair id or the liveramp id in storage or cookie then this module logs an error every single time. Which seems like overkill.

Same with the check below this, if no ID is found it throws a pretty nasty error:

image

Perhaps these can be changed to WARN or INFO messages instead?

Example of error where pair id is not found:

image

I think an error is a bit too strong here.

}
}

if (ids.length == 0) {
logError('PairId not found.')
return undefined;
}
return {'id': ids};
}
};

submodule('userId', pairIdSubmodule);
6 changes: 6 additions & 0 deletions modules/userId/eids.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export const USER_IDS_CONFIG = {
atype: 1
},

// pairId
'pairId': {
source: 'google.com',
atype: 571187
patmmccann marked this conversation as resolved.
Show resolved Hide resolved
},

// justId
'justId': {
source: 'justtag.com',
Expand Down
56 changes: 56 additions & 0 deletions test/spec/modules/pairIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { storage, pairIdSubmodule } from 'modules/pairIdSystem.js';
import * as utils from 'src/utils.js';

describe('pairId', function () {
let sandbox;
let logErrorStub;

beforeEach(() => {
sandbox = sinon.sandbox.create();
logErrorStub = sandbox.stub(utils, 'logError');
});
afterEach(() => {
sandbox.restore();
});

it('should log an error if no ID is found when getId', function() {
pairIdSubmodule.getId({ params: {} });
expect(logErrorStub.calledOnce).to.be.true;
});

it('should read pairId from local stroage if exists', function() {
congdu-kun marked this conversation as resolved.
Show resolved Hide resolved
let pairIds = ['test-pair-id1', 'test-pair-id2', 'test-pair-id3'];
sandbox.stub(storage, 'getDataFromLocalStorage').withArgs('pairId').returns(btoa(JSON.stringify(pairIds)));

let id = pairIdSubmodule.getId({ params: {} });
expect(id).to.be.deep.equal({id: pairIds});
});

it('should read pairId from cookie if exists', function() {
let pairIds = ['test-pair-id4', 'test-pair-id5', 'test-pair-id6'];
sandbox.stub(storage, 'getCookie').withArgs('pairId').returns(btoa(JSON.stringify(pairIds)));

let id = pairIdSubmodule.getId({ params: {} });
expect(id).to.be.deep.equal({id: pairIds});
});

it('should read pairId from liveramp envelop local storage key if configured', function() {
congdu-kun marked this conversation as resolved.
Show resolved Hide resolved
let pairIds = ['test-pair-id1', 'test-pair-id2', 'test-pair-id3'];
sandbox.stub(storage, 'getDataFromLocalStorage').withArgs('lr_pairId').returns(btoa(JSON.stringify({'envelope': pairIds})));
let id = pairIdSubmodule.getId({
params: {
liveramp: {}
}})
expect(id).to.be.deep.equal({id: pairIds})
})

it('should read pairId from liveramp envelop cookie entry if configured', function() {
congdu-kun marked this conversation as resolved.
Show resolved Hide resolved
let pairIds = ['test-pair-id4', 'test-pair-id5', 'test-pair-id6'];
sandbox.stub(storage, 'getDataFromLocalStorage').withArgs('lr_pairId').returns(btoa(JSON.stringify({'envelope': pairIds})));
let id = pairIdSubmodule.getId({
params: {
liveramp: {}
}})
expect(id).to.be.deep.equal({id: pairIds})
})
});