@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 12.7 kB
JavaScript
!function(){var e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","groupId"];var n="__CONFIGURATION_PLACEHOLDER__";function t(){var e;"undefined"!=typeof document&&document.location.href.indexOf("sp_debug=1")>0&&(e=console).debug.apply(e,["[CMP]"].concat(Array.prototype.slice.call(arguments)))}function r(e,n,t,r,o){var i=new URL(e);i.searchParams.append("utm_source","cmp"),i.searchParams.append("utm_medium","psi"),i.searchParams.append("referrer",r||window.location.href),t&&i.searchParams.append("client_id",t),n&&i.searchParams.append("environment_id",n),o&&i.searchParams.append("state",o),window.location.href=i.href}function o(e){return null===e?null:Boolean(parseInt(e))}var i=["Mixed","PSI","SCC","TCF","PMC"],s="default",a="cmp",c="cache",u="account",p="accepted",l="rejected",d="unknown",f=Object.freeze({purposes:{CMP_ANALYTICS:{optIn:!1,status:d},CMP_MARKETING:{optIn:!1,status:d},CMP_ADVERTISING:{optIn:!1,status:d},CMP_PERSONALIZATION:{optIn:!1,status:d}},source:s});function g(e,n,r,o){t("Pulse: "+r+"(actor, data):",n,o),m(e,(function(e){e&&"function"==typeof e.track&&e.track(r,{schema:"http://schema.pulse.m10s.io/events/tracker-event.json/362.json",object:Object.assign({},o,g.pulseCommonData),provider:{component:"CMP-Marketplaces"},actor:n})}))}function m(e,n){if(e.psi.pulseTracker)return n(e.psi.pulseTracker);var t=e.psi.pulseObjectName||"pulse";return"function"!=typeof e[t]?console.error("No Pulse integration detected. Aborting."):e[t]((function(e){"function"==typeof n&&n(e)}))}function y(){var e,n=null==(e=g.pulseCommonData)?void 0:e.messageName;if(n)return i.find((function(e){return n.includes(e)}))}function v(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function _(e,n,t){var r=Object.keys(b(e)).filter((function(n){return b(e)[n]})),o=Object.keys(b(e)).filter((function(n){return!b(e)[n]}));return r.map((function(e){var t=n.purposes[v(e)];t.optIn=!0,t.status=p})),o.map((function(e){n.purposes[v(e)].status=l})),n.source=t,n}function b(e){var n,t=o((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),r=o((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),i=o((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),s=o((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));return n={advertising:t,analytics:r,marketing:i,personalisation:s},Object.entries(n).filter((function(e){return e[0],null!=e[1]})).reduce((function(e,n){var t,r=n[0],o=n[1];return Object.assign({},e,((t={})[r]=o,t))}),{})}function h(e,n){var t=n.performance.now();return e?Math.round((t-e)/1e3):null}var C=null,w=!1,P=!1;function I(e,n,t){if(n&&("object"!=typeof(r=n)||r.constructor!==Object))return console.error("Provided object is incorrect");var r;if(0===Object.keys(b(e)).length){if(!n)return f;var o=JSON.parse(JSON.stringify(n));return o.source=s,o}var i=JSON.parse(JSON.stringify(n||f));return _(e,i,t?u:c)}function M(e,n){n.events=n.events||{};var r,o=n.events.onMessageReceiveData,i=n.events.onMessageChoiceSelect,s=n.events.onMessageReady,c=n.events.onPrivacyManagerAction,u=n.events.onConsentReady,p=!0,l={id:n.userId,realm:n.realm};n.events.onMessageReceiveData=function(e,r){t("Sourcepoint: onMessageReceiveData(data)",r),C=!0,g.pulseCommonData={"@id":"sdrn:privacy:cmp:"+(r.messageId||""),"@type":"CMP",messageName:r.msgDescription||"",propertyId:n.propertyId,messageId:r.messageId||void 0,bucket:r.bucket||void 0,jsSdkVersion:n.jsSdkVersion,partitionId:r.prtnUUID||"",language:n.consentLanguage},"function"==typeof o&&o(r)},n.events.onMessageReady=function(){var n,o;null==(n=window._tcf_)||null==(o=n.TCFCallbacks)||o.map((function(e){e()})),t("Sourcepoint: onMessageReady()");var i=y();if(t("Pulse: Detected message type: "+i),!C)return g(e,l,"View",{name:"CMP: Privacy Manager displayed",messageType:y()});r=e.performance.now(),g(e,l,"View",{name:"CMP: Message displayed",messageType:i}),"function"==typeof s&&s()},n.events.onMessageChoiceSelect=function(n,o,s){if(t("Sourcepoint: onMessageReady(choice_id="+o+", choice_type_id="+s+")"),5===s){var a=h(r,e);g(e,l,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:C,messageType:y()},a&&{duration:a})),C=null}if(9===s){p=!1,C=null;var c=h(r,e);g(e,l,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:y()},c&&{duration:c}))}if(11===s&&(p=!0),12===s&&(C=!1),15===s){var u=h(r,e);g(e,l,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:y()},u&&{duration:u}))}"function"==typeof i&&i(o,s)},n.events.onPrivacyManagerAction=function(e,n){t("Sourcepoint: onPrivacyManagerAction()"),w=!0,C=!1,"none"===n.purposeConsent?(P=!0,p=!1):(P=!1,p=!0),"function"==typeof c&&c()},n.events.onConsentReady=function(n,o,i,s){var c,d,v,I;null==(c=window._tcf_)||null==(d=c.consentedCallback)||d.map((function(e){e(s.consentedToAll)})),null==(v=window._tcf_)||null==(I=v.TCFCallbacks)||I.map((function(e){e()})),t("Sourcepoint: onConsentReady(message_type="+n+", consentUUID="+o+", euconsent)",i),t("Sourcepoint: getMessageType() "+y());var M=h(r,e);if(void 0!==y()||w){var O="PSI"===y()?null:b(e);if(function(e){var n=JSON.parse(JSON.stringify(f));m(e,(function(t){try{t.setConsents(_(e,n,a))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}(e),P&&g(e,l,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:y()},M&&{duration:M})),!p)return!1;g(e,l,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:y(),consentedToAll:null==s?void 0:s.consentedToAll,isFirstLayer:C||!!C},O&&{consents:O},M&&{duration:M})),C=null,w=!1,P=!1,"function"==typeof u&&u(n,o,i,s)}}}function O(e,n,r){var o;if(!n||!n.groupPmId)throw new Error("groupPmId is required");var i="CMP:advertising",s="CMP:analytics",a="CMP:marketing",c="CMP:personalisation",u="CMP:performance_marketing",p={};function l(n){return t(r+": getPermissionFromCache(category="+n+")"),e.localStorage.getItem(n)}p[i]=[],p[s]=[],p[a]=[],p[c]=[],p[u]=[];var d,f={subscribers:p,consentedCallback:[],TCFCallbacks:[],showPrivacyManager:function(){t("${type}: showPrivacyManager()"),g.pulseCommonData.messageName="",w=!0,C=!1,e._sp_.gdpr.loadPrivacyManagerModal()},getPermissionSync:function(e){return t(r+": getPermissionSync(category="+e+")"),l(e)},getPermission:function(e,n){t(r+": getPermission(category="+e+")");var o,i=f.subscribe(e,(o=!1,function(e){o?"function"==typeof i&&i():(o=!0,n(e),n=function(){})}))},notify:(o={},o[i]=l(i),o[s]=l(s),o[a]=l(a),o[c]=l(c),o[u]=l(u),d=o,function(e,n){if(d[e]!==n){t(r+": notify(category="+e+", value="+n+")"),d[e]=n;for(var o=f.subscribers[e],i=0;i<o.length;i++)o[i](n)}else t(r+": notify(category="+e+", value="+n+") - IGNORED")}),subscribe:function(e,n){if(t(r+": subscribe(category="+e+")"),f.subscribers[e]){f.subscribers[e].push(n);var o=l(e);return null!==o&&n(o),function(){f.subscribers[e]=f.subscribers[e].filter((function(e){return e!==n}))}}console.error("Unable to subscribe - undefined consent category!")},getCachedOrDefaultConsentsForPulse:function(t){return I(e,t,n.authId)},isConsentedToAll:function(e){f.consentedCallback.push(e)},getConsentedToAllSync:function(){var e=l("_sp_user_consent_"+n.propertyId);try{var t,r;if(0===Object.keys(localStorage).filter((function(e){return e.startsWith("CMP:")})).length)return null;var o=null==(t=JSON.parse(e))||null==(r=t.gdpr)?void 0:r.consentStatus;if((null==o?void 0:o.vendorListAdditions)||(null==o?void 0:o.legalBasisChanges))return null;var i=null==o?void 0:o.consentedAll;return void 0===i?null:i}catch(e){return null}},onTcfReady:function(e){f.TCFCallbacks.push(e)},clearCMPData:function(){["_sp_user_consent_"+n.propertyId,"_sp_non_keyed_local_state","_sp_local_state","CMP:advertising","CMP:analytics","CMP:marketing","CMP:personalisation"].forEach((function(e){localStorage.removeItem(e),sessionStorage.removeItem(e)}));var e=function(){for(var e=window.location.hostname,n=e.split("."),t=[],r=0;r<n.length-1;r++)t.push("."+n.slice(r).join("."));return t.push(e),t}();["consentUUID","euconsent-v2","consentDate","_cmp_advertising","_cmp_analytics","_cmp_marketing","_cmp_personalisation"].forEach((function(n){e.forEach((function(e){document.cookie=n+"=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain="+e+";"})),document.cookie=n+"=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;"}))}};return e._SourcePoint_Consent_Callback=f.notify,f}function j(e,n){var t,r;if(null!=(t=e.psi)&&t.identityObject)return e.psi.identityObject;if(null!=n&&n.identityObject)return n.identityObject;var o=(null==(r=e.psi)?void 0:r.identityObjectName)||(null==n?void 0:n.identityObjectName)||"Identity";return e[o]?e[o]:console.warn("No Schibsted Account integration detected.")}function S(e){var n=Array.from({length:20},(function(){return String.fromCharCode(Math.floor(25*Math.random()+97))})).join(""),t=btoa(JSON.stringify(n));j(e)&&j(e).login({state:t})}var k=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},T=function(e){return function(e){try{return Boolean(new URL(e))}catch(e){return!1}}(e)?window.psi.isLoggedInUser?r(e,null,window.psi.clientId,window.psi.referrer,window.psi.state):m(window,(function(n){n.getEnvironmentId().then((function(n){r(e,n,window.psi.clientId,window.psi.referrer,window.psi.state)}))})):console.error("The URL passed to the configuration is invalid. Please, correct it.")},N=function(){return S(window)},E=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},A=["Hermes","_app_","tv.nu","FinnApp","Omni"];!function(e,n){if(!n.disableSentry){var t,r,o=e.createElement("script");t=o,r={async:!0,type:"module",src:"https://static.pro.cmp.m10s.io/dist/index.js",fetchPriority:"low"},Object.keys(r).filter((function(e){return void 0!==t[e]})).forEach((function(e){return"object"==typeof r[e]?Object.keys(r[e]).forEach((function(n){return t[e][n]=r[e][n]})):t[e]=r[e]})),e.head.appendChild(o)}}(document,n),function(e,n){e._scc_=O(e,n)}(window,n),function(n,r,o,i,s){(function(e,n,t,r,o){var i,s,a;if(!(r&&r.baseEndpoint&&r.propertyId&&r.consentLanguage))throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");M(e,r),function(e){e.events=e.events||{},e.events.onError=function(n,t,r){try{n&&window._cmp_.sentry.setTag("message_type",n),t&&window._cmp_.sentry.setTag("errorCode",t),window._cmp_.sentry.setTag("propertyId",e.propertyId),window._cmp_.sentry.captureException(r)}catch(e){console.error("Can not send the error to Sentry, "+e)}},e.events.onMessageChoiceError=function(e,n){try{e&&window._cmp_.sentry.setTag("message_type",e),window._cmp_.sentry.captureException(n)}catch(n){console.error("Can not send the error to Sentry, "+n)}}}(r),r.accountId=r.accountId||1960,r.custom={},"psi"!==o&&(r.gdpr={includeTcfApi:"tcf"===o});r.propertyHref&&(r.joinHref=!0);var c=r.userId||(null==(i=j(e,r))?void 0:i._session.userId);c&&E(r,"loggedin","true");var u=r.clientId||(null==(s=j(window,r))?void 0:s.clientId);function p(){var e=/(_sch_cmp_[_a-z0-9]+)=(.+)/i;n.cookie.split(";").filter((function(n){return e.test(n)})).map((function(e){return e.split("=").map((function(e){return e.trim()}))})).forEach((function(e){var n=e[0],t=e[1];return E(r,n,t)}))}(function(e){return A.some((function(n){return e.userAgent.includes(n)}))})(t)&&E(r,"mobile-webview",!r.showInWebview||r.showInWebview);var l=null==n||null==(a=n.referrer)?void 0:a.split("/")[2];l&&E(r,"referrer-hostname",l);p(),r.propertyHref&&n.location.search&&-1===r.propertyHref.indexOf("?")&&(r.propertyHref+=n.location.search);var d=r.pulseTracker,f=r.referrer,g=r.state,m=r.pulseObjectName,y=r.showInWebview,v=r.identityObject,_=r.identityObjectName,b=r.groupId;e.psi=e.psi||{},e.psi.isLoggedInUser=!!c,e.psi.setUserId=k,e.psi.openPrivacySettings=T,e.psi.pulseTracker=d,e.psi.clientId=u,e.psi.referrer=f,e.psi.state=g,e.psi.pulseObjectName=m,e.psi.showInWebview=y,e.psi.identityObject=v,e.psi.identityObjectName=_,e.psi.triggerLogin=N,e.psi.groupId=b})(n,r,o,i,s),i.jsSdkVersion,i.id,i.realm,i.clientId,i.pulseTracker,i.referrer,i.state,i.pulseObjectName,i.showInWebview,i.identityObject,i.identityObjectName,i.disableSentry,i.disableNativeConsentCheck,i.groupId,function(e,n){e._sp_=e._sp_||{},e._sp_.config=n,t("Sourcepoint initiated with config: ",n)}(n,function(e,n){if(null==e)return{};var t={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(-1!==n.indexOf(r))continue;t[r]=e[r]}return t}(i,e))}(window,document,navigator,n,"scc")}();