|
| 1 | +/** |
| 2 | + * This module adds mobkoiId support to the User ID module |
| 3 | + * The {@link module:modules/userId} module is required. |
| 4 | + * @module modules/mobkoiIdSystem |
| 5 | + * @requires module:modules/userId |
| 6 | + */ |
| 7 | + |
| 8 | +import { submodule } from '../src/hook.js'; |
| 9 | +import { getStorageManager } from '../src/storageManager.js'; |
| 10 | +import { MODULE_TYPE_UID } from '../src/activities/modules.js'; |
| 11 | +import { logError, logInfo, deepAccess, insertUserSyncIframe } from '../src/utils.js'; |
| 12 | + |
| 13 | +const GVL_ID = 898; |
| 14 | +const MODULE_NAME = 'mobkoiId'; |
| 15 | +export const PROD_AD_SERVER_BASE_URL = 'https://adserver.maximus.mobkoi.com'; |
| 16 | +export const EQUATIV_BASE_URL = 'https://sync.smartadserver.com'; |
| 17 | +export const EQUATIV_NETWORK_ID = '5290'; |
| 18 | +/** |
| 19 | + * !IMPORTANT: This value must match the value in mobkoiAnalyticsAdapter.js |
| 20 | + * The name of the parameter that the publisher can use to specify the ad server endpoint. |
| 21 | + */ |
| 22 | +const PARAM_NAME_AD_SERVER_BASE_URL = 'adServerBaseUrl'; |
| 23 | + |
| 24 | +export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME }); |
| 25 | + |
| 26 | +export const mobkoiIdSubmodule = { |
| 27 | + name: MODULE_NAME, |
| 28 | + gvlid: GVL_ID, |
| 29 | + |
| 30 | + decode(value) { |
| 31 | + return value ? { [MODULE_NAME]: value } : undefined; |
| 32 | + }, |
| 33 | + |
| 34 | + getId(userSyncOptions, gdprConsent) { |
| 35 | + logInfo('Getting Equativ SAS ID.'); |
| 36 | + |
| 37 | + if (!storage.cookiesAreEnabled()) { |
| 38 | + logError('Cookies are not enabled. Module will not work.'); |
| 39 | + return { |
| 40 | + id: null |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + const storageName = userSyncOptions && userSyncOptions.storage && userSyncOptions.storage.name; |
| 45 | + if (!storageName) { |
| 46 | + logError('Storage name is not defined. Module will not work.'); |
| 47 | + return { |
| 48 | + id: null |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + const existingId = storage.getCookie(storageName); |
| 53 | + |
| 54 | + if (existingId) { |
| 55 | + logInfo(`Found "${storageName}" from local cookie: "${existingId}"`); |
| 56 | + return { id: existingId }; |
| 57 | + } |
| 58 | + |
| 59 | + logInfo(`Cannot found "${storageName}" in local cookie with name.`); |
| 60 | + return { |
| 61 | + callback: () => { |
| 62 | + return new Promise((resolve, _reject) => { |
| 63 | + utils.requestEquativSasId( |
| 64 | + userSyncOptions, |
| 65 | + gdprConsent, |
| 66 | + (sasId) => { |
| 67 | + if (!sasId) { |
| 68 | + logError('Equativ SAS ID is empty'); |
| 69 | + resolve({ id: null }); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + logInfo(`Fetched Equativ SAS ID: "${sasId}"`); |
| 74 | + storage.setCookie(storageName, sasId, userSyncOptions.storage.expires); |
| 75 | + logInfo(`Stored Equativ SAS ID in local cookie with name: "${storageName}"`); |
| 76 | + resolve({ id: sasId }); |
| 77 | + } |
| 78 | + ); |
| 79 | + }); |
| 80 | + } |
| 81 | + }; |
| 82 | + }, |
| 83 | +}; |
| 84 | + |
| 85 | +submodule('userId', mobkoiIdSubmodule); |
| 86 | + |
| 87 | +export const utils = { |
| 88 | + requestEquativSasId(syncUserOptions, gdprConsent, onCompleteCallback) { |
| 89 | + logInfo('Start requesting Equativ SAS ID'); |
| 90 | + const adServerBaseUrl = deepAccess( |
| 91 | + syncUserOptions, |
| 92 | + `params.${PARAM_NAME_AD_SERVER_BASE_URL}`) || PROD_AD_SERVER_BASE_URL; |
| 93 | + |
| 94 | + const equativPixelUrl = utils.buildEquativPixelUrl(syncUserOptions, gdprConsent); |
| 95 | + logInfo('Equativ SAS ID request URL:', equativPixelUrl); |
| 96 | + |
| 97 | + const url = adServerBaseUrl + '/pixeliframe?' + |
| 98 | + 'pixelUrl=' + encodeURIComponent(equativPixelUrl) + |
| 99 | + '&cookieName=sas_uid'; |
| 100 | + |
| 101 | + /** |
| 102 | + * Listen for messages from the iframe |
| 103 | + */ |
| 104 | + window.addEventListener('message', function(event) { |
| 105 | + switch (event.data.type) { |
| 106 | + case 'MOBKOI_PIXEL_SYNC_COMPLETE': |
| 107 | + const sasUid = event.data.syncData; |
| 108 | + logInfo('Parent window Sync completed. SAS ID:', sasUid); |
| 109 | + onCompleteCallback(sasUid); |
| 110 | + break; |
| 111 | + case 'MOBKOI_PIXEL_SYNC_ERROR': |
| 112 | + logError('Parent window Sync failed:', event.data.error); |
| 113 | + onCompleteCallback(null); |
| 114 | + break; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + insertUserSyncIframe(url, () => { |
| 119 | + logInfo('insertUserSyncIframe loaded'); |
| 120 | + }); |
| 121 | + |
| 122 | + // Return the URL for testing purposes |
| 123 | + return url; |
| 124 | + }, |
| 125 | + |
| 126 | + /** |
| 127 | + * Build a pixel URL that will be placed in an iframe to fetch the Equativ SAS ID |
| 128 | + */ |
| 129 | + buildEquativPixelUrl(syncUserOptions, gdprConsent) { |
| 130 | + logInfo('Generating Equativ SAS ID request URL'); |
| 131 | + const adServerBaseUrl = |
| 132 | + deepAccess( |
| 133 | + syncUserOptions, |
| 134 | + `params.${PARAM_NAME_AD_SERVER_BASE_URL}`) || PROD_AD_SERVER_BASE_URL; |
| 135 | + |
| 136 | + const gdprConsentString = gdprConsent && gdprConsent.gdprApplies ? gdprConsent.consentString : ''; |
| 137 | + const smartServerUrl = EQUATIV_BASE_URL + '/getuid?' + |
| 138 | + `url=` + encodeURIComponent(`${adServerBaseUrl}/getPixel?value=`) + '[sas_uid]' + |
| 139 | + `&gdpr_consent=${gdprConsentString}` + |
| 140 | + `&nwid=${EQUATIV_NETWORK_ID}`; |
| 141 | + |
| 142 | + return smartServerUrl; |
| 143 | + } |
| 144 | +}; |
0 commit comments