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

LiveIntent Identity Module. #4178

Merged
merged 19 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
4 changes: 4 additions & 0 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
* @property {(string|undefined)} partner - partner url param value
* @property {(string|undefined)} url - webservice request url used to load Id data
* @property {(string|undefined)} pid - placement id url param value
* @property {(string|undefined)} publisherId - the unique identifier of the publisher in question
* @property {(array|undefined)} identifiersToResolve - the identifiers from either ls|cookie to be attached to the getId query
*/

/**
Expand All @@ -79,6 +81,7 @@ import {gdprDataHandler} from '../../src/adapterManager';
import CONSTANTS from '../../src/constants.json';
import {module} from '../../src/hook';
import {unifiedIdSubmodule} from './unifiedIdSystem.js';
import {liveIntentIdSubmodule} from './liveIntentIdSystem.js';
import {pubCommonIdSubmodule} from './pubCommonIdSystem.js';

const MODULE_NAME = 'User ID';
Expand Down Expand Up @@ -482,5 +485,6 @@ init(config);
// add submodules after init has been called
attachIdSystem(pubCommonIdSubmodule);
attachIdSystem(unifiedIdSubmodule);
attachIdSystem(liveIntentIdSubmodule);
idettman marked this conversation as resolved.
Show resolved Hide resolved

module('userId', attachIdSystem);
92 changes: 92 additions & 0 deletions modules/userId/liveIntentIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* This module adds LiveIntentId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/liveIntentIdSystem
* @requires module:modules/userId
*/
/** @type {Submodule} */
import * as utils from '../../src/utils'
import {ajax} from '../../src/ajax';

const MODULE_NAME = 'liveIntentId'
const LIVE_CONNECT_DUID_KEY = '_li_duid';
const DOMAIN_USER_ID_QUERY_PARAM_KEY = 'duid';
const DEFAULT_LIVEINTENT_IDENTITY_URL = '//idx.liadm.com';
const DEFAULT_PREBID_SOURCE = 'prebid';

export const liveIntentIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,

/**
* decode the stored id value for passing to bid requests
* @function
* @param {{unifiedId:string}} value
* @returns {{liuid:Object}}
*/
decode(value) {
function composeIdObject(value) {
const base = {'lipbid': value['unifiedId']};
delete value.unifiedId;
return {'lipb': {...base, ...value}};
}
return (value && typeof value['unifiedId'] === 'string') ? composeIdObject(value) : undefined;
},

/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleParams} [configParams]
* @returns {function(callback:function)}
*/
getId(configParams) {
const publisherId = configParams && configParams.publisherId;
if (!publisherId && typeof publisherId !== 'string') {
utils.logError(`${MODULE_NAME} - publisherId must be defined, not a '${publisherId}'`);
return;
}
let baseUrl = DEFAULT_LIVEINTENT_IDENTITY_URL;
let source = DEFAULT_PREBID_SOURCE;
if (configParams.url) {
baseUrl = configParams.url
}
if (configParams.partner) {
source = configParams.partner
}

const additionalIdentifierNames = configParams.identifiersToResolve || [];

const additionalIdentifiers = additionalIdentifierNames.concat([LIVE_CONNECT_DUID_KEY]).reduce((obj, identifier) => {
const value = utils.getCookie(identifier) || utils.getDataFromLocalStorage(identifier);
const key = identifier.replace(LIVE_CONNECT_DUID_KEY, DOMAIN_USER_ID_QUERY_PARAM_KEY);
if (value) {
if (typeof value === 'object') {
obj[key] = JSON.stringify(value);
} else {
obj[key] = value;
}
}
return obj
}, {});

const queryString = utils.parseQueryStringParameters(additionalIdentifiers)
const url = `${baseUrl}/idex/${source}/${publisherId}?${queryString}`;

return function (callback) {
ajax(url, response => {
let responseObj;
if (response) {
try {
responseObj = JSON.parse(response);
} catch (error) {
utils.logError(error);
}
}
callback(responseObj);
}, undefined, { method: 'GET', withCredentials: true });
}
}
};
24 changes: 22 additions & 2 deletions modules/userId/userId.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@ pbjs.setConfig({
name: 'idl_env',
expires: 60
}
}],
}, {
name: 'liveIntentId',
params: {
publisherId: '7798696' // Set an identifier of a publisher know to your systems
},
storage: {
type: 'cookie',
name: '_li_pbid',
expires: 60
}
}],
syncDelay: 5000
}
});
Expand Down Expand Up @@ -81,7 +91,17 @@ pbjs.setConfig({
name: 'idl_env',
expires: 60
}
}],
}, {
name: 'liveIntentId',
params: {
publisherId: '7798696' // Set an identifier of a publisher know to your systems
},
storage: {
type: 'html5',
name: '_li_pbid',
expires: 60
}
}],
syncDelay: 5000
}
});
Expand Down
145 changes: 145 additions & 0 deletions test/spec/modules/liveIntentIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { liveIntentIdSubmodule } from 'modules/userId/liveIntentIdSystem';
import * as utils from 'src/utils';

describe('LiveIntentId', function() {
let xhr;
let requests;
let getCookieStub;
let getDataFromLocalStorageStub;
let logErrorStub;

const defaultConfigParams = {'publisherId': '89899'};
const responseHeader = { 'Content-Type': 'application/json' }

beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = request => requests.push(request);
getCookieStub = sinon.stub(utils, 'getCookie');
getDataFromLocalStorageStub = sinon.stub(utils, 'getDataFromLocalStorage');
logErrorStub = sinon.stub(utils, 'logError');
});

afterEach(function () {
xhr.restore();
getCookieStub.restore();
getDataFromLocalStorageStub.restore();
logErrorStub.restore();
});

it('should log an error if no configParams were passed', function() {
liveIntentIdSubmodule.getId();
expect(logErrorStub.calledOnce).to.be.true;
});

it('should log an error if publisherId configParam was not passed', function() {
liveIntentIdSubmodule.getId({});
expect(logErrorStub.calledOnce).to.be.true;
});

it('should call the Custom URL of the LiveIntent Identity Exchange endpoint', function() {
getCookieStub.returns(null);
let callBackSpy = sinon.spy();
let submoduleCallback = liveIntentIdSubmodule.getId({...defaultConfigParams, ...{'url': 'https://dummy.liveintent.com'}});
submoduleCallback(callBackSpy);
let request = requests[0];
expect(request.url).to.be.eq('https://dummy.liveintent.com/idex/prebid/89899?');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should call the default url of the LiveIntent Identity Exchange endpoint, with a partner', function() {
getCookieStub.returns(null);
let callBackSpy = sinon.spy();
let submoduleCallback = liveIntentIdSubmodule.getId({...defaultConfigParams, ...{'url': 'https://dummy.liveintent.com', 'partner': 'rubicon'}});
submoduleCallback(callBackSpy);
let request = requests[0];
expect(request.url).to.be.eq('https://dummy.liveintent.com/idex/rubicon/89899?');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should call the LiveIntent Identity Exchange endpoint, with no additional query params', function() {
getCookieStub.returns(null);
let callBackSpy = sinon.spy();
let submoduleCallback = liveIntentIdSubmodule.getId(defaultConfigParams);
submoduleCallback(callBackSpy);
let request = requests[0];
expect(request.url).to.be.eq('//idx.liadm.com/idex/prebid/89899?');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should include the LiveConnect identifier when calling the LiveIntent Identity Exchange endpoint', function() {
getCookieStub.withArgs('_li_duid').returns('li-fpc');
let callBackSpy = sinon.spy();
let submoduleCallback = liveIntentIdSubmodule.getId(defaultConfigParams);
submoduleCallback(callBackSpy);
let request = requests[0];
expect(request.url).to.be.eq('//idx.liadm.com/idex/prebid/89899?duid=li-fpc&');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should include the LiveConnect identifier and additional Identifiers to resolve', function() {
getCookieStub.withArgs('_li_duid').returns('li-fpc');
getDataFromLocalStorageStub.withArgs('_thirdPC').returns('third-pc');
let configParams = {
...defaultConfigParams,
...{
'identifiersToResolve': ['_thirdPC']
}
};

let callBackSpy = sinon.spy();
let submoduleCallback = liveIntentIdSubmodule.getId(configParams);
submoduleCallback(callBackSpy);
let request = requests[0];
expect(request.url).to.be.eq('//idx.liadm.com/idex/prebid/89899?_thirdPC=third-pc&duid=li-fpc&');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should include an additional identifier value to resolve even if it is an object', function() {
getCookieStub.returns(null);
getDataFromLocalStorageStub.withArgs('_thirdPC').returns({'key': 'value'});
let configParams = {
...defaultConfigParams,
...{
'identifiersToResolve': ['_thirdPC']
}
};

let callBackSpy = sinon.spy();
let submoduleCallback = liveIntentIdSubmodule.getId(configParams);
submoduleCallback(callBackSpy);
let request = requests[0];
expect(request.url).to.be.eq('//idx.liadm.com/idex/prebid/89899?_thirdPC=%7B%22key%22%3A%22value%22%7D&');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});
});
Loading