UNPKG

@m10s/cmp

Version:

Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP

167 lines (146 loc) 5.6 kB
import { appendPulseTrackingToEvents } from './pulse'; import { debug, getCookie, setCookie, eraseCookie } from './utils'; import { appendSentryEvents } from './sentry/index'; import { getIdentityObject } from './schibsted-account'; import { setUserId, openPrivacySettings, triggerLogin, setTargetingParams } from './utils/psi'; import { isWebView } from './utils/mobile'; export function setupPsiConfig(_window, _document, _navigator, config, type) { getPsiConfig(_window, _document, _navigator, config, type); const { jsSdkVersion, id, realm, clientId, pulseTracker, referrer, state, pulseObjectName, showInWebview, identityObject, identityObjectName, disableSentry, disableNativeConsentCheck, enableUserCentric, groupId, ...spConfig } = config; return initConfig(_window, spConfig); } export async function setupPsiConfigAsync(_window, _document, _navigator, config, type) { getPsiConfig(_window, _document, _navigator, config, type); const { jsSdkVersion, id, realm, clientId, pulseTracker, referrer, state, pulseObjectName, showInWebview, identityObject, identityObjectName, disableSentry, disableNativeConsentCheck, enableUserCentric, groupId, ...spConfig } = config; try { const identityObject = getIdentityObject(_window); const isLoggedInUser = getCookie('CMP:isLoggedIn') || await identityObject.isLoggedIn(); if (!isLoggedInUser) return initConfig(_window, spConfig); identityObject.on('logout', function () { eraseCookie('CMP:isLoggedIn'); }); if (!getCookie('CMP:isLoggedIn')) { setCookie('CMP:isLoggedIn', true); } // Targeting parameter to show PSI to logged-in users setTargetingParams(config, 'loggedin', true); const configObject = { externalParty: 'sourcepoint', optionalSuffix: config.groupId }; const authId = await identityObject.getExternalId(configObject.externalParty, configObject.optionalSuffix); const userCentricParam = setTargetingParams(config, 'enableUserCentric', true); initConfig(_window, { authId, targetingParams: userCentricParam, ...spConfig }); } catch (err) { console.error('Identity: ', err); initConfig(_window, spConfig); } } function getPsiConfig(_window, _document, _navigator, config, type) { if (!config || !config.baseEndpoint || !config.propertyId || !config.consentLanguage) { throw new Error('One of missing: baseEndpoint, propertyId, consentLanguage'); } appendPulseTrackingToEvents(_window, config); appendSentryEvents(config); config.accountId = config.accountId || 1960; // empty object should be assigned to make Custom Messages work config.custom = {}; if (type !== 'psi') { config.gdpr = { includeTcfApi: type === 'tcf' }; } if (config.propertyHref) config.joinHref = true; /* workaround not to use async methods (such as isLoggedIn or hasSession) because the snippet would have to be loaded synchronously which is a breaking change */ const userId = config.userId || getIdentityObject(_window, config)?._session.userId; if (userId) setTargetingParams(config, 'loggedin', 'true'); const clientId = config.clientId || getIdentityObject(window, config)?.clientId; function getTargetingParamsFromCookie() { const cookieNamePattern = /(_sch_cmp_[_a-z0-9]+)=(.+)/i; const matches = _document.cookie .split(';') .filter((it) => cookieNamePattern.test(it)) .map((it) => it.split('=').map((it) => it.trim())); matches.forEach(([name, value]) => setTargetingParams(config, name, value)); } // targeting parameter for webview if (isWebView(_navigator)) { setTargetingParams(config, 'mobile-webview', config.showInWebview ? config.showInWebview : true) } const ref = _document?.referrer?.split('/')[2]; if (ref) { setTargetingParams(config, 'referrer-hostname', ref); } getTargetingParamsFromCookie(); if ( config.propertyHref && _document.location.search && config.propertyHref.indexOf('?') === -1 ) { config.propertyHref += _document.location.search; } const { pulseTracker, referrer, state, pulseObjectName, showInWebview, identityObject, identityObjectName, enableUserCentric, groupId } = config; _window.psi = _window.psi || {}; _window.psi.isLoggedInUser = !!userId; _window.psi.setUserId = setUserId; _window.psi.openPrivacySettings = openPrivacySettings; _window.psi.pulseTracker = pulseTracker; _window.psi.clientId = clientId; _window.psi.referrer = referrer; _window.psi.state = state; _window.psi.pulseObjectName = pulseObjectName; _window.psi.showInWebview = showInWebview; _window.psi.identityObject = identityObject; _window.psi.identityObjectName = identityObjectName; _window.psi.triggerLogin = triggerLogin; _window.psi.enableUserCentric = enableUserCentric; _window.psi.groupId = groupId; } function initConfig(_window, spConfig) { _window._sp_ = _window._sp_ || {}; _window._sp_.config = spConfig; debug('Sourcepoint initiated with config: ', spConfig); }