@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 13.1 kB
JavaScript
!function(){var e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","groupId"];function n(){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 t(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 r(e){return null===e?null:Boolean(parseInt(e))}var o=function(e,n){return Object.keys(n).filter((function(n){return void 0!==e[n]})).forEach((function(t){return"object"==typeof n[t]?Object.keys(n[t]).forEach((function(r){return e[t][r]=n[t][r]})):e[t]=n[t]}))};var i="__CONFIGURATION_PLACEHOLDER__",s=["Mixed","PSI","SCC","TCF","PMC"],a="default",c="cmp",u="cache",p="account",l="accepted",d="rejected",f="unknown",g=Object.freeze({purposes:{CMP_ANALYTICS:{optIn:!1,status:f},CMP_MARKETING:{optIn:!1,status:f},CMP_ADVERTISING:{optIn:!1,status:f},CMP_PERSONALIZATION:{optIn:!1,status:f}},source:a});function m(e,t,r,o){n("Pulse: "+r+"(actor, data):",t,o),y(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,m.pulseCommonData),provider:{component:"CMP-Marketplaces"},actor:t})}))}function y(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 v(){var e,n=null==(e=m.pulseCommonData)?void 0:e.messageName;if(n)return s.find((function(e){return n.includes(e)}))}function _(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function b(e,n,t){var r=Object.keys(h(e)).filter((function(n){return h(e)[n]})),o=Object.keys(h(e)).filter((function(n){return!h(e)[n]}));return r.map((function(e){var t=n.purposes[_(e)];t.optIn=!0,t.status=l})),o.map((function(e){n.purposes[_(e)].status=d})),n.source=t,n}function h(e){var n,t=r((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),o=r((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),i=r((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),s=r((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));return n={advertising:t,analytics:o,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 C(e,n){var t=n.performance.now();return e?Math.round((t-e)/1e3):null}var w=null,P=!1,I=!1;function M(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(h(e)).length){if(!n)return g;var o=JSON.parse(JSON.stringify(n));return o.source=a,o}var i=JSON.parse(JSON.stringify(n||g));return b(e,i,t?p:u)}function O(e,t){t.events=t.events||{};var r,o=t.events.onMessageReceiveData,i=t.events.onMessageChoiceSelect,s=t.events.onMessageReady,a=t.events.onPrivacyManagerAction,u=t.events.onConsentReady,p=!0,l={id:t.userId,realm:t.realm};t.events.onMessageReceiveData=function(e,r){n("Sourcepoint: onMessageReceiveData(data)",r),w=!0,m.pulseCommonData={"@id":"sdrn:privacy:cmp:"+(r.messageId||""),"@type":"CMP",messageName:r.msgDescription||"",propertyId:t.propertyId,messageId:r.messageId||void 0,bucket:r.bucket||void 0,jsSdkVersion:t.jsSdkVersion,partitionId:r.prtnUUID||"",language:t.consentLanguage},"function"==typeof o&&o(r)},t.events.onMessageReady=function(){var t,o;null==(t=window._tcf_)||null==(o=t.TCFCallbacks)||o.map((function(e){e()})),n("Sourcepoint: onMessageReady()");var i=v();if(n("Pulse: Detected message type: "+i),!w)return m(e,l,"View",{name:"CMP: Privacy Manager displayed",messageType:v()});r=e.performance.now(),m(e,l,"View",{name:"CMP: Message displayed",messageType:i}),"function"==typeof s&&s()},t.events.onMessageChoiceSelect=function(t,o,s){if(n("Sourcepoint: onMessageReady(choice_id="+o+", choice_type_id="+s+")"),5===s){var a=C(r,e);m(e,l,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:w,messageType:v()},a&&{duration:a})),w=null}if(9===s){p=!1,w=null;var c=C(r,e);m(e,l,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:v()},c&&{duration:c}))}if(11===s&&(p=!0),12===s&&(w=!1),15===s){var u=C(r,e);m(e,l,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:v()},u&&{duration:u}))}"function"==typeof i&&i(o,s)},t.events.onPrivacyManagerAction=function(e,t){n("Sourcepoint: onPrivacyManagerAction()"),P=!0,w=!1,"none"===t.purposeConsent?(I=!0,p=!1):(I=!1,p=!0),"function"==typeof a&&a()},t.events.onConsentReady=function(t,o,i,s){var a,d,f,_;null==(a=window._tcf_)||null==(d=a.consentedCallback)||d.map((function(e){e(s.consentedToAll)})),null==(f=window._tcf_)||null==(_=f.TCFCallbacks)||_.map((function(e){e()})),n("Sourcepoint: onConsentReady(message_type="+t+", consentUUID="+o+", euconsent)",i),n("Sourcepoint: getMessageType() "+v());var M=C(r,e);if(void 0!==v()||P){var O="PSI"===v()?null:h(e);if(function(e){var n=JSON.parse(JSON.stringify(g));y(e,(function(t){try{t.setConsents(b(e,n,c))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}(e),I&&m(e,l,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:v()},M&&{duration:M})),!p)return!1;m(e,l,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:v(),consentedToAll:null==s?void 0:s.consentedToAll,isFirstLayer:w||!!w},O&&{consents:O},M&&{duration:M})),w=null,P=!1,I=!1,"function"==typeof u&&u(t,o,i,s)}}}function j(e,t,r){var o;if(!t||!t.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(t){return n(r+": getPermissionFromCache(category="+t+")"),e.localStorage.getItem(t)}p[i]=[],p[s]=[],p[a]=[],p[c]=[],p[u]=[];var d,f={subscribers:p,consentedCallback:[],TCFCallbacks:[],showPrivacyManager:function(){n("${type}: showPrivacyManager()"),m.pulseCommonData.messageName="",P=!0,w=!1,e._sp_.gdpr.loadPrivacyManagerModal()},getPermissionSync:function(e){return n(r+": getPermissionSync(category="+e+")"),l(e)},getPermission:function(e,t){n(r+": getPermission(category="+e+")");var o,i=f.subscribe(e,(o=!1,function(e){o?"function"==typeof i&&i():(o=!0,t(e),t=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,t){if(d[e]!==t){n(r+": notify(category="+e+", value="+t+")"),d[e]=t;for(var o=f.subscribers[e],i=0;i<o.length;i++)o[i](t)}else n(r+": notify(category="+e+", value="+t+") - IGNORED")}),subscribe:function(e,t){if(n(r+": subscribe(category="+e+")"),f.subscribers[e]){f.subscribers[e].push(t);var o=l(e);return null!==o&&t(o),function(){f.subscribers[e]=f.subscribers[e].filter((function(e){return e!==t}))}}console.error("Unable to subscribe - undefined consent category!")},getCachedOrDefaultConsentsForPulse:function(n){return M(e,n,t.authId)},isConsentedToAll:function(e){f.consentedCallback.push(e)},getConsentedToAllSync:function(){var e=l("_sp_user_consent_"+t.propertyId);try{var n,r;if(0===Object.keys(localStorage).filter((function(e){return e.startsWith("CMP:")})).length)return null;var o=null==(n=JSON.parse(e))||null==(r=n.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_"+t.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 S(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 k(e){var n=Array.from({length:20},(function(){return String.fromCharCode(Math.floor(25*Math.random()+97))})).join(""),t=btoa(JSON.stringify(n));S(e)&&S(e).login({state:t})}var T=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},N=function(e){return function(e){try{return Boolean(new URL(e))}catch(e){return!1}}(e)?window.psi.isLoggedInUser?t(e,null,window.psi.clientId,window.psi.referrer,window.psi.state):y(window,(function(n){n.getEnvironmentId().then((function(n){t(e,n,window.psi.clientId,window.psi.referrer,window.psi.state)}))})):console.error("The URL passed to the configuration is invalid. Please, correct it.")},E=function(){return k(window)},A=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},D=["Hermes","_app_","tv.nu","FinnApp","Omni"];!function(e,n){if(!n.disableSentry){var t=e.createElement("script");o(t,{async:!0,type:"module",src:"https://static.pro.cmp.m10s.io/dist/index.js",fetchPriority:"low"}),e.head.appendChild(t)}}(document,i),function(e,n){e._scc_=j(e,n)}(window,i),function(t,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");O(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=S(e,r))?void 0:i._session.userId);c&&A(r,"loggedin","true");var u=r.clientId||(null==(s=S(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 A(r,n,t)}))}(function(e){return D.some((function(n){return e.userAgent.includes(n)}))})(t)&&A(r,"mobile-webview",!r.showInWebview||r.showInWebview);var l=null==n||null==(a=n.referrer)?void 0:a.split("/")[2];l&&A(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=T,e.psi.openPrivacySettings=N,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=E,e.psi.groupId=b})(t,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,t){e._sp_=e._sp_||{},e._sp_.config=t,n("Sourcepoint initiated with config: ",t)}(t,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,i,"scc"),function(e,n){var t=e.baseEndpoint+"/unified/wrapperMessagingWithoutDetection.js",r=n.getElementsByTagName("script"),i=r[r.length-1],s=n.createElement("link");o(s,{rel:"preload",as:"script",crossorigin:"crossorigin",href:t}),n.head.appendChild(s);var a=n.createElement("script");o(a,{async:!0,src:t}),i.insertAdjacentElement("afterend",a)}(i,document)}();