Skip to content

Commit

Permalink
LiveIntent User ID Module: Eliminating live-connect NPM Dependency (#…
Browse files Browse the repository at this point in the history
…12167)

* CM-1260 Implement LiveIntent Prebid User Id Module Based On The Hub

* add tests and rebase to the correct master

* add fpid handling

* fix naming

* Revert superfluous changes

* Update test/spec/modules/liveIntentIdHubSystem_spec.js

Co-authored-by: Viktor Dreiling <[email protected]>

* Fix eids generation

* comment

* only keep collect and rename

* wrong name

* rename and fix import

* Clean up after merge

* Lint

* Lint more

* Put back module loading

* fix import

---------

Co-authored-by: Peixun Zhang <[email protected]>
Co-authored-by: PeiZ <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 447a3c4 commit bdab819
Show file tree
Hide file tree
Showing 8 changed files with 1,034 additions and 416 deletions.
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) {
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

0 comments on commit bdab819

Please sign in to comment.