|
| 1 | +/** |
| 2 | + * This module adds the Rayn provider to the real time data module |
| 3 | + * The {@link module:modules/realTimeData} module is required |
| 4 | + * The module will fetch real-time audience and context data from Rayn |
| 5 | + * @module modules/raynRtdProvider |
| 6 | + * @requires module:modules/realTimeData |
| 7 | + */ |
| 8 | + |
| 9 | +import { MODULE_TYPE_RTD } from '../src/activities/modules.js'; |
| 10 | +import { submodule } from '../src/hook.js'; |
| 11 | +import { getStorageManager } from '../src/storageManager.js'; |
| 12 | +import { deepAccess, deepSetValue, logError, logMessage, mergeDeep } from '../src/utils.js'; |
| 13 | + |
| 14 | +const MODULE_NAME = 'realTimeData'; |
| 15 | +const SUBMODULE_NAME = 'rayn'; |
| 16 | +const RAYN_TCF_ID = 1220; |
| 17 | +const LOG_PREFIX = 'RaynJS: '; |
| 18 | +export const SEGMENTS_RESOLVER = 'rayn.io'; |
| 19 | +export const RAYN_LOCAL_STORAGE_KEY = 'rayn-segtax'; |
| 20 | + |
| 21 | +const defaultIntegration = { |
| 22 | + iabAudienceCategories: { |
| 23 | + v1_1: { |
| 24 | + tier: 6, |
| 25 | + enabled: true, |
| 26 | + }, |
| 27 | + }, |
| 28 | + iabContentCategories: { |
| 29 | + v3_0: { |
| 30 | + tier: 4, |
| 31 | + enabled: true, |
| 32 | + }, |
| 33 | + v2_2: { |
| 34 | + tier: 4, |
| 35 | + enabled: true, |
| 36 | + }, |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +export const storage = getStorageManager({ |
| 41 | + moduleType: MODULE_TYPE_RTD, |
| 42 | + moduleName: SUBMODULE_NAME, |
| 43 | +}); |
| 44 | + |
| 45 | +function init(moduleConfig, userConsent) { |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Create and return ORTB2 object with segtax and segments |
| 51 | + * @param {number} segtax |
| 52 | + * @param {Array} segmentIds |
| 53 | + * @param {number} maxTier |
| 54 | + * @return {Array} |
| 55 | + */ |
| 56 | +export function generateOrtbDataObject(segtax, segment, maxTier) { |
| 57 | + const segmentIds = []; |
| 58 | + |
| 59 | + try { |
| 60 | + Object.keys(segment).forEach(tier => { |
| 61 | + if (tier <= maxTier) { |
| 62 | + segmentIds.push(...segment[tier].map((id) => { |
| 63 | + return { id }; |
| 64 | + })) |
| 65 | + } |
| 66 | + }); |
| 67 | + } catch (error) { |
| 68 | + logError(LOG_PREFIX, error); |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + name: SEGMENTS_RESOLVER, |
| 73 | + ext: { |
| 74 | + segtax, |
| 75 | + }, |
| 76 | + segment: segmentIds, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Generates checksum |
| 82 | + * @param {string} url |
| 83 | + * @returns {string} |
| 84 | + */ |
| 85 | +export function generateChecksum(stringValue) { |
| 86 | + const l = stringValue.length; |
| 87 | + let i = 0; |
| 88 | + let h = 0; |
| 89 | + if (l > 0) while (i < l) h = ((h << 5) - h + stringValue.charCodeAt(i++)) | 0; |
| 90 | + return h.toString(); |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * Gets an object of segtax and segment IDs from LocalStorage |
| 95 | + * or return the default value provided. |
| 96 | + * @param {string} key |
| 97 | + * @return {Object} |
| 98 | + */ |
| 99 | +export function readSegments(key) { |
| 100 | + try { |
| 101 | + return JSON.parse(storage.getDataFromLocalStorage(key)); |
| 102 | + } catch (error) { |
| 103 | + logError(LOG_PREFIX, error); |
| 104 | + return null; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Pass segments to configured bidders, using ORTB2 |
| 110 | + * @param {Object} bidConfig |
| 111 | + * @param {Array} bidders |
| 112 | + * @param {Object} integrationConfig |
| 113 | + * @param {Array} segments |
| 114 | + * @return {void} |
| 115 | + */ |
| 116 | +export function setSegmentsAsBidderOrtb2(bidConfig, bidders, integrationConfig, segments, checksum) { |
| 117 | + const raynOrtb2 = {}; |
| 118 | + |
| 119 | + const raynContentData = []; |
| 120 | + if (integrationConfig.iabContentCategories.v2_2.enabled && segments[checksum] && segments[checksum][6]) { |
| 121 | + raynContentData.push(generateOrtbDataObject(6, segments[checksum][6], integrationConfig.iabContentCategories.v2_2.tier)); |
| 122 | + } |
| 123 | + if (integrationConfig.iabContentCategories.v3_0.enabled && segments[checksum] && segments[checksum][7]) { |
| 124 | + raynContentData.push(generateOrtbDataObject(7, segments[checksum][7], integrationConfig.iabContentCategories.v3_0.tier)); |
| 125 | + } |
| 126 | + if (raynContentData.length > 0) { |
| 127 | + deepSetValue(raynOrtb2, 'site.content.data', raynContentData); |
| 128 | + } |
| 129 | + |
| 130 | + if (integrationConfig.iabAudienceCategories.v1_1.enabled && segments[4]) { |
| 131 | + const raynUserData = [generateOrtbDataObject(4, segments[4], integrationConfig.iabAudienceCategories.v1_1.tier)]; |
| 132 | + deepSetValue(raynOrtb2, 'user.data', raynUserData); |
| 133 | + } |
| 134 | + |
| 135 | + if (!bidders || bidders.length === 0 || !segments || Object.keys(segments).length <= 0) { |
| 136 | + mergeDeep(bidConfig?.ortb2Fragments?.global, raynOrtb2); |
| 137 | + } else { |
| 138 | + const bidderConfig = Object.fromEntries( |
| 139 | + bidders.map((bidder) => [bidder, raynOrtb2]), |
| 140 | + ); |
| 141 | + mergeDeep(bidConfig?.ortb2Fragments?.bidder, bidderConfig); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Real-time data retrieval from Rayn |
| 147 | + * @param {Object} reqBidsConfigObj |
| 148 | + * @param {function} callback |
| 149 | + * @param {Object} config |
| 150 | + * @param {Object} userConsent |
| 151 | + * @return {void} |
| 152 | + */ |
| 153 | +function alterBidRequests(reqBidsConfigObj, callback, config, userConsent) { |
| 154 | + try { |
| 155 | + const checksum = generateChecksum(window.location.href); |
| 156 | + |
| 157 | + const segments = readSegments(RAYN_LOCAL_STORAGE_KEY); |
| 158 | + |
| 159 | + const bidders = deepAccess(config, 'params.bidders'); |
| 160 | + const integrationConfig = mergeDeep(defaultIntegration, deepAccess(config, 'params.integration')); |
| 161 | + |
| 162 | + if (segments && Object.keys(segments).length > 0 && ( |
| 163 | + segments[checksum] || (segments[4] && |
| 164 | + integrationConfig.iabAudienceCategories.v1_1.enabled && |
| 165 | + !integrationConfig.iabContentCategories.v2_2.enabled && |
| 166 | + !integrationConfig.iabContentCategories.v3_0.enabled |
| 167 | + ) |
| 168 | + )) { |
| 169 | + logMessage(LOG_PREFIX, `Segtax data from localStorage: ${JSON.stringify(segments)}`); |
| 170 | + setSegmentsAsBidderOrtb2(reqBidsConfigObj, bidders, integrationConfig, segments, checksum); |
| 171 | + callback(); |
| 172 | + } else if (window.raynJS && typeof window.raynJS.getSegtax === 'function') { |
| 173 | + window.raynJS.getSegtax().then((segtaxData) => { |
| 174 | + logMessage(LOG_PREFIX, `Segtax data from RaynJS: ${JSON.stringify(segtaxData)}`); |
| 175 | + setSegmentsAsBidderOrtb2(reqBidsConfigObj, bidders, integrationConfig, segtaxData, checksum); |
| 176 | + callback(); |
| 177 | + }).catch((error) => { |
| 178 | + logError(LOG_PREFIX, error); |
| 179 | + callback(); |
| 180 | + }); |
| 181 | + } else { |
| 182 | + logMessage(LOG_PREFIX, 'No segtax data'); |
| 183 | + callback(); |
| 184 | + } |
| 185 | + } catch (error) { |
| 186 | + logError(LOG_PREFIX, error); |
| 187 | + callback(); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +export const raynSubmodule = { |
| 192 | + name: SUBMODULE_NAME, |
| 193 | + init: init, |
| 194 | + getBidRequestData: alterBidRequests, |
| 195 | + gvlid: RAYN_TCF_ID, |
| 196 | +}; |
| 197 | + |
| 198 | +submodule(MODULE_NAME, raynSubmodule); |
0 commit comments