UNPKG

@m10s/cmp

Version:

Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP

2 lines (1 loc) 10.1 kB
!function(){var e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","enableUserCentric","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,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 r(e){return null===e?null:Boolean(parseInt(e))}var i=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 o=["Mixed","PSI","SCC","TCF","PMC"],s="default",a="cmp",c="accepted",u="rejected",p="unknown",d=Object.freeze({purposes:{CMP_ANALYTICS:{optIn:!1,status:p},CMP_MARKETING:{optIn:!1,status:p},CMP_ADVERTISING:{optIn:!1,status:p},CMP_PERSONALIZATION:{optIn:!1,status:p}},source:s});function l(e,t,r,i){n("Pulse: "+r+"(actor, data):",t,i),f(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,l.pulseCommonData),provider:{component:"CMP-Marketplaces"},actor:t})}))}function f(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 m(){var e,n=null==(e=l.pulseCommonData)?void 0:e.messageName;if(n)return o.find((function(e){return n.includes(e)}))}function g(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function y(e){var n=JSON.parse(JSON.stringify(d));return f(e,(function(t){try{t.setConsents(function(e,n,t){var r=Object.keys(v(e)).filter((function(n){return v(e)[n]})),i=Object.keys(v(e)).filter((function(n){return!v(e)[n]}));return r.map((function(e){var t=n.purposes[g(e)];t.optIn=!0,t.status=c})),i.map((function(e){n.purposes[g(e)].status=u})),n.source=t,n}(e,n,a))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}function v(e){var n,t=r((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),i=r((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),o=r((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),s=r((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));return n={advertising:t,analytics:i,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 w(e,n){var t=n.performance.now();return e?Math.round((t-e)/1e3):null}var b=null,_=!1,h=!1;function C(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 I(e){var n=Array.from({length:20},(function(){return String.fromCharCode(Math.floor(25*Math.random()+97))})).join(""),t=btoa(JSON.stringify(n));C(e)&&C(e).login({state:t})}var P=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},j=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):f(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.")},O=function(){return I(window)},M=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},T=["Hermes","_app_","tv.nu","FinnApp","Omni"];var S="__CONFIGURATION_PLACEHOLDER__";!function(e,n){if(!n.disableSentry){var t=e.createElement("script");i(t,{async:!0,type:"module",src:"https://static.pro.cmp.m10s.io/dist/index.js",fetchPriority:"low"}),e.head.appendChild(t)}}(document,S),function(t,r,i,o,s){(function(e,t,r,i,o){var s,a,c;if(!(i&&i.baseEndpoint&&i.propertyId&&i.consentLanguage))throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");(function(e,t){t.events=t.events||{};var r,i=t.events.onMessageReceiveData,o=t.events.onMessageChoiceSelect,s=t.events.onMessageReady,a=t.events.onPrivacyManagerAction,c=t.events.onConsentReady,u=!0,p={id:t.userId,realm:t.realm};t.events.onMessageReceiveData=function(e,r){n("Sourcepoint: onMessageReceiveData(data)",r),b=!0,l.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 i&&i(r)},t.events.onMessageReady=function(){var t,i;null==(t=window._tcf_)||null==(i=t.TCFCallbacks)||i.map((function(e){e()})),n("Sourcepoint: onMessageReady()");var o=m();if(n("Pulse: Detected message type: "+o),!b)return l(e,p,"View",{name:"CMP: Privacy Manager displayed",messageType:m()});r=e.performance.now(),l(e,p,"View",{name:"CMP: Message displayed",messageType:o}),"function"==typeof s&&s()},t.events.onMessageChoiceSelect=function(t,i,s){if(n("Sourcepoint: onMessageReady(choice_id="+i+", choice_type_id="+s+")"),5===s){var a=w(r,e);l(e,p,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:b,messageType:m()},a&&{duration:a})),b=null}if(9===s){u=!1,b=null;var c=w(r,e);l(e,p,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:m()},c&&{duration:c}))}if(11===s&&(u=!0),12===s&&(b=!1),15===s){var d=w(r,e);l(e,p,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:m()},d&&{duration:d}))}"function"==typeof o&&o(i,s)},t.events.onPrivacyManagerAction=function(e,t){n("Sourcepoint: onPrivacyManagerAction()"),_=!0,b=!1,"none"===t.purposeConsent?(h=!0,u=!1):(h=!1,u=!0),"function"==typeof a&&a()},t.events.onConsentReady=function(t,i,o,s){var a,d,f,g;null==(a=window._tcf_)||null==(d=a.consentedCallback)||d.map((function(e){e(s.consentedToAll)})),null==(f=window._tcf_)||null==(g=f.TCFCallbacks)||g.map((function(e){e()})),n("Sourcepoint: onConsentReady(message_type="+t+", consentUUID="+i+", euconsent)",o),n("Sourcepoint: getMessageType() "+m());var C=w(r,e);if(void 0!==m()||_){var I="PSI"===m()?null:v(e);if(y(e),h&&l(e,p,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:m()},C&&{duration:C})),!u)return!1;l(e,p,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:m(),consentedToAll:null==s?void 0:s.consentedToAll,isFirstLayer:b||!!b},I&&{consents:I},C&&{duration:C})),b=null,_=!1,h=!1,"function"==typeof c&&c(t,i,o,s)}}})(e,i),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)}}}(i),i.accountId=i.accountId||1960,i.custom={},"psi"!==o&&(i.gdpr={includeTcfApi:"tcf"===o});i.propertyHref&&(i.joinHref=!0);var u=i.userId||(null==(s=C(e,i))?void 0:s._session.userId);u&&M(i,"loggedin","true");var p=i.clientId||(null==(a=C(window,i))?void 0:a.clientId);function d(){var e=/(_sch_cmp_[_a-z0-9]+)=(.+)/i;t.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 M(i,n,t)}))}(function(e){return T.some((function(n){return e.userAgent.includes(n)}))})(r)&&M(i,"mobile-webview",!i.showInWebview||i.showInWebview);var f=null==t||null==(c=t.referrer)?void 0:c.split("/")[2];f&&M(i,"referrer-hostname",f);d(),i.propertyHref&&t.location.search&&-1===i.propertyHref.indexOf("?")&&(i.propertyHref+=t.location.search);var g=i.pulseTracker,I=i.referrer,S=i.state,k=i.pulseObjectName,E=i.showInWebview,N=i.identityObject,A=i.identityObjectName,R=i.enableUserCentric,L=i.groupId;e.psi=e.psi||{},e.psi.isLoggedInUser=!!u,e.psi.setUserId=P,e.psi.openPrivacySettings=j,e.psi.pulseTracker=g,e.psi.clientId=p,e.psi.referrer=I,e.psi.state=S,e.psi.pulseObjectName=k,e.psi.showInWebview=E,e.psi.identityObject=N,e.psi.identityObjectName=A,e.psi.triggerLogin=O,e.psi.enableUserCentric=R,e.psi.groupId=L})(t,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,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,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)t=o[r],n.indexOf(t)>=0||(i[t]=e[t]);return i}(o,e))}(window,document,navigator,S,"psi"),function(e,n){var t=e.baseEndpoint+"/unified/wrapperMessagingWithoutDetection.js",r=n.getElementsByTagName("script"),o=r[r.length-1],s=n.createElement("link");i(s,{rel:"preload",as:"script",crossorigin:"crossorigin",href:t}),n.head.appendChild(s);var a=n.createElement("script");i(a,{async:!0,src:t}),o.insertAdjacentElement("afterend",a)}(S,document)}();