@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
46 lines (43 loc) • 1.97 kB
JavaScript
import { cmpTrack } from '../pulse/helpers';
import { setCookie, setAttributes } from '../utils.js';
export async function getConsentHistory (_window, config) {
try {
const url = `https://cdn.privacy-mgmt.com/consent/tcfv2/consent/v3/history/${config.propertyId}?authId=${config.authId}`;
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const history = await res.json();
if (history.length === 0) {
cmpTrack(_window, config.userId, 'View', {
'@id': `sdrn:privacy:cmp:${config.propertyId}`,
'@type': 'CMP',
name: 'CMP: First save to account',
language: config.consentLanguage
});
}
if (history.length > 0) {
cmpTrack(_window, config.userId, 'View', {
'@id': `sdrn:privacy:cmp:${config.propertyId}`,
'@type': 'CMP',
name: 'CMP: Account already has saved choices',
language: config.consentLanguage
});
showRetrieveMessage(config);
}
setCookie('CMP:stickyConsent', true, 400);
} catch (err) {
console.warn('[SP] Fetching consent history error:', err);
}
};
function showRetrieveMessage(config) {
const script = document.createElement('script');
script.src = 'https://static.pro.cmp.m10s.io/dist/retrieve.js';
document.body.appendChild(script);
const retrieveMessage = document.createElement('retrieve-message');
setAttributes(retrieveMessage, {
isWeb: true,
lang: config.consentLanguage,
...(config.consentStatusMessage?.verticalPosition && { verticalPosition: config.consentStatusMessage?.verticalPosition } ),
...(config.consentStatusMessage?.horizontalPosition && { horizontalPosition: config.consentStatusMessage?.horizontalPosition } ),
});
document.body.appendChild(retrieveMessage);
};