@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 13.2 kB
JavaScript
!function(){var e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","enableUserCentric","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,i){var o=new URL(e);o.searchParams.append("utm_source","cmp"),o.searchParams.append("utm_medium","psi"),o.searchParams.append("referrer",r||window.location.href),t&&o.searchParams.append("client_id",t),n&&o.searchParams.append("environment_id",n),i&&o.searchParams.append("state",i),window.location.href=o.href}function i(e){return null===e?null:Boolean(parseInt(e))}function o(e){for(var n=e+"=",t=decodeURIComponent(document.cookie).split(";"),r=0;r<t.length;r++){for(var i=t[r];" "==i.charAt(0);)i=i.substring(1);if(0==i.indexOf(n))return i.substring(n.length,i.length)}return""}var s=["Mixed","PSI","SCC","TCF","PMC"],a="default",c="cmp",u="cache",l="accepted",p="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:a});function g(e,n,r,i){t("Pulse: "+r+"(actor, data):",n,i),m(e,(function(e){e&&"function"==typeof e.track&&e.track(r,{schema:"http://schema.schibsted.com/events/tracker-event.json/348.json",object:Object.assign({},i,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 v(){var e,n=null==(e=g.pulseCommonData)?void 0:e.messageName;if(n)return s.find((function(e){return n.includes(e)}))}function y(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function b(e,n,t){var r=Object.keys(C(e)).filter((function(n){return C(e)[n]})),i=Object.keys(C(e)).filter((function(n){return!C(e)[n]}));return r.map((function(e){var t=n.purposes[y(e)];t.optIn=!0,t.status=l})),i.map((function(e){n.purposes[y(e)].status=p})),n.source=t,n}function C(e){var n,t=i((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),r=i((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),o=i((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),s=i((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));return n={advertising:t,analytics:r,marketing:o,personalisation:s},Object.entries(n).filter((function(e){return e[0],null!=e[1]})).reduce((function(e,n){var t,r=n[0],i=n[1];return Object.assign({},e,((t={})[r]=i,t))}),{})}function h(e,n){var t=n.performance.now();return e?Math.round((t-e)/1e3):null}var w="CMP:retrieveMessage";function _(e){e.enableUserCentric&&o("CMP:isLoggedIn")&&!o(w)&&(function(e,n,t){var r=new Date;r.setTime(r.getTime()+24*t*60*60*1e3);var i="expires="+r.toUTCString();document.cookie=e+"="+n+";"+i+";path=/"}(w,!0,400),function(e){var n,t,r,i,o=document.createElement("script");o.src="https://static.dev.cmp.m10s.io/dist/retrieve.js",document.body.appendChild(o);var s=document.createElement("retrieve-message");a=s,c=Object.assign({isWeb:!0,lang:e.consentLanguage},(null==(n=e.consentStatusMessage)?void 0:n.verticalPosition)&&{verticalPosition:null==(t=e.consentStatusMessage)?void 0:t.verticalPosition},(null==(r=e.consentStatusMessage)?void 0:r.horizontalPosition)&&{horizontalPosition:null==(i=e.consentStatusMessage)?void 0:i.horizontalPosition}),Object.entries(c).forEach((function(e){return a.setAttribute.apply(a,e)})),document.body.appendChild(s);var a,c}(e))}var P=null,I=!1,O=!1;function M(e,n){if(n&&("object"!=typeof(t=n)||t.constructor!==Object))return console.error("Provided object is incorrect");var t;if(0===Object.keys(C(e)).length){if(!n)return f;var r=JSON.parse(JSON.stringify(n));return r.source=a,r}return b(e,JSON.parse(JSON.stringify(n||f)),u)}function j(e,n){n.events=n.events||{};var r,i=n.events.onMessageReceiveData,o=n.events.onMessageChoiceSelect,s=n.events.onMessageReady,a=n.events.onPrivacyManagerAction,u=n.events.onConsentReady,l=!0,p={id:n.userId,realm:n.realm};n.events.onMessageReceiveData=function(e,r){t("Sourcepoint: onMessageReceiveData(data)",r),P=!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 i&&i(r)},n.events.onMessageReady=function(){var n,i;null==(n=window._tcf_)||null==(i=n.TCFCallbacks)||i.map((function(e){e()})),t("Sourcepoint: onMessageReady()");var o=v();if(t("Pulse: Detected message type: "+o),!P)return g(e,p,"View",{name:"CMP: Privacy Manager displayed",messageType:v()});r=e.performance.now(),g(e,p,"View",{name:"CMP: Message displayed",messageType:o}),"function"==typeof s&&s()},n.events.onMessageChoiceSelect=function(n,i,s){if(t("Sourcepoint: onMessageReady(choice_id="+i+", choice_type_id="+s+")"),5===s){var a=h(r,e);g(e,p,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:P,messageType:v()},a&&{duration:a})),P=null}if(9===s){l=!1,P=null;var c=h(r,e);g(e,p,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:v()},c&&{duration:c}))}if(11===s&&(l=!0),12===s&&(P=!1),15===s){var u=h(r,e);g(e,p,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:v()},u&&{duration:u}))}"function"==typeof o&&o(i,s)},n.events.onPrivacyManagerAction=function(e,n){t("Sourcepoint: onPrivacyManagerAction()"),I=!0,P=!1,"none"===n.purposeConsent?(O=!0,l=!1):(O=!1,l=!0),"function"==typeof a&&a()},n.events.onConsentReady=function(i,o,s,a){var d,y,w,M;_(n),null==(d=window._tcf_)||null==(y=d.consentedCallback)||y.map((function(e){e(a.consentedToAll)})),null==(w=window._tcf_)||null==(M=w.TCFCallbacks)||M.map((function(e){e()})),t("Sourcepoint: onConsentReady(message_type="+i+", consentUUID="+o+", euconsent)",s),t("Sourcepoint: getMessageType() "+v());var j=h(r,e);if(void 0!==v()||I){var S="PSI"===v()?null:C(e);if(function(e){var n=JSON.parse(JSON.stringify(f));m(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),O&&g(e,p,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:v()},j&&{duration:j})),!l)return!1;g(e,p,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:v(),consentedToAll:null==a?void 0:a.consentedToAll,isFirstLayer:P||!!P},S&&{consents:S},j&&{duration:j})),P=null,I=!1,O=!1,"function"==typeof u&&u(i,o,s,a)}}}function S(e,n,r){var i;if(!n||!n.groupPmId)throw new Error("groupPmId is required");var o="CMP:advertising",s="CMP:analytics",a="CMP:marketing",c="CMP:personalisation",u="CMP:performance_marketing",l={};function p(n){return t(r+": getPermissionFromCache(category="+n+")"),e.localStorage.getItem(n)}l[o]=[],l[s]=[],l[a]=[],l[c]=[],l[u]=[];var d,f={subscribers:l,consentedCallback:[],TCFCallbacks:[],showPrivacyManager:function(){t("${type}: showPrivacyManager()"),g.pulseCommonData.messageName="",I=!0,P=!1,e._sp_.gdpr.loadPrivacyManagerModal()},getPermissionSync:function(e){return t(r+": getPermissionSync(category="+e+")"),p(e)},getPermission:function(e,n){t(r+": getPermission(category="+e+")");var i,o=f.subscribe(e,(i=!1,function(e){i?"function"==typeof o&&o():(i=!0,n(e),n=function(){})}))},notify:(i={},i[o]=p(o),i[s]=p(s),i[a]=p(a),i[c]=p(c),i[u]=p(u),d=i,function(e,n){if(d[e]!==n){t(r+": notify(category="+e+", value="+n+")"),d[e]=n;for(var i=f.subscribers[e],o=0;o<i.length;o++)i[o](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 i=p(e);return null!==i&&n(i),function(){f.subscribers[e]=f.subscribers[e].filter((function(e){return e!==n}))}}console.error("Unable to subscribe - undefined consent category!")},getCachedOrDefaultConsentsForPulse:function(n){return M(e,n)},isConsentedToAll:function(e){f.consentedCallback.push(e)},getConsentedToAllSync:function(){var e=p("_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 i=null==(t=JSON.parse(e))||null==(r=t.gdpr)?void 0:r.consentStatus;if((null==i?void 0:i.vendorListAdditions)||(null==i?void 0:i.legalBasisChanges))return null;var o=null==i?void 0:i.consentedAll;return void 0===o?null:o}catch(e){return null}},onTcfReady:function(e){f.TCFCallbacks.push(e)}};return e._SourcePoint_Consent_Callback=f.notify,f}function T(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 i=(null==(r=e.psi)?void 0:r.identityObjectName)||(null==n?void 0:n.identityObjectName)||"Identity";return e[i]?e[i]: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));T(e)&&T(e).login({state:t})}var N=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},A=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.")},E=function(){return k(window)},R=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},U=["Hermes","_app_","tv.nu","FinnApp","Omni"];!function(e,n){if(!n.disableSentry){var t,r,i=e.createElement("script");t=i,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(i)}}(document,n),function(e,n){e._scc_=S(e,n)}(window,n),function(n,r,i,o,s){(function(e,n,t,r,i){var o,s,a;if(!(r&&r.baseEndpoint&&r.propertyId&&r.consentLanguage))throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");j(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"!==i&&(r.gdpr={includeTcfApi:"tcf"===i});r.propertyHref&&(r.joinHref=!0);var c=r.userId||(null==(o=T(e,r))?void 0:o._session.userId);c&&R(r,"loggedin","true");var u=r.clientId||(null==(s=T(window,r))?void 0:s.clientId);function l(){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 R(r,n,t)}))}(function(e){return U.some((function(n){return e.userAgent.includes(n)}))})(t)&&R(r,"mobile-webview",!r.showInWebview||r.showInWebview);var p=null==n||null==(a=n.referrer)?void 0:a.split("/")[2];p&&R(r,"referrer-hostname",p);l(),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,v=r.showInWebview,y=r.identityObject,b=r.identityObjectName,C=r.enableUserCentric,h=r.groupId;e.psi=e.psi||{},e.psi.isLoggedInUser=!!c,e.psi.setUserId=N,e.psi.openPrivacySettings=A,e.psi.pulseTracker=d,e.psi.clientId=u,e.psi.referrer=f,e.psi.state=g,e.psi.pulseObjectName=m,e.psi.showInWebview=v,e.psi.identityObject=y,e.psi.identityObjectName=b,e.psi.triggerLogin=E,e.psi.enableUserCentric=C,e.psi.groupId=h})(n,r,i,o,s),o.jsSdkVersion,o.id,o.realm,o.clientId,o.pulseTracker,o.referrer,o.state,o.pulseObjectName,o.showInWebview,o.identityObject,o.identityObjectName,o.disableSentry,o.disableNativeConsentCheck,o.enableUserCentric,o.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}(o,e))}(window,document,navigator,n,"scc")}();