Skip to content

Commit

Permalink
UserID Module: allow userid to ppid sync (#7681)
Browse files Browse the repository at this point in the history
* sync PPID with googletag

* dont need to export

* add log warning if not right length
  • Loading branch information
robertrmartinez authored Nov 18, 2021
1 parent 10516b5 commit 664f284
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
29 changes: 27 additions & 2 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ import { createEidsArray, buildEidPermissions } from './eids.js';
import { getCoreStorageManager } from '../../src/storageManager.js';
import {
getPrebidInternal, isPlainObject, logError, isArray, cyrb53Hash, deepAccess, timestamp, delayExecution, logInfo, isFn,
logWarn, isEmptyStr, isNumber
logWarn, isEmptyStr, isNumber, isGptPubadsDefined
} from '../../src/utils.js';
import includes from 'core-js-pure/features/array/includes.js';

Expand Down Expand Up @@ -184,6 +184,9 @@ export let syncDelay;
/** @type {(number|undefined)} */
export let auctionDelay;

/** @type {(string|undefined)} */
let ppidSource;

/** @param {Submodule[]} submodules */
export function setSubmoduleRegistry(submodules) {
submoduleRegistry = submodules;
Expand Down Expand Up @@ -557,6 +560,26 @@ export function requestBidsHook(fn, reqBidsConfigObj) {
initializeSubmodulesAndExecuteCallbacks(function () {
// pass available user id data to bid adapters
addIdDataToAdUnitBids(reqBidsConfigObj.adUnits || getGlobal().adUnits, initializedSubmodules);

// userSync.ppid should be one of the 'source' values in getUserIdsAsEids() eg pubcid.org or id5-sync.com
const matchingUserId = ppidSource && (getUserIdsAsEids() || []).find(userID => userID.source === ppidSource);
if (matchingUserId && typeof deepAccess(matchingUserId, 'uids.0.id') === 'string') {
const ppidValue = matchingUserId.uids[0].id.replace(/[\W_]/g, '');
if (ppidValue.length >= 32 && ppidValue.length <= 150) {
if (isGptPubadsDefined()) {
window.googletag.pubads().setPublisherProvidedId(ppidValue);
} else {
window.googletag = window.googletag || {};
window.googletag.cmd = window.googletag.cmd || [];
window.googletag.cmd.push(function() {
window.googletag.pubads().setPublisherProvidedId(ppidValue);
});
}
} else {
logWarn(`User ID - Googletag Publisher Provided ID for ${ppidSource} is not between 32 and 150 characters - ${ppidValue}`);
}
}

// calling fn allows prebid to continue processing
fn.call(this, reqBidsConfigObj);
});
Expand Down Expand Up @@ -817,6 +840,7 @@ export function attachIdSystem(submodule) {
* @param {{getConfig:function}} config
*/
export function init(config) {
ppidSource = undefined;
submodules = [];
configRegistry = [];
addedUserIdHook = false;
Expand All @@ -839,9 +863,10 @@ export function init(config) {
}

// listen for config userSyncs to be set
config.getConfig(conf => {
config.getConfig('userSync', conf => {
// Note: support for 'usersync' was dropped as part of Prebid.js 4.0
const userSync = conf.userSync;
ppidSource = userSync.ppid;
if (userSync && userSync.userIds) {
configRegistry = userSync.userIds;
syncDelay = isNumber(userSync.syncDelay) ? userSync.syncDelay : DEFAULT_SYNC_DELAY;
Expand Down
6 changes: 6 additions & 0 deletions test/spec/integration/faker/googletag.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ export function enable() {
window.googletag = {
_slots: [],
_callbackMap: {},
_ppid: undefined,
cmd: [],
pubads: function () {
var self = this;
return {
setPublisherProvidedId: function (ppid) {
self._ppid = ppid;
},

getSlots: function () {
return self._slots;
},
Expand Down
51 changes: 51 additions & 0 deletions test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {flocIdSubmodule} from 'modules/flocIdSystem.js'
import {amxIdSubmodule} from '../../../modules/amxIdSystem.js';
import {akamaiDAPIdSubmodule} from 'modules/akamaiDAPIdSystem.js'
import {kinessoIdSubmodule} from 'modules/kinessoIdSystem.js'
import * as mockGpt from '../integration/faker/googletag.js';

let assert = require('chai').assert;
let expect = require('chai').expect;
Expand Down Expand Up @@ -114,15 +115,21 @@ describe('User ID', function () {

describe('Decorate Ad Units', function () {
beforeEach(function () {
// reset mockGpt so nothing else interferes
mockGpt.disable();
mockGpt.enable();
coreStorage.setCookie('pubcid', '', EXPIRED_COOKIE_DATE);
coreStorage.setCookie('pubcid_alt', 'altpubcid200000', (new Date(Date.now() + 5000).toUTCString()));
sinon.spy(coreStorage, 'setCookie');
sinon.stub(utils, 'logWarn');
});

afterEach(function () {
mockGpt.enable();
$$PREBID_GLOBAL$$.requestBids.removeAll();
config.resetConfig();
coreStorage.setCookie.restore();
utils.logWarn.restore();
});

after(function () {
Expand Down Expand Up @@ -321,6 +328,50 @@ describe('User ID', function () {
expect((getGlobal()).getUserIdsAsEids()).to.deep.equal(createEidsArray((getGlobal()).getUserIds()));
});

it('should set googletag ppid correctly', function () {
let adUnits = [getAdUnitMock()];
setSubmoduleRegistry([amxIdSubmodule, sharedIdSystemSubmodule, identityLinkSubmodule]);
init(config);

config.setConfig({
userSync: {
ppid: 'pubcid.org',
userIds: [
{ name: 'amxId', value: {'amxId': 'amx-id-value-amx-id-value-amx-id-value'} },
{ name: 'pubCommonId', value: {'pubcid': 'pubCommon-id-value-pubCommon-id-value'} },
{ name: 'identityLink', value: {'idl_env': 'identityLink-id-value-identityLink-id-value'} },
]
}
});
// before ppid should not be set
expect(window.googletag._ppid).to.equal(undefined);
requestBidsHook(() => {}, {adUnits});
// ppid should have been set without dashes and stuff
expect(window.googletag._ppid).to.equal('pubCommonidvaluepubCommonidvalue');
});

it('should log a warning if PPID too big or small', function () {
let adUnits = [getAdUnitMock()];
setSubmoduleRegistry([sharedIdSystemSubmodule]);
init(config);

config.setConfig({
userSync: {
ppid: 'pubcid.org',
userIds: [
{ name: 'pubCommonId', value: {'pubcid': 'pubcommonIdValue'} },
]
}
});
// before ppid should not be set
expect(window.googletag._ppid).to.equal(undefined);
requestBidsHook(() => {}, {adUnits});
// ppid should NOT have been set
expect(window.googletag._ppid).to.equal(undefined);
// a warning should have been emmited
expect(utils.logWarn.args[0][0]).to.exist.and.to.contain('User ID - Googletag Publisher Provided ID for pubcid.org is not between 32 and 150 characters - pubcommonIdValue');
});

it('pbjs.refreshUserIds refreshes', function() {
let sandbox = sinon.createSandbox();

Expand Down

0 comments on commit 664f284

Please sign in to comment.