@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
203 lines (177 loc) • 7.88 kB
JavaScript
import { debug, isPlainObject } from '../utils';
import { cmpTrack, getMessageType, getLocalStorage, pulseSetConsents, prepareConsentObject, getVisibilityDuration } from './helpers';
import { sourceTypes, pulseConsentsDefaultObject } from './variables';
export let firstLayer = null;
let pmAction = false;
let rejectAllCookies = false;
export function getConsentObjectForBrands(_window, obj, authId) {
if (obj && !isPlainObject(obj)) {
return console.error('Provided object is incorrect');
}
// user has not consented yet
if (Object.keys(getLocalStorage(_window)).length === 0) {
if (!obj) return pulseConsentsDefaultObject;
const updatedObject = JSON.parse(JSON.stringify(obj));
updatedObject.source = sourceTypes.DEFAULT;
return updatedObject;
}
const updatedObject = JSON.parse(JSON.stringify(obj || pulseConsentsDefaultObject));
if (authId) {
return prepareConsentObject(_window, updatedObject, sourceTypes.ACCOUNT);
}
return prepareConsentObject(_window, updatedObject, sourceTypes.CACHE);
}
export function onInitPrivacyManagerView() {
cmpTrack.pulseCommonData.messageName = '';
pmAction = true;
firstLayer = false;
}
export function appendPulseTrackingToEvents(_window, config) {
config.events = config.events || {};
const oldOnMessageReceiveData = config.events.onMessageReceiveData;
const oldOnMessageChoiceSelect = config.events.onMessageChoiceSelect;
const oldOnMessageReady = config.events.onMessageReady;
const onPrivacyManagerAction = config.events.onPrivacyManagerAction;
const onConsentReady = config.events.onConsentReady;
let messageReceiveDataStart, sendConsent = true;
const actor = {id: config.userId, realm: config.realm};
config.events.onMessageReceiveData = function (_, data) {
debug('Sourcepoint: onMessageReceiveData(data)', data);
firstLayer = true;
cmpTrack.pulseCommonData = {
'@id': 'sdrn:privacy:cmp:' + (data.messageId || ''),
'@type': 'CMP',
messageName: data.msgDescription || '',
propertyId: config.propertyId,
messageId: data.messageId || undefined,
bucket: data.bucket || undefined,
jsSdkVersion: config.jsSdkVersion,
partitionId: data.prtnUUID || '',
language: config.consentLanguage
};
typeof oldOnMessageReceiveData === 'function' && oldOnMessageReceiveData(data);
};
config.events.onMessageReady = function () {
window._tcf_?.TCFCallbacks?.map(callback => {
callback();
});
debug('Sourcepoint: onMessageReady()');
const messageType = getMessageType();
debug(`Pulse: Detected message type: ${messageType}`);
if (!firstLayer) {
return cmpTrack(_window, actor, 'View', {
name: 'CMP: Privacy Manager displayed',
messageType: getMessageType()
});
}
messageReceiveDataStart = _window.performance.now();
cmpTrack(_window, actor, 'View', {
name: 'CMP: Message displayed',
messageType: messageType
});
typeof oldOnMessageReady === 'function' && oldOnMessageReady();
};
config.events.onMessageChoiceSelect = function (_, choice_id, choice_type_id) {
debug(`Sourcepoint: onMessageReady(choice_id=${choice_id}, choice_type_id=${choice_type_id})`);
// PSI / SCC
if (choice_type_id === 5) {
// The user has chosen a option to redirect to another page (privacy policy).
const duration = getVisibilityDuration(messageReceiveDataStart, _window);
cmpTrack(_window, actor, 'engagementEvent', {
name: 'CMP: Link to Privacy / Cookie Policy clicked',
elementType: 'anchor',
isFirstLayer: firstLayer,
messageType: getMessageType(),
...(duration && { duration })
});
firstLayer = null;
}
// PSI
if (choice_type_id === 9) {
// The user has selected an option tied to custom javascript on the site page. In this case open privacy settings.
sendConsent = false;
firstLayer = null;
const duration = getVisibilityDuration(messageReceiveDataStart, _window);
cmpTrack(_window, actor, 'engagementEvent', {
name: 'CMP: Open Privacy Settings',
elementType: 'button',
messageType: getMessageType(),
...(duration && { duration })
});
}
// PSI / SCC
if (choice_type_id === 11) {
// The user has chosen the "Accept All" option in a consent message.
sendConsent = true;
}
// SCC
if (choice_type_id === 12) {
// The user has chosen the "Manage cookies" option in a consent message.
firstLayer = false;
}
if (choice_type_id === 15) {
// The user has chosen the "Dismiss button" option in a consent message.
const duration = getVisibilityDuration(messageReceiveDataStart, _window);
cmpTrack(_window, actor, 'engagementEvent', {
name: 'CMP: Dismiss message',
elementType: 'button',
messageType: getMessageType(),
...(duration && { duration })
});
}
typeof oldOnMessageChoiceSelect === 'function' &&
oldOnMessageChoiceSelect(choice_id, choice_type_id);
};
config.events.onPrivacyManagerAction = function (_, pmData) {
debug('Sourcepoint: onPrivacyManagerAction()');
pmAction = true;
firstLayer = false;
// purposeConsent: 'all' | 'none' | 'some'
if (pmData.purposeConsent === 'none') {
rejectAllCookies = true;
sendConsent = false;
} else {
rejectAllCookies = false;
sendConsent = true;
}
typeof onPrivacyManagerAction === 'function' && onPrivacyManagerAction();
};
// PSI / SCC
config.events.onConsentReady = function (message_type, consentUUID, euconsent, info) {
window._tcf_?.consentedCallback?.map(callback => {
callback(info.consentedToAll);
});
window._tcf_?.TCFCallbacks?.map(callback => {
callback();
});
debug(`Sourcepoint: onConsentReady(message_type=${message_type}, consentUUID=${consentUUID}, euconsent)`, euconsent);
debug(`Sourcepoint: getMessageType() ${getMessageType()}`);
const duration = getVisibilityDuration(messageReceiveDataStart, _window);
if (getMessageType() === undefined && !pmAction) return;
const consents = getMessageType() === 'PSI' ? null : getLocalStorage(_window);
pulseSetConsents(_window);
if (rejectAllCookies) {
cmpTrack(_window, actor, 'engagementEvent', {
name: 'CMP: Reject all',
elementType: 'button',
messageType: getMessageType(),
...(duration && { duration })
});
};
if (!sendConsent) return false;
cmpTrack(_window, actor, 'engagementEvent', {
name: 'CMP: Accept',
elementType: 'button',
messageType: getMessageType(),
consentedToAll: info?.consentedToAll,
isFirstLayer: firstLayer || !!firstLayer,
...(consents && { consents }),
...(duration && { duration })
});
firstLayer = null;
pmAction = false;
rejectAllCookies = false;
typeof onConsentReady === 'function'
&& onConsentReady(message_type, consentUUID, euconsent, info);
};
}