@schibsted/sourcepoint
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 9.24 kB
JavaScript
!function(){const e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","enableUserCentric","groupId"];function n(e,n){var t,s;if(null!=(t=e.psi)&&t.identityObject)return e.psi.identityObject;if(null!=n&&n.identityObject)return n.identityObject;const o=(null==(s=e.psi)?void 0:s.identityObjectName)||(null==n?void 0:n.identityObjectName)||"Identity";return e[o]?e[o]:console.warn("@schibsted/sourcepoint: No Schibsted Account integration detected.")}function t(e){const t=Array.from({length:20},(()=>String.fromCharCode(Math.floor(25*Math.random()+97)))).join(""),s=btoa(JSON.stringify(t));n(e)&&n(e).login({state:s})}const s=["Mixed","PSI","SCC","TCF","PMC"],o="default",i="cmp",r="accepted",c="rejected",a="unknown",p=Object.freeze({purposes:{CMP_ANALYTICS:{optIn:!1,status:a},CMP_MARKETING:{optIn:!1,status:a},CMP_ADVERTISING:{optIn:!1,status:a},CMP_PERSONALIZATION:{optIn:!1,status:a}},source:o});function u(e,n,t,s){v("Pulse: "+t+"(actor, data):",n,s),d(e,(e=>{e&&"function"==typeof e.track&&e.track(t,{schema:"http://schema.schibsted.com/events/tracker-event.json/314.json",object:Object.assign({},s,u.pulseCommonData),provider:{component:"CMP"},actor:n})}))}function d(e,n){var t,s;if(null!=(t=e.psi)&&t.pulseTracker)return n(e.psi.pulseTracker);const o=(null==(s=e.psi)?void 0:s.pulseObjectName)||"pulse";return"function"!=typeof e[o]?console.error("No Pulse integration detected. Aborting."):e[o]((e=>{"function"==typeof n&&n(e)}))}function l(){var e;const n=null==(e=u.pulseCommonData)?void 0:e.messageName;if(n)return s.find((e=>n.includes(e)))}function f(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function m(e){const n=JSON.parse(JSON.stringify(p));return d(e,(t=>{try{t.setConsents(function(e,n,t){const s=Object.keys(g(e)).filter((n=>g(e)[n])),o=Object.keys(g(e)).filter((n=>!g(e)[n]));return s.map((e=>{const t=n.purposes[f(e)];t.optIn=!0,t.status=r})),o.map((e=>{n.purposes[f(e)].status=c})),n.source=t,n}(e,n,i))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}function g(e){const n=_((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),t=_((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),s=_((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),o=_((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));var i;return i={advertising:n,analytics:t,marketing:s,personalisation:o},Object.entries(i).filter((e=>{let[n,t]=e;return null!=t})).reduce(((e,n)=>{let[t,s]=n;return Object.assign({},e,{[t]:s})}),{})}function y(e,n){const t=n.performance.now();return e?Math.round((t-e)/1e3):null}function v(){"undefined"!=typeof document&&document.location.href.indexOf("sp_debug=1")>0&&console.debug("[CMP]",...arguments)}function w(e,n,t,s,o){const i=new URL(e);i.searchParams.append("utm_source","cmp"),i.searchParams.append("utm_medium","psi"),i.searchParams.append("referrer",s||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 _(e){return null===e?null:Boolean(parseInt(e))}function b(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t}const h=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},C=function(e){return(e=>{try{return Boolean(new URL(e))}catch(e){return!1}})(e)?window.psi.isLoggedInUser?w(e,null,window.psi.clientId,window.psi.referrer,window.psi.state):d(window,(n=>{n.getEnvironmentId().then((n=>{w(e,n,window.psi.clientId,window.psi.referrer,window.psi.state)}))})):console.error("The URL passed to the configuration is invalid. Please, correct it.")},I=function(){return t(window)};let P=null,O=!1,j=!1;const M=["Hermes","_app_","tv.nu","FinnApp","Omni"];const T="__CONFIGURATION_PLACEHOLDER__";!function(e,n){if(n.disableSentry)return;const t=e.createElement("script");var s,o;s=t,o={async:!0,type:"module",src:"https://static.privacy.schibsted.com/dist/index.js",fetchPriority:"low"},Object.keys(o).filter((e=>void 0!==s[e])).forEach((e=>"object"==typeof o[e]?Object.keys(o[e]).forEach((n=>s[e][n]=o[e][n])):s[e]=o[e])),e.head.appendChild(t)}(document,T),function(t,s,o,i){(function(e,t,s,o){var i,r,c;if(!o.baseEndpoint||!o.propertyId||!o.consentLanguage)throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");(function(e,n){n.events=n.events||{};const t=n.events.onMessageReceiveData,s=n.events.onMessageChoiceSelect,o=n.events.onMessageReady,i=n.events.onPrivacyManagerAction,r=n.events.onConsentReady;let c,a=!0;const p={id:n.userId,realm:n.realm};n.events.onMessageReceiveData=function(e,s){v("Sourcepoint: onMessageReceiveData(data)",s),P=!0,u.pulseCommonData={"@id":"sdrn:privacy:cmp:"+(s.messageId||""),"@type":"CMP",messageName:s.msgDescription||"",propertyId:n.propertyId,messageId:s.messageId||void 0,bucket:s.bucket||void 0,jsSdkVersion:n.jsSdkVersion,partitionId:s.prtnUUID||"",language:n.consentLanguage},"function"==typeof t&&t(s)},n.events.onMessageReady=function(){var n;null==(n=window._tcf_)||null==(n=n.TCFCallbacks)||n.map((e=>{e()})),v("Sourcepoint: onMessageReady()");const t=l();if(v("Pulse: Detected message type: "+t),!P)return u(e,p,"View",{name:"CMP: Privacy Manager displayed",messageType:l()});c=e.performance.now(),u(e,p,"View",{name:"CMP: Message displayed",messageType:t}),"function"==typeof o&&o()},n.events.onMessageChoiceSelect=function(n,t,o){if(v("Sourcepoint: onMessageReady(choice_id="+t+", choice_type_id="+o+")"),5===o){const n=y(c,e);u(e,p,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:P,messageType:l()},n&&{duration:n})),P=null}if(9===o){a=!1,P=null;const n=y(c,e);u(e,p,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:l()},n&&{duration:n}))}if(11===o&&(a=!0),12===o&&(P=!1),15===o){const n=y(c,e);u(e,p,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:l()},n&&{duration:n}))}"function"==typeof s&&s(t,o)},n.events.onPrivacyManagerAction=function(e,n){v("Sourcepoint: onPrivacyManagerAction()"),O=!0,P=!1,"none"===n.purposeConsent?(j=!0,a=!1):(j=!1,a=!0),"function"==typeof i&&i()},n.events.onConsentReady=function(n,t,s,o){var i,d;null==(i=window._tcf_)||null==(i=i.consentedCallback)||i.map((e=>{e(o.consentedToAll)})),null==(d=window._tcf_)||null==(d=d.TCFCallbacks)||d.map((e=>{e()})),v("Sourcepoint: onConsentReady(message_type="+n+", consentUUID="+t+", euconsent)",s),v("Sourcepoint: getMessageType() "+l());const f=y(c,e);if(void 0===l()&&!O)return;const w="PSI"===l()?null:g(e);if(m(e),j&&u(e,p,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:l()},f&&{duration:f})),!a)return!1;u(e,p,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:l(),consentedToAll:null==o?void 0:o.consentedToAll,isFirstLayer:P||!!P},w&&{consents:w},f&&{duration:f})),P=null,O=!1,j=!1,"function"==typeof r&&r(n,t,s,o)}})(e,o),function(e){e.events=e.events||{},e.events.onError=function(n,t,s){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(s)}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)}}}(o),o.accountId=o.accountId||1047,o.custom={},o.propertyHref&&(o.joinHref=!0);const a=o.userId||(null==(i=n(e,o))?void 0:i._session.userId);a&&b(o,"loggedin","true");const p=o.clientId||(null==(r=n(e,o))?void 0:r.clientId);function d(){const e=/(_sch_cmp_[_a-z0-9]+)=(.+)/i;t.cookie.split(";").filter((n=>e.test(n))).map((e=>e.split("=").map((e=>e.trim())))).forEach((e=>{let[n,t]=e;return b(o,n,t)}))}(function(e){return M.some((n=>e.userAgent.includes(n)))})(s)&&b(o,"mobile-webview",!o.showInWebview||o.showInWebview);const f=null==t||null==(c=t.referrer)?void 0:c.split("/")[2];f&&b(o,"referrer-hostname",f);d(),o.propertyHref&&t.location.search&&-1===o.propertyHref.indexOf("?")&&(o.propertyHref+=t.location.search);const{pulseTracker:w,referrer:_,state:T,pulseObjectName:S,showInWebview:k,identityObject:E,identityObjectName:N,enableUserCentric:A,groupId:R}=o;e.psi=e.psi||{},e.psi.isLoggedInUser=!!a,e.psi.setUserId=h,e.psi.openPrivacySettings=C,e.psi.pulseTracker=w,e.psi.clientId=p,e.psi.referrer=_,e.psi.state=T,e.psi.pulseObjectName=S,e.psi.showInWebview=k,e.psi.identityObject=E,e.psi.identityObjectName=N,e.psi.triggerLogin=I,e.psi.enableUserCentric=A,e.psi.groupId=R})(t,s,o,i),function(e,n){e._sp_=e._sp_||{},e._sp_.config=n,v("Sourcepoint initiated with config: ",n)}(t,function(e,n){if(null==e)return{};var t={};for(var s in e)if({}.hasOwnProperty.call(e,s)){if(n.includes(s))continue;t[s]=e[s]}return t}(i,e))}(window,document,navigator,T),dispatchCmpInitEvent()}();