Skip to content

Commit

Permalink
Merge pull request #2 from Parrable/PBID-1
Browse files Browse the repository at this point in the history
PBID-1 add parrable id system module
  • Loading branch information
eyas-ranjous authored Aug 7, 2019
2 parents 8302d96 + 16a7444 commit 07c44bd
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 21 deletions.
102 changes: 102 additions & 0 deletions modules/parrableIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* This module adds Parrable to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/parrableIdSystem
* @requires module:modules/userId
*/

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

function buildUrl(config, currentStoredId) {
const endpoint = 'https://h.parrable.com/prebid';

const eid = currentStoreId ? currentStoredId : null;

// @TODO: Any other parameters? Even null/empty ones?
const data = {
eid: eid,
trackers: config.partners
};

return endpoint +
'?data=' + btoa(JSON.stringify(data)) +
'&_rand=' + Math.random();
};

function getOrRefreshId(configParams, consentData, currentStoredId) {
if (!configParams) {
utils.logError('User ID - parrableId submodule requires configParams');
return;
}
if (configParams.partners && !Array.isArray(configParams.partners)) {
utils.logError('User ID - parrableId submodule requires partners to be an array');
return;
}
if (!configParams.storage) {
utils.logError('User ID - parrableId submodule requires storage config');
return;
}
// @TODO require storage type is cookie ???
// @TODO require that cookie name is _parrable_eid ???

const url = buildUrl(configParams, currentStoredId);
return function (callback) {
ajax(url, response => {
let eid;
if (response) {
try {
let responseObj = JSON.parse(response);
eid = responseObj ? responseObj['eid'] : undefined;
} catch (error) {
utils.logError(error);
}
}
callback(eid);
}, undefined, { method: 'GET' });
};
};

/** @type {Submodule} */
export const parrableIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: 'parrableId',
/**
* decode the stored id value for passing to bid requests
* @function
* @param {Object|string} value
* @return {(Object|undefined}
*/
decode(value) {
return (value && typeof value === 'string') ? { 'parrableid': value } : undefined;
},

/**
* performs action to refresh existing id and return a value in the callback's response argument
* @function
* @param {SubmoduleParams} [configParams]
* @param {ConsentData} [consentData]
* @param {Object|string} currentStoredId
* @returns {function(callback:function)}
*/
refreshId(configParams, consentData, currentStoredId) {
return getOrRefreshId(configParams, consentData, currentStoredId);
},

/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleParams} [configParams]
* @param {ConsentData} [consentData]
* @returns {function(callback:function)}
*/
getId(configParams, consentData) {
return getOrRefreshId(configParams, consentData);
}
};

submodule('userId', parrableIdSubmodule);
20 changes: 20 additions & 0 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
* @return {(Object|function)} id data or a callback, the callback is called on the auction end event
*/

/**
* @function
* @summary performs action to refresh stored id (only one of Submodule#getId or Submodule#refreshId will be called)
* @name Submodule#refreshId
* @param {SubmoduleParams} configParams
* @param {ConsentData} consentData
* @param {Object|string} currentStoredId
* @return {function} a callback, the callback is called on the auction end event
*/

/**
* @function
* @summary decode a stored value for passing to bid requests
Expand Down Expand Up @@ -314,6 +324,16 @@ function initSubmodules(submodules, consentData) {
if (storedId) {
// cache decoded value (this is copied to every adUnit bid)
submodule.idObj = submodule.submodule.decode(storedId);

if (typeof submodule.submodule.refreshId === 'function') {
// if defined, refreshId will return a function that will load an updated id to be stored for subsequent use
const refreshIdResult = submodule.submodule.refreshId(submodule.config.params, consentData, storedId);

// a function to be called later is expected, otherwise ignore
if (typeof refreshIdResult === 'function') {
submodule.callback = refreshIdResult;
}
}
} else {
// getId will return user id data or a function that will load the data
const getIdResult = submodule.submodule.getId(submodule.config.params, consentData);
Expand Down
Loading

0 comments on commit 07c44bd

Please sign in to comment.