@schibsted/sourcepoint
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
144 lines (118 loc) • 4.12 kB
JavaScript
import { handleLoginFlow } from './schibsted-account/index.js';
import { pulseObject } from './pulse/helpers.js';
export function debug() {
if (typeof document !== 'undefined' && document.location.href.indexOf('sp_debug=1') > 0) {
console.debug('[CMP]', ...arguments);
}
}
export function handleRedirect (url, environmentId, clientId, referrer, state) {
const urlObject = new URL(url);
// UTM tags
urlObject.searchParams.append('utm_source', 'cmp');
urlObject.searchParams.append('utm_medium', 'psi');
urlObject.searchParams.append('referrer', referrer || window.location.href);
if (clientId) {
urlObject.searchParams.append('client_id', clientId);
}
if (environmentId) {
urlObject.searchParams.append('environment_id', environmentId);
}
if (state) {
urlObject.searchParams.append('state', state);
}
window.location.href = urlObject.href;
}
export const isUrl = (string) => {
try {
return Boolean(new URL(string));
}
catch {
return false;
}
}
export function convertToBoolean(value) {
if (value === null) return null;
return Boolean(parseInt(value));
}
export function isPlainObject(item) {
return (typeof item === 'object' && item.constructor === Object);
}
export function removeEmpty(obj) {
return Object.entries(obj)
.filter(([_, v]) => v != null)
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
}
export const setMultipleAttributes = (element, attributes) => {
return Object.keys(attributes)
.filter(key => element[key] !== undefined)
.forEach(key =>
typeof attributes[key] === 'object'
? Object.keys(attributes[key])
.forEach(innerKey => element[key][innerKey] = attributes[key][innerKey])
: element[key] = attributes[key]
);
}
export function setCookie(name, value, exdays) {
const date = new Date();
date.setTime(date.getTime() + (exdays*24*60*60*1000));
let expires = "expires="+ date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
export function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
export function eraseCookie(name) {
document.cookie = name+'=; Max-Age=-99999999;';
}
export function setTargetingParams(config, key, val) {
config.targetingParams = config.targetingParams || {};
config.targetingParams[key] = val;
}
export async function hashUserAuthId(userId) {
const encoder = new TextEncoder();
const data = encoder.encode(userId);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
export const setUserId = function (id) {
if (typeof id === 'number' || id === undefined) {
return config.userId = id;
}
throw new Error(`Id value ${id} is invalid`);
};
export const openPrivacySettings = function (url) {
if (!isUrl(url)) {
return console.error('The URL passed to the configuration is invalid. Please, correct it.');
}
if (!window.psi.isLoggedInUser) {
return pulseObject(window, (sdk) => {
sdk.getEnvironmentId().then((environmentId) => {
handleRedirect(
url,
environmentId,
window.psi.clientId,
window.psi.referrer,
window.psi.state
);
});
});
}
return handleRedirect(url, null, window.psi.clientId, window.psi.referrer, window.psi.state);
};
export const triggerLogin = function () {
return handleLoginFlow(window);
};