diff --git a/modules/.submodules.json b/modules/.submodules.json index f321e075208..097fe4b1f3b 100644 --- a/modules/.submodules.json +++ b/modules/.submodules.json @@ -1,7 +1,8 @@ { "userId": [ "digiTrustIdSystem", - "id5IdSystem" + "id5IdSystem", + "criteortusIdSystem" ], "adpod": [ "freeWheelAdserverVideo" diff --git a/modules/appnexusBidAdapter.js b/modules/appnexusBidAdapter.js index 50c5a0e6f04..67388848815 100644 --- a/modules/appnexusBidAdapter.js +++ b/modules/appnexusBidAdapter.js @@ -169,6 +169,16 @@ export const spec = { }); } + const rtusId = utils.deepAccess(bidRequests[0], `userId.criteortus.${BIDDER_CODE}.userid`); + if (rtusId) { + let tpuids = []; + tpuids.push({ + 'provider': 'criteo', + 'user_id': rtusId + }); + payload.tpuids = tpuids; + } + const request = formatRequest(payload, bidderRequest); return request; }, diff --git a/modules/criteortusIdSystem.js b/modules/criteortusIdSystem.js new file mode 100644 index 00000000000..02edf0ef06e --- /dev/null +++ b/modules/criteortusIdSystem.js @@ -0,0 +1,105 @@ +/** + * This module adds Criteo Real Time User Sync to the User ID module + * The {@link module:modules/userId} module is required + * @module modules/criteortusIdSystem + * @requires module:modules/userId + */ + +import * as utils from '../src/utils' +import { ajax } from '../src/ajax'; +import { submodule } from '../src/hook'; + +const key = '__pbjs_criteo_rtus'; + +/** @type {Submodule} */ +export const criteortusIdSubmodule = { + /** + * used to link submodule with config + * @type {string} + */ + name: 'criteortus', + /** + * decode the stored id value for passing to bid requests + * @function + * @returns {{criteortus:Object}} + */ + decode() { + let uid = utils.getCookie(key); + try { + uid = JSON.parse(uid); + return { 'criteortus': uid }; + } catch (error) { + utils.logError('Error in parsing criteo rtus data', error); + } + }, + /** + * 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) { + if (!configParams || !utils.isPlainObject(configParams.clientIdentifier)) { + utils.logError('User ID - Criteo rtus requires client identifier to be defined'); + return; + } + + let uid = utils.getCookie(key); + if (uid) { + return uid; + } else { + let userIds = {}; + return function(callback) { + let bidders = Object.keys(configParams.clientIdentifier); + + function afterAllResponses() { + // criteo rtus user id expires in 1 hour + const expiresStr = (new Date(Date.now() + (60 * 60 * 1000))).toUTCString(); + utils.setCookie(key, JSON.stringify(userIds), expiresStr); + callback(userIds); + } + + const onResponse = utils.delayExecution(afterAllResponses, bidders.length); + + bidders.forEach((bidder) => { + let url = `https://gum.criteo.com/sync?c=${configParams.clientIdentifier[bidder]}&r=3`; + const getSuccessHandler = (bidder) => { + return function onSuccess(response) { + if (response) { + try { + response = JSON.parse(response); + userIds[bidder] = response; + onResponse(); + } catch (error) { + utils.logError(error); + } + } + } + } + + const getFailureHandler = (bidder) => { + return function onFailure(error) { + utils.logError(`Criteo RTUS server call failed for ${bidder}`, error); + onResponse(); + } + } + + ajax( + url, + { + success: getSuccessHandler(bidder), + error: getFailureHandler(bidder) + }, + undefined, + Object.assign({ + method: 'GET', + withCredentials: true + }) + ); + }) + } + } + } +}; + +submodule('userId', criteortusIdSubmodule); diff --git a/modules/userId/index.js b/modules/userId/index.js index 2091a6a763a..e83258a4e02 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -186,7 +186,9 @@ function processSubmoduleCallbacks(submodules) { submodule.callback = undefined; // if valid, id data should be saved to cookie/html storage if (idObj) { - setStoredValue(submodule.config.storage, idObj, submodule.config.storage.expires); + if (submodule.config.storage) { + setStoredValue(submodule.config.storage, idObj, submodule.config.storage.expires); + } // cache decoded value (this is copied to every adUnit bid) submodule.idObj = submodule.submodule.decode(idObj); } else { @@ -299,6 +301,13 @@ function initSubmodules(submodules, consentData) { } else if (submodule.config.value) { // cache decoded value (this is copied to every adUnit bid) submodule.idObj = submodule.config.value; + } else { + const result = submodule.submodule.getId(submodule.config.params, consentData); + if (typeof result === 'function') { + submodule.callback = result; + } else { + submodule.idObj = submodule.submodule.decode(); + } } carry.push(submodule); return carry; @@ -332,6 +341,8 @@ function getValidSubmoduleConfigs(configRegistry, submoduleRegistry, activeStora carry.push(config); } else if (utils.isPlainObject(config.value)) { carry.push(config); + } else if (!config.storage && !config.value) { + carry.push(config); } return carry; }, []); diff --git a/test/spec/modules/appnexusBidAdapter_spec.js b/test/spec/modules/appnexusBidAdapter_spec.js index e55e3e32029..30d2dc08159 100644 --- a/test/spec/modules/appnexusBidAdapter_spec.js +++ b/test/spec/modules/appnexusBidAdapter_spec.js @@ -614,6 +614,22 @@ describe('AppNexusAdapter', function () { rd_stk: bidderRequest.refererInfo.stack.map((url) => encodeURIComponent(url)).join(',') }); }); + + it('should populate tpids array when userId is available', function () { + const bidRequest = Object.assign({}, bidRequests[0], { + userId: { + criteortus: { + appnexus: { + userid: 'sample-userid' + } + } + } + }); + + const request = spec.buildRequests([bidRequest]); + const payload = JSON.parse(request.data); + expect(payload.tpuids).to.deep.equal([{provider: 'criteo', user_id: 'sample-userid'}]); + }); }) describe('interpretResponse', function () { diff --git a/test/spec/modules/criteortusIdSystem_spec.js b/test/spec/modules/criteortusIdSystem_spec.js new file mode 100644 index 00000000000..578f14d066d --- /dev/null +++ b/test/spec/modules/criteortusIdSystem_spec.js @@ -0,0 +1,88 @@ +import { criteortusIdSubmodule } from 'modules/criteortusIdSystem'; +import * as utils from 'src/utils'; + +describe('Criteo RTUS', function() { + let xhr; + let requests; + let getCookieStub; + let logErrorStub; + + beforeEach(function () { + xhr = sinon.useFakeXMLHttpRequest(); + requests = []; + xhr.onCreate = request => requests.push(request); + getCookieStub = sinon.stub(utils, 'getCookie'); + logErrorStub = sinon.stub(utils, 'logError'); + }); + + afterEach(function () { + xhr.restore(); + getCookieStub.restore(); + logErrorStub.restore(); + }); + + it('should log error when configParams are not passed', function() { + criteortusIdSubmodule.getId(); + expect(logErrorStub.calledOnce).to.be.true; + }) + + it('should call criteo endpoint to get user id', function() { + getCookieStub.returns(null); + let configParams = { + clientIdentifier: { + 'sampleBidder': 1 + } + } + + let response = { 'status': 'ok', 'userid': 'sample-userid' } + let callBackSpy = sinon.spy(); + let submoduleCallback = criteortusIdSubmodule.getId(configParams); + submoduleCallback(callBackSpy); + requests[0].respond( + 200, + { 'Content-Type': 'text/plain' }, + JSON.stringify(response) + ); + expect(callBackSpy.calledOnce).to.be.true; + expect(callBackSpy.calledWith({'sampleBidder': response})).to.be.true; + }) + + it('should get uid from cookie and not call endpoint', function() { + let response = {'appnexus': {'status': 'ok', 'userid': 'sample-userid'}} + getCookieStub.returns(JSON.stringify(response)); + let configParams = { + clientIdentifier: { + 'sampleBidder': 1 + } + } + let uid = criteortusIdSubmodule.getId(configParams); + expect(requests.length).to.equal(0); + }) + + it('should call criteo endpoint for multiple bidders', function() { + getCookieStub.returns(null); + let configParams = { + clientIdentifier: { + 'sampleBidder': 1, + 'sampleBidder2': 2 + } + } + + let response = { 'status': 'ok', 'userid': 'sample-userid' } + let callBackSpy = sinon.spy(); + let submoduleCallback = criteortusIdSubmodule.getId(configParams); + submoduleCallback(callBackSpy); + requests[0].respond( + 200, + { 'Content-Type': 'text/plain' }, + JSON.stringify(response) + ); + expect(callBackSpy.calledOnce).to.be.false; + requests[1].respond( + 200, + { 'Content-Type': 'text/plain' }, + JSON.stringify(response) + ); + expect(callBackSpy.calledOnce).to.be.true; + }) +});