diff --git a/modules/.submodules.json b/modules/.submodules.json index fc69dd276a3..54da15ca438 100644 --- a/modules/.submodules.json +++ b/modules/.submodules.json @@ -30,6 +30,7 @@ "browsiRtdProvider", "haloRtdProvider", "jwplayerRtdProvider", + "pubwiseRtdProvider", "reconciliationRtdProvider", "geoedgeRtdProvider" ] diff --git a/modules/pubwiseRtdProvider.js b/modules/pubwiseRtdProvider.js new file mode 100644 index 00000000000..5be76c79313 --- /dev/null +++ b/modules/pubwiseRtdProvider.js @@ -0,0 +1,103 @@ +/** + * This module adds the PubWise.io provider to the Real Time Data module (rtdModule) + * The {@link module:modules/realTimeData} module is required + * The module allows page level traffic quality assessments as well as audience ad targeting information + * @requires module:modules/realTimeData + */ + +import * as utils from '../src/utils.js'; +import { submodule } from '../src/hook.js'; +import { getGlobal } from '../src/prebidGlobal.js'; +import { ajaxBuilder } from '../src/ajax.js'; + +let _moduleParams = {}; +let _assessmentData = {Quality: {Result: 0}}; +const LogPrepend = 'PubWise: '; + +function init(provider, userConsent) { + const win = window.top; + const doc = win.document; + + _moduleParams = provider.params; + if (_moduleParams && _moduleParams.siteId) { + let paramData = { + ...{ + siteId: _moduleParams.siteId, + pageUrl: `${doc.location.protocol}//${doc.location.host}${doc.location.pathname}`, + }, + ...(document.referrer ? {r: document.referrer} : {}), + ...(document.title ? {at: document.title} : {}) + }; + getAssessment(`${_moduleParams.endpoint}/traffic/quality/?${toUrlParams(paramData)}`); + } else { + utils.logError(LogPrepend + 'missing params for PubWise audience rtd module'); + } + return true; +} + +/** + * serialize object and return query params string + * @param {Object} data + * @return {string} + */ +function toUrlParams(data) { + return Object.keys(data) + .map(key => key + '=' + encodeURIComponent(data[key])) + .join('&'); +} + +function getAssessment(url) { + let ajax = ajaxBuilder(); + + ajax(url, + { + success: function (response, req) { + if (req.status === 200) { + try { + const data = JSON.parse(response); + if (data && data.Quality) { + setData({Quality: data.Quality}); + } else { + setData({Quality: {Result: 0}}); + } + } catch (err) { + utils.logError(LogPrepend + 'unable to parse assessment data'); + setData({Quality: {Result: 0}}) + } + } else if (req.status === 204) { + // unrecognized site key + setData({Quality: {Result: 0}}); + } + }, + error: function () { + setData({Quality: {Result: 0}}); + utils.logError(LogPrepend + 'unable to get assessment data'); + } + } + ); +} + +function setData(data) { + _assessmentData = data; +} + +function processBidRequestData(reqBidsConfigObj, onDone, config, userConsent) { + utils.logInfo(LogPrepend + 'Assessment Data', _assessmentData) + if (_assessmentData.Quality && _assessmentData.Quality.Result && _assessmentData.Quality.Result == 1) { + const adUnits = reqBidsConfigObj.adUnits || getGlobal().adUnits; + adUnits.forEach(adUnit => { + delete adUnit.bids; + }); + reqBidsConfigObj.adUnitCodes = {}; + reqBidsConfigObj.timeout = 1; + } + onDone(); +} + +export const pubwiseRtdSubmodule = { + name: 'pubwise', + init: init, + getBidRequestData: processBidRequestData +}; + +submodule('realTimeData', pubwiseRtdSubmodule); diff --git a/modules/pubwiseRtdProvider.md b/modules/pubwiseRtdProvider.md new file mode 100644 index 00000000000..e7dedfdbd32 --- /dev/null +++ b/modules/pubwiseRtdProvider.md @@ -0,0 +1,9 @@ +# Overview + +Module Name: PubWise Audience Provider +Module Type: Rtd Provider +Maintainer: support@pubwise.io + +# Description + +RTD Module for PubWise.io. Includes audience functionality, traffic quality assessment and more. \ No newline at end of file