Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserID Module: allow userid to ppid sync #7681

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 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,24 @@ 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);
});
}
}
}

// calling fn allows prebid to continue processing
fn.call(this, reqBidsConfigObj);
});
Expand Down Expand Up @@ -817,6 +838,7 @@ export function attachIdSystem(submodule) {
* @param {{getConfig:function}} config
*/
export function init(config) {
ppidSource = undefined;
submodules = [];
configRegistry = [];
addedUserIdHook = false;
Expand All @@ -839,9 +861,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
27 changes: 27 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,12 +115,16 @@ 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');
});

afterEach(function () {
mockGpt.enable();
$$PREBID_GLOBAL$$.requestBids.removeAll();
config.resetConfig();
coreStorage.setCookie.restore();
Expand Down Expand Up @@ -321,6 +326,28 @@ 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('pbjs.refreshUserIds refreshes', function() {
let sandbox = sinon.createSandbox();

Expand Down