Skip to content

Commit

Permalink
* Added BritePool User ID Submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Faherty committed Aug 7, 2019
1 parent 5cce306 commit 19770e5
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 23 deletions.
118 changes: 118 additions & 0 deletions modules/userId/britepoolIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* This module adds BritePoolId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/unifiedIdSystem
* @requires module:modules/userId
*/

import * as utils from '../../src/utils'
import {ajax} from '../../src/ajax';

/** @type {Submodule} */
export const britepoolIdSubmodule = {
/**
* Used to link submodule with config
* @type {string}
*/
name: 'britepoolId',
/**
* Decode the stored id value for passing to bid requests
* @function
* @param {string} value
* @returns {{britepoolid:string}}
*/
decode(value) {
return {
'britepoolid': value['primaryBPID']
}
},
/**
* Performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleParams} [configParams]
* @returns {function(callback:function)}
*/
getId(submoduleConfigParams, consentData) {
return function(callback) {
const { params, headers, url, getter, errors } = britepoolIdSubmodule.createParams(submoduleConfigParams, consentData);
if (errors.length > 0) {
errors.forEach(error => utils.logError(error));
callback();
return;
}
if (typeof getter === 'function') {
// Use caller provided getter function
getter(params).then(response => {
let responseObj = null;
if (typeof response === 'object') {
responseObj = response;
} else if (typeof response === 'string') {
try {
responseObj = JSON.parse(response);
} catch (error) {
utils.logError(error);
}
}
callback(responseObj);
}).catch(error => {
utils.logError(error);
callback();
});
} else {
ajax(url, {
success: response => {
let responseObj = null;
if (response) {
try {
responseObj = JSON.parse(response);
} catch (error) {
utils.logError(error);
}
}
callback(responseObj);
},
error: error => {
if (error !== '') utils.logError(error);
callback();
}
}, JSON.stringify(params), { customHeaders: headers, contentType: 'application/json', method: 'POST' });
}
}
},
/**
* Helper method to create params for our API call
* @param {SubmoduleParams} [configParams]
* @returns {object} Object with parsed out params
*/
createParams(submoduleConfigParams, consentData) {
let errors = [];
const headers = {};
let params = Object.assign({}, submoduleConfigParams);
if (params.getter) {
// Custom getter will not require other params
if (typeof params.getter !== 'function') {
errors.push(`${MODULE_NAME} - britepoolId submodule requires getter to be a function`);
return { errors };
}
} else {
if (typeof params.api_key !== 'string' || Object.keys(params).length < 2) {
errors.push('User ID - britepoolId submodule requires api_key and at least one identifier to be defined');
return { errors };
}
// Add x-api-key into the header
headers['x-api-key'] = params.api_key;
}
const url = params.url || 'https://api.britepool.com/v1/britepool/id';
const getter = params.getter;
delete params.api_key;
delete params.url;
delete params.getter;
return {
params,
headers,
url,
getter,
errors
};
}
};
70 changes: 70 additions & 0 deletions modules/userId/britepoolIdSystem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## Prebid.js BritePool User ID Submodule

There are two ways to use the BritePool User ID Submodule. The Publisher Kit may be used to create a Prebid usersync.userIds compatible array element, or you may set the Prebid usersync.userIds params manually.

### Publisher Kit

The BritePool Publisher Kit getPrebidUserConfig() will return a usersync.userIds compatible element.

api_key - Provided by BritePool

Set as part of script load:
```
<script async type="text/javascript" id="britepool_publisher_kit" src="https://cdn.britepool.com/publisher_kit.js?api_key=xxx"></script>
```

Within your current setConfig():
```
pbjs.setConfig({
usersync: {
userIds: [window.britepool.getPrebidUserConfig()]
}
});
```

getPrebidUserConfig(identifiers) may optionally be given additional identifiers: aaid, dtid, idfa, ilid, luid, mmid, msid, mwid, rida, ssid, hash.

### Prebid params

Individual params may be set for the BritePool User ID Submodule.
```
pbjs.setConfig({
usersync: {
userIds: [{
name: ’britepoolId’,
storage: {
name: ‘britepoolid’,
type: ‘cookie’,
expires: 30
},
params: {
api_key: ’xxx’,
hash: ’yyyy’ // example identifier
}
}]
}
});
```

### Async Command Queue

Loading dependent scripts asynchronously can be a challenge. The BritePool Publisher Kit provides a command queue similar to the Prebid (cmd, que). This will allow commands to be queued prior and after the script is async loaded.

Within your code:
```
window.britepool = window.britepool || {};
window.britepool.cmd = window.britepool.cmd || [];
```

An example of async loading both BritePool Publisher Kit and Prebid:
```
window.britepool = window.britepool || {};
window.britepool.cmd = window.britepool.cmd || [];
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
window.britepool.cmd.push(function() {
pbjs.que.push(function() {
// Both BritePool Publisher Kit and Prebid are loaded
});
});
```
2 changes: 2 additions & 0 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import CONSTANTS from '../../src/constants.json';
import {module} from '../../src/hook';
import {unifiedIdSubmodule} from './unifiedIdSystem.js';
import {pubCommonIdSubmodule} from './pubCommonIdSystem.js';
import {britepoolIdSubmodule} from './britepoolIdSystem.js';

const MODULE_NAME = 'User ID';
const COOKIE = 'cookie';
Expand Down Expand Up @@ -467,5 +468,6 @@ init(config);
// add submodules after init has been called
attachIdSystem(pubCommonIdSubmodule);
attachIdSystem(unifiedIdSubmodule);
attachIdSystem(britepoolIdSubmodule);

module('userId', attachIdSystem);
Loading

0 comments on commit 19770e5

Please sign in to comment.