Skip to content

Commit

Permalink
Browsi RTD : add split key (prebid#11445)
Browse files Browse the repository at this point in the history
* real time data module,
browsi sub module for real time data,
new hook bidsBackCallback,
fix for config unsubscribe

* change timeout&primary ad server only to auctionDelay
update docs

* support multiple providers

* change promise to callbacks
configure submodule on submodules.json

* bug fixes

* use Prebid ajax

* tests fix

* browsi real time data provider improvements

* real time data module,
browsi sub module for real time data,
new hook bidsBackCallback,
fix for config unsubscribe

* change timeout&primary ad server only to auctionDelay
update docs

* support multiple providers

* change promise to callbacks
configure submodule on submodules.json

* bug fixes

* use Prebid ajax

* tests fix

* browsi real time data provider improvements

* browsi-rtd-kv

* browsi-rtd-kv (lint)

* unit tests

* lint fix
  • Loading branch information
omerDotan authored and DecayConstant committed Jul 18, 2024
1 parent af91057 commit 1f1b4aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
13 changes: 13 additions & 0 deletions modules/browsiRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @property {string} pubKey
* @property {string} url
* @property {?string} keyName
* @property {?string} splitKey
*/

import {deepClone, deepSetValue, isFn, isGptPubadsDefined, isNumber, logError, logInfo, generateUUID} from '../src/utils.js';
Expand All @@ -33,6 +34,7 @@ import {MODULE_TYPE_RTD} from '../src/activities/modules.js';
const MODULE_NAME = 'browsi';

const storage = getStorageManager({moduleType: MODULE_TYPE_RTD, moduleName: MODULE_NAME});
const RANDOM = Math.floor(Math.random() * 10) + 1;

/** @type {ModuleParams} */
let _moduleParams = {};
Expand All @@ -58,12 +60,22 @@ export function addBrowsiTag(data) {
script.setAttribute('id', 'browsi-tag');
script.setAttribute('src', data.u);
script.prebidData = deepClone(typeof data === 'string' ? Object(data) : data)
script.brwRandom = RANDOM;
if (_moduleParams.keyName) {
script.prebidData.kn = _moduleParams.keyName;
}
return script;
}

export function setKeyValue(key) {
if (!key || typeof key !== 'string') return false;
window.googletag = window.googletag || {cmd: []};
window.googletag.cmd = window.googletag.cmd || [];
window.googletag.cmd.push(() => {
window.googletag.pubads().setTargeting(key, RANDOM.toString());
});
}

export function sendPageviewEvent(eventType) {
if (eventType === 'PAGEVIEW') {
window.addEventListener('browsi_pageview', () => {
Expand Down Expand Up @@ -380,6 +392,7 @@ function init(moduleConfig) {
_moduleParams = moduleConfig.params;
if (_moduleParams && _moduleParams.siteKey && _moduleParams.pubKey && _moduleParams.url) {
collectData();
setKeyValue(_moduleParams.splitKey);
} else {
logError('missing params for Browsi provider');
}
Expand Down
33 changes: 29 additions & 4 deletions test/spec/modules/browsiRtdProvider_spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as browsiRTD from '../../../modules/browsiRtdProvider.js';
import {makeSlot} from '../integration/faker/googletag.js';
import * as utils from '../../../src/utils'
import * as events from '../../../src/events';
import * as sinon from 'sinon';
import {sendPageviewEvent} from '../../../modules/browsiRtdProvider.js';
import * as mockGpt from 'test/spec/integration/faker/googletag.js';

describe('browsi Real time data sub module', function () {
const conf = {
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('browsi Real time data sub module', function () {
});

it('should match placement with ad unit', function () {
const slot = makeSlot({code: '/123/abc', divId: 'browsiAd_1'});
const slot = mockGpt.makeSlot({code: '/123/abc', divId: 'browsiAd_1'});

const test1 = browsiRTD.isIdMatchingAdUnit(slot, ['/123/abc']); // true
const test2 = browsiRTD.isIdMatchingAdUnit(slot, ['/123/abc', '/456/def']); // true
Expand All @@ -69,7 +69,7 @@ describe('browsi Real time data sub module', function () {
});

it('should return correct macro values', function () {
const slot = makeSlot({code: '/123/abc', divId: 'browsiAd_1'});
const slot = mockGpt.makeSlot({code: '/123/abc', divId: 'browsiAd_1'});

slot.setTargeting('test', ['test', 'value']);
// slot getTargeting doesn't act like GPT so we can't expect real value
Expand All @@ -90,7 +90,7 @@ describe('browsi Real time data sub module', function () {
});

it('should return prediction from server', function () {
makeSlot({code: 'hasPrediction', divId: 'hasPrediction'});
mockGpt.makeSlot({code: 'hasPrediction', divId: 'hasPrediction'});
const data = {
p: {'hasPrediction': {ps: {0: 0.234}}},
kn: 'bv',
Expand Down Expand Up @@ -266,4 +266,29 @@ describe('browsi Real time data sub module', function () {
expect(eventsEmitSpy.callCount).to.equal(0);
})
})

describe('set targeting - invalid params', function () {
it('should return false if key is undefined', function () {
expect(browsiRTD.setKeyValue()).to.equal(false);
})
it('should return false if key is not string', function () {
expect(browsiRTD.setKeyValue(1)).to.equal(false);
})
})
describe('set targeting - valid params', function () {
let slot;
const splitKey = 'splitTest';
before(() => {
mockGpt.reset();
window.googletag.pubads().clearTargeting();
slot = mockGpt.makeSlot({code: '/123/split', divId: 'split'});
browsiRTD.setKeyValue(splitKey);
window.googletag.cmd.forEach(cmd => cmd());
})
it('should place numeric key value on all slots', function () {
const targetingValue = window.googletag.pubads().getTargeting(splitKey);
expect(targetingValue).to.be.an('array').that.is.not.empty;
expect(targetingValue[0]).to.be.a('string');
})
})
});

0 comments on commit 1f1b4aa

Please sign in to comment.