forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Parrable/PBID-1
PBID-1 add parrable id system module
- Loading branch information
Showing
3 changed files
with
191 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.