Skip to content

Commit

Permalink
Sonobi Bid Adapter: add IntentIq Id (prebid#9649)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
About7Sharks and Zac Carlin authored Mar 27, 2023
1 parent 0eb76d7 commit faac597
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
65 changes: 64 additions & 1 deletion modules/sonobiBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { config } from '../src/config.js';
import { Renderer } from '../src/Renderer.js';
import { userSync } from '../src/userSync.js';
import { bidderSettings } from '../src/bidderSettings.js';
const BIDDER_CODE = 'sonobi';
const STR_ENDPOINT = 'https://apex.go.sonobi.com/trinity.json';
const PAGEVIEW_ID = generateUUID();
Expand Down Expand Up @@ -49,6 +50,7 @@ export const spec = {

return true;
},

/**
* Make a server request from the list of BidRequests.
*
Expand Down Expand Up @@ -93,7 +95,7 @@ export const spec = {
'lib_name': 'prebid',
'lib_v': '$prebid.version$',
'us': 0,

'iqid': bidderSettings.get(BIDDER_CODE, 'storageAllowed') ? JSON.stringify(loadOrCreateFirstPartyData()) : null,
};

const fpd = bidderRequest.ortb2;
Expand Down Expand Up @@ -388,6 +390,67 @@ export function _getPlatform(context = window) {
}
return 'desktop';
}
/**
* Check for local storage
* Generate a UUID for the user if one does not exist in local storage
* Store the UUID in local storage for future use
* @return {object} firstPartyData - Data object containing first party information
*/
function loadOrCreateFirstPartyData() {
var localStorageEnabled;

var FIRST_PARTY_KEY = '_iiq_fdata';
var tryParse = function (data) {
try {
return JSON.parse(data);
} catch (err) {
return null;
}
};
var readData = function (key) {
if (hasLocalStorage()) {
return window.localStorage.getItem(key);
}
return null;
};
var hasLocalStorage = function () {
if (typeof localStorageEnabled != 'undefined') { return localStorageEnabled; } else {
try {
localStorageEnabled = !!window.localStorage;
return localStorageEnabled;
} catch (e) {
localStorageEnabled = false;
}
}
return false;
};
var generateGUID = function () {
var d = new Date().getTime();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
});
};
var storeData = function (key, value) {
try {
if (hasLocalStorage()) {
window.localStorage.setItem(key, value);
}
} catch (error) {
return null;
}
};
var firstPartyData = tryParse(readData(FIRST_PARTY_KEY));
if (!firstPartyData || !firstPartyData.pcid) {
var firstPartyId = generateGUID();
firstPartyData = { pcid: firstPartyId, pcidDate: Date.now() };
} else if (firstPartyData && !firstPartyData.pcidDate) {
firstPartyData.pcidDate = Date.now();
}
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData));
return firstPartyData;
};

function newRenderer(adUnitCode, bid, rendererOptions = {}) {
const renderer = Renderer.install({
Expand Down
13 changes: 13 additions & 0 deletions test/spec/modules/sonobiBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ describe('SonobiBidAdapter', function () {
});

describe('.buildRequests', function () {
before(function () {
$$PREBID_GLOBAL$$.bidderSettings = {
sonobi: {
storageAllowed: true
}
};
});
let sandbox;
beforeEach(function () {
sinon.stub(userSync, 'canBidderRegisterSync');
Expand Down Expand Up @@ -389,6 +396,10 @@ describe('SonobiBidAdapter', function () {
expect(bidRequests.data.coppa).to.equal(0);
});

it('should have storageAllowed set to true', function () {
expect($$PREBID_GLOBAL$$.bidderSettings.sonobi.storageAllowed).to.be.true;
});

it('should return a properly formatted request', function () {
const bidRequests = spec.buildRequests(bidRequest, bidderRequests)
const bidRequestsPageViewID = spec.buildRequests(bidRequest, bidderRequests)
Expand All @@ -398,6 +409,8 @@ describe('SonobiBidAdapter', function () {
expect(bidRequests.data.ref).not.to.be.empty
expect(bidRequests.data.s).not.to.be.empty
expect(bidRequests.data.pv).to.equal(bidRequestsPageViewID.data.pv)
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}$/)
expect(JSON.parse(bidRequests.data.iqid).pcidDate).to.match(/^[0-9]{13}$/)
expect(bidRequests.data.hfa).to.not.exist
expect(bidRequests.bidderRequests).to.eql(bidRequest);
expect(bidRequests.data.ref).to.equal('overrides_top_window_location');
Expand Down

0 comments on commit faac597

Please sign in to comment.