Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LiveIntent User ID Module: Eliminating live-connect NPM Dependency #12167

Merged
merged 17 commits into from
Sep 30, 2024
Merged
173 changes: 173 additions & 0 deletions libraries/liveIntentId/externalIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { logError } from '../../src/utils.js';
import { gdprDataHandler, uspDataHandler, gppDataHandler } from '../../src/adapterManager.js';
import { submodule } from '../../src/hook.js';
import { DEFAULT_AJAX_TIMEOUT, MODULE_NAME, parseRequestedAttributes, composeIdObject, eids, GVLID, PRIMARY_IDS } from './shared.js'

// Reference to the client for the liQHub.
let cachedClientRef

/**
* This function is used in tests.
*/
export function resetSubmodule() {
cachedClientRef = undefined
}

window.liQHub = window.liQHub ?? []

function initializeClient(configParams) {
// Only initialize once.
if (cachedClientRef != null) return cachedClientRef

const clientRef = {}

const clientDetails = { name: 'prebid', version: '$prebid.version$' }

const collectConfig = configParams.liCollectConfig ?? {};

let integration
if (collectConfig.appId != null) {
ncolletti marked this conversation as resolved.
Show resolved Hide resolved
integration = { type: 'application', appId: collectConfig.appId, publisherId: configParams.publisherId }
} else if (configParams.distributorId != null && configParams.publisherId == null) {
integration = { type: 'distributor', distributorId: configParams.distributorId }
} else {
integration = { type: 'custom', publisherId: configParams.publisherId, distributorId: configParams.distributorId }
}

const partnerCookies = new Set(configParams.identifiersToResolve ?? []);

const collectSettings = { timeout: collectConfig.ajaxTimeout ?? DEFAULT_AJAX_TIMEOUT }

let identityPartner
if (collectConfig.appId == null && configParams.distributorId != null) {
identityPartner = configParams.distributorId
} else if (configParams.partner != null) {
identityPartner = configParams.partner
} else {
identityPartner = 'prebid'
}

const resolveSettings = {
identityPartner,
timeout: configParams.ajaxTimeout ?? DEFAULT_AJAX_TIMEOUT
}

let idCookieSettings
if (configParams.fpid != null) {
const fpidConfig = configParams.fpid
let source
if (fpidConfig.strategy === 'html5') {
source = 'local_storage'
} else {
source = fpidConfig.strategy
}
idCookieSettings = { idCookieSettings: { type: 'provided', source, key: fpidConfig.name } };
} else {
idCookieSettings = {}
}

function loadConsent() {
const consent = {}
const usPrivacyString = uspDataHandler.getConsentData();
if (usPrivacyString != null) {
consent.usPrivacy = { consentString: usPrivacyString }
}
const gdprConsent = gdprDataHandler.getConsentData()
if (gdprConsent != null) {
consent.gdpr = gdprConsent
}
const gppConsent = gppDataHandler.getConsentData();
if (gppConsent != null) {
consent.gpp = { consentString: gppConsent.gppString, applicableSections: gppConsent.applicableSections }
}

return consent
}
const consent = loadConsent()

window.liQHub.push({
type: 'register_client',
clientRef,
clientDetails,
integration,
consent,
partnerCookies,
collectSettings,
...idCookieSettings,
resolveSettings
})

if (configParams.emailHash != null) {
window.liQHub.push({ type: 'collect', clientRef, sourceEvent: { hash: configParams.emailHash } })
}

cachedClientRef = clientRef
return clientRef
}

/**
* Create requestedAttributes array to pass to LiveConnect.
* @function
* @param {Object} overrides - object with boolean values that will override defaults { 'foo': true, 'bar': false }
* @returns {Array}
*/

function resolve(configParams, clientRef, callback) {
function onFailure(error) {
logError(`${MODULE_NAME}: ID fetch encountered an error: `, error);
callback();
}

const onSuccess = [{ type: 'callback', callback }]

window.liQHub.push({
type: 'resolve',
clientRef,
requestedAttributes: parseRequestedAttributes(configParams.requestedAttributesOverrides),
onFailure,
onSuccess
})
}

/**
* @typedef {import('../../modules/userId/index.js').Submodule} Submodule
*/

/** @type {Submodule} */
export const liveIntentExternalIdSubmodule = {
/**
* Used to link submodule with config.
* @type {string}
*/
name: MODULE_NAME,
gvlid: GVLID,

/**
* Decode the stored id value for passing to bid requests.
* @function
*/
decode(value, config) {
const configParams = config?.params ?? {};

// Ensure client is initialized and we fired at least one collect request.
initializeClient(configParams)

return composeIdObject(value);
},

/**
* Performs action to obtain id and return a value in the callback's response argument.
* @function
*/
getId(config) {
const configParams = config?.params ?? {};

const clientRef = initializeClient(configParams)

return { callback: function(cb) { resolve(configParams, clientRef, cb); } };
},
primaryIds: PRIMARY_IDS,
eids
};

submodule('userId', liveIntentExternalIdSubmodule);
Loading