diff --git a/modules/tripleliftBidAdapter.js b/modules/tripleliftBidAdapter.js index e9e6e5d2a63..df4f9a9ba38 100644 --- a/modules/tripleliftBidAdapter.js +++ b/modules/tripleliftBidAdapter.js @@ -2,6 +2,7 @@ import { tryAppendQueryString, logMessage, logError, isEmpty, isStr, isPlainObje import { BANNER, VIDEO } from '../src/mediaTypes.js'; import { registerBidder } from '../src/adapters/bidderFactory.js'; import { config } from '../src/config.js'; +import { getStorageManager } from '../src/storageManager.js'; const GVLID = 28; const BIDDER_CODE = 'triplelift'; @@ -10,6 +11,7 @@ const BANNER_TIME_TO_LIVE = 300; const INSTREAM_TIME_TO_LIVE = 3600; let gdprApplies = true; let consentString = null; +export const storage = getStorageManager({gvlid: GVLID, bidderCode: BIDDER_CODE}); export const tripleliftAdapterSpec = { gvlid: GVLID, @@ -197,10 +199,23 @@ function _getGlobalFpd(bidderRequest) { const context = {} const user = {}; const ortbData = bidderRequest.ortb2 || {}; + const opeCloudStorage = _fetchOpeCloud(); const fpdContext = Object.assign({}, ortbData.site); const fpdUser = Object.assign({}, ortbData.user); + if (opeCloudStorage) { + fpdUser.data = fpdUser.data || [] + try { + fpdUser.data.push({ + name: 'www.1plusx.com', + ext: opeCloudStorage + }) + } catch (err) { + logError('Triplelift: error adding 1plusX segments: ', err); + } + } + _addEntries(context, fpdContext); _addEntries(user, fpdUser); @@ -213,6 +228,18 @@ function _getGlobalFpd(bidderRequest) { return fpd; } +function _fetchOpeCloud() { + const opeCloud = storage.getDataFromLocalStorage('opecloud_ctx'); + if (!opeCloud) return null; + try { + const parsedJson = JSON.parse(opeCloud); + return parsedJson + } catch (err) { + logError('Triplelift: error parsing JSON: ', err); + return null + } +} + function _getAdUnitFpd(adUnitFpd) { const fpd = {}; const context = {}; diff --git a/test/spec/modules/tripleliftBidAdapter_spec.js b/test/spec/modules/tripleliftBidAdapter_spec.js index 733265b0bae..b0269aaf077 100644 --- a/test/spec/modules/tripleliftBidAdapter_spec.js +++ b/test/spec/modules/tripleliftBidAdapter_spec.js @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { tripleliftAdapterSpec } from 'modules/tripleliftBidAdapter.js'; +import { tripleliftAdapterSpec, storage } from 'modules/tripleliftBidAdapter.js'; import { newBidder } from 'src/adapters/bidderFactory.js'; import { deepClone } from 'src/utils.js'; import { config } from 'src/config.js'; @@ -379,10 +379,17 @@ describe('triplelift adapter', function () { }; sandbox = sinon.sandbox.create(); logErrorSpy = sinon.spy(utils, 'logError'); + + $$PREBID_GLOBAL$$.bidderSettings = { + triplelift: { + storageAllowed: true + } + }; }); afterEach(() => { sandbox.restore(); utils.logError.restore(); + $$PREBID_GLOBAL$$.bidderSettings = {}; }); it('exists and is an object', function () { @@ -827,6 +834,68 @@ describe('triplelift adapter', function () { expect(request.data.imp[0].fpd.context.data).to.haveOwnProperty('adUnitSpecificAttribute'); expect(request.data.imp[1].fpd).to.not.exist; }); + it('should send 1PlusX data as fpd if localStorage is available and no other fpd is defined', function() { + sandbox.stub(storage, 'getDataFromLocalStorage').callsFake(() => '{"kid":1,"s":"ySRdArquXuBolr/cVv0UNqrJhTO4QZsbNH/t+2kR3gXjbA==","t":"/yVtBrquXuBolr/cVv0UNtx1mssdLYeKFhWFI3Dq1dJnug=="}'); + const request = tripleliftAdapterSpec.buildRequests(bidRequests, bidderRequest); + expect(request.data.ext.fpd).to.deep.equal({ + 'user': { + 'data': [ + { + 'name': 'www.1plusx.com', + 'ext': { + 'kid': 1, + 's': 'ySRdArquXuBolr/cVv0UNqrJhTO4QZsbNH/t+2kR3gXjbA==', + 't': '/yVtBrquXuBolr/cVv0UNtx1mssdLYeKFhWFI3Dq1dJnug==' + } + } + ] + } + }) + }); + it('should append 1PlusX data to existing user.data entries if localStorage is available', function() { + bidderRequest.ortb2 = { + user: { + data: [ + { name: 'dataprovider.com', ext: { segtax: 4 }, segment: [{ id: '1' }] } + ] + } + } + sandbox.stub(storage, 'getDataFromLocalStorage').callsFake(() => '{"kid":1,"s":"ySRdArquXuBolr/cVv0UNqrJhTO4QZsbNH/t+2kR3gXjbA==","t":"/yVtBrquXuBolr/cVv0UNtx1mssdLYeKFhWFI3Dq1dJnug=="}'); + const request = tripleliftAdapterSpec.buildRequests(bidRequests, bidderRequest); + expect(request.data.ext.fpd).to.deep.equal({ + 'user': { + 'data': [ + { 'name': 'dataprovider.com', 'ext': { 'segtax': 4 }, 'segment': [{ 'id': '1' }] }, + { + 'name': 'www.1plusx.com', + 'ext': { + 'kid': 1, + 's': 'ySRdArquXuBolr/cVv0UNqrJhTO4QZsbNH/t+2kR3gXjbA==', + 't': '/yVtBrquXuBolr/cVv0UNtx1mssdLYeKFhWFI3Dq1dJnug==' + } + } + ] + } + }) + }); + it('should not append anything if getDataFromLocalStorage returns null', function() { + bidderRequest.ortb2 = { + user: { + data: [ + { name: 'dataprovider.com', ext: { segtax: 4 }, segment: [{ id: '1' }] } + ] + } + } + sandbox.stub(storage, 'getDataFromLocalStorage').callsFake(() => null); + const request = tripleliftAdapterSpec.buildRequests(bidRequests, bidderRequest); + expect(request.data.ext.fpd).to.deep.equal({ + 'user': { + 'data': [ + { 'name': 'dataprovider.com', 'ext': { 'segtax': 4 }, 'segment': [{ 'id': '1' }] }, + ] + } + }) + }); }); describe('interpretResponse', function () {