-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Perhaps these can be changed to WARN or INFO messages instead? Example of error where pair id is not found: 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); |
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}) | ||
}) | ||
}); |
There was a problem hiding this comment.
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