@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
86 lines (73 loc) • 3.78 kB
JavaScript
import { debug, convertToBoolean, removeEmpty } from '../utils';
import { messageTypes, sourceTypes, pulseConsentsDefaultObject, consentStatuses } from './variables';
export function cmpTrack(_window, actor, event, data) {
debug(`Pulse: ${event}(actor, data):`, actor, data);
pulseObject(_window, (sdk) => {
if (!sdk || typeof sdk.track !== 'function') return;
sdk.track(event, {
schema: 'http://schema.schibsted.com/events/tracker-event.json/348.json',
object: { ...data, ...cmpTrack.pulseCommonData },
provider: { component: 'CMP-Marketplaces' },
actor,
});
});
}
export function pulseObject(_window, callback) {
if (_window.psi.pulseTracker) return callback(_window.psi.pulseTracker);
const windowPulse = _window.psi.pulseObjectName || 'pulse';
if (typeof _window[windowPulse] !== 'function') {
return console.error('No Pulse integration detected. Aborting.');
};
return _window[windowPulse](sdk => {
if (typeof callback === 'function') callback(sdk);
});
};
export function getMessageType() {
const messageName = cmpTrack.pulseCommonData?.messageName;
if (!messageName) return;
return messageTypes.find(messageType => messageName.includes(messageType));
}
// workaround for misspelling data schema (event-formats) in Pulse
// see: https://github.schibsted.io/spt-dataanalytics/pulse-sdk-js/blob/174d1db0477cd66d6e5f52cf7435a2fe5d6abffa/workspaces/sdk/src/builders/consents.ts#L30
function handleConsentForPulse(consent) {
return `CMP_${(consent === 'personalisation' ? 'personalization' : consent).toUpperCase()}`;
};
export function pulseSetConsents(window) {
const updatedObject = JSON.parse(JSON.stringify(pulseConsentsDefaultObject));
return pulseObject(window, (sdk) => {
try {
sdk.setConsents(prepareConsentObject(window, updatedObject, sourceTypes.CMP));
} catch {
console.error(`Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event`);
}
});
}
export function prepareConsentObject(window, obj, sourceType) {
// filter out false values
const consents = Object.keys(getLocalStorage(window)).filter(consent => getLocalStorage(window)[consent]);
const rejections = Object.keys(getLocalStorage(window)).filter(consent => !getLocalStorage(window)[consent]);
consents.map(consent => {
const purposeObject = obj.purposes[handleConsentForPulse(consent)];
purposeObject.optIn = true;
purposeObject.status = consentStatuses.ACCEPTED;
});
rejections.map(rejection => {
const purposeObject = obj.purposes[handleConsentForPulse(rejection)];
purposeObject.status = consentStatuses.REJECTED;
});
obj.source = sourceType;
return obj;
}
export function getLocalStorage(_window) {
const advertising = convertToBoolean((_window._scc_ || _window._tcf_).getPermissionSync('CMP:advertising'));
const analytics = convertToBoolean((_window._scc_ || _window._tcf_).getPermissionSync('CMP:analytics'));
const marketing = convertToBoolean((_window._scc_ || _window._tcf_).getPermissionSync('CMP:marketing'));
const personalisation = convertToBoolean((_window._scc_ || _window._tcf_).getPermissionSync('CMP:personalisation'));
const consents = { advertising, analytics, marketing, personalisation };
const filteredConsents = removeEmpty(consents);
return filteredConsents;
}
export function getVisibilityDuration(messageReceiveDataStart, _window) {
const messageChoiceSelectStop = _window.performance.now();
return messageReceiveDataStart ? Math.round((messageChoiceSelectStop - messageReceiveDataStart)/1000) : null;
}