Skip to content

Commit faac597

Browse files
About7SharksZac Carlin
andauthored
Sonobi Bid Adapter: add IntentIq Id (prebid#9649)
* Add intentIq Id to trinity request. * Minimized function to needed components. * Added return statemen. * Fixed return statement linter rule. * Changes suggested in PR review. * Added date to trinity request. Updated tests. --------- Co-authored-by: Zac Carlin <zcarlin@C02DP5X4MD6R.local>
1 parent 0eb76d7 commit faac597

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

modules/sonobiBidAdapter.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BANNER, VIDEO } from '../src/mediaTypes.js';
44
import { config } from '../src/config.js';
55
import { Renderer } from '../src/Renderer.js';
66
import { userSync } from '../src/userSync.js';
7+
import { bidderSettings } from '../src/bidderSettings.js';
78
const BIDDER_CODE = 'sonobi';
89
const STR_ENDPOINT = 'https://apex.go.sonobi.com/trinity.json';
910
const PAGEVIEW_ID = generateUUID();
@@ -49,6 +50,7 @@ export const spec = {
4950

5051
return true;
5152
},
53+
5254
/**
5355
* Make a server request from the list of BidRequests.
5456
*
@@ -93,7 +95,7 @@ export const spec = {
9395
'lib_name': 'prebid',
9496
'lib_v': '$prebid.version$',
9597
'us': 0,
96-
98+
'iqid': bidderSettings.get(BIDDER_CODE, 'storageAllowed') ? JSON.stringify(loadOrCreateFirstPartyData()) : null,
9799
};
98100

99101
const fpd = bidderRequest.ortb2;
@@ -388,6 +390,67 @@ export function _getPlatform(context = window) {
388390
}
389391
return 'desktop';
390392
}
393+
/**
394+
* Check for local storage
395+
* Generate a UUID for the user if one does not exist in local storage
396+
* Store the UUID in local storage for future use
397+
* @return {object} firstPartyData - Data object containing first party information
398+
*/
399+
function loadOrCreateFirstPartyData() {
400+
var localStorageEnabled;
401+
402+
var FIRST_PARTY_KEY = '_iiq_fdata';
403+
var tryParse = function (data) {
404+
try {
405+
return JSON.parse(data);
406+
} catch (err) {
407+
return null;
408+
}
409+
};
410+
var readData = function (key) {
411+
if (hasLocalStorage()) {
412+
return window.localStorage.getItem(key);
413+
}
414+
return null;
415+
};
416+
var hasLocalStorage = function () {
417+
if (typeof localStorageEnabled != 'undefined') { return localStorageEnabled; } else {
418+
try {
419+
localStorageEnabled = !!window.localStorage;
420+
return localStorageEnabled;
421+
} catch (e) {
422+
localStorageEnabled = false;
423+
}
424+
}
425+
return false;
426+
};
427+
var generateGUID = function () {
428+
var d = new Date().getTime();
429+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
430+
var r = (d + Math.random() * 16) % 16 | 0;
431+
d = Math.floor(d / 16);
432+
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
433+
});
434+
};
435+
var storeData = function (key, value) {
436+
try {
437+
if (hasLocalStorage()) {
438+
window.localStorage.setItem(key, value);
439+
}
440+
} catch (error) {
441+
return null;
442+
}
443+
};
444+
var firstPartyData = tryParse(readData(FIRST_PARTY_KEY));
445+
if (!firstPartyData || !firstPartyData.pcid) {
446+
var firstPartyId = generateGUID();
447+
firstPartyData = { pcid: firstPartyId, pcidDate: Date.now() };
448+
} else if (firstPartyData && !firstPartyData.pcidDate) {
449+
firstPartyData.pcidDate = Date.now();
450+
}
451+
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData));
452+
return firstPartyData;
453+
};
391454

392455
function newRenderer(adUnitCode, bid, rendererOptions = {}) {
393456
const renderer = Renderer.install({

test/spec/modules/sonobiBidAdapter_spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ describe('SonobiBidAdapter', function () {
238238
});
239239

240240
describe('.buildRequests', function () {
241+
before(function () {
242+
$$PREBID_GLOBAL$$.bidderSettings = {
243+
sonobi: {
244+
storageAllowed: true
245+
}
246+
};
247+
});
241248
let sandbox;
242249
beforeEach(function () {
243250
sinon.stub(userSync, 'canBidderRegisterSync');
@@ -389,6 +396,10 @@ describe('SonobiBidAdapter', function () {
389396
expect(bidRequests.data.coppa).to.equal(0);
390397
});
391398

399+
it('should have storageAllowed set to true', function () {
400+
expect($$PREBID_GLOBAL$$.bidderSettings.sonobi.storageAllowed).to.be.true;
401+
});
402+
392403
it('should return a properly formatted request', function () {
393404
const bidRequests = spec.buildRequests(bidRequest, bidderRequests)
394405
const bidRequestsPageViewID = spec.buildRequests(bidRequest, bidderRequests)
@@ -398,6 +409,8 @@ describe('SonobiBidAdapter', function () {
398409
expect(bidRequests.data.ref).not.to.be.empty
399410
expect(bidRequests.data.s).not.to.be.empty
400411
expect(bidRequests.data.pv).to.equal(bidRequestsPageViewID.data.pv)
412+
expect(JSON.parse(bidRequests.data.iqid).pcid).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)
413+
expect(JSON.parse(bidRequests.data.iqid).pcidDate).to.match(/^[0-9]{13}$/)
401414
expect(bidRequests.data.hfa).to.not.exist
402415
expect(bidRequests.bidderRequests).to.eql(bidRequest);
403416
expect(bidRequests.data.ref).to.equal('overrides_top_window_location');

0 commit comments

Comments
 (0)