UNPKG

@schibsted/sourcepoint

Version:

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

2 lines (1 loc) 11.8 kB
!function(){const e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","enableUserCentric","groupId"];const n="__CONFIGURATION_PLACEHOLDER__";function t(){"undefined"!=typeof document&&document.location.href.indexOf("sp_debug=1")>0&&console.debug("[CMP]",...arguments)}function s(e,n,t,s,o){const r=new URL(e);r.searchParams.append("utm_source","cmp"),r.searchParams.append("utm_medium","psi"),r.searchParams.append("referrer",s||window.location.href),t&&r.searchParams.append("client_id",t),n&&r.searchParams.append("environment_id",n),o&&r.searchParams.append("state",o),window.location.href=r.href}function o(e){return null===e?null:Boolean(parseInt(e))}const r=["Mixed","PSI","SCC","TCF","PMC"],i="default",c="cmp",a="cache",u="accepted",l="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:i});function f(e,n,s,o){t("Pulse: "+s+"(actor, data):",n,o),g(e,(e=>{e&&"function"==typeof e.track&&e.track(s,{schema:"http://schema.schibsted.com/events/tracker-event.json/314.json",object:Object.assign({},o,f.pulseCommonData),provider:{component:"CMP"},actor:n})}))}function g(e,n){if(e.psi.pulseTracker)return n(e.psi.pulseTracker);const t=e.psi.pulseObjectName||"pulse";return"function"!=typeof e[t]?console.error("No Pulse integration detected. Aborting."):e[t]((e=>{"function"==typeof n&&n(e)}))}function m(){var e;const n=null==(e=f.pulseCommonData)?void 0:e.messageName;if(n)return r.find((e=>n.includes(e)))}function y(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function b(e,n,t){const s=Object.keys(v(e)).filter((n=>v(e)[n])),o=Object.keys(v(e)).filter((n=>!v(e)[n]));return s.map((e=>{const t=n.purposes[y(e)];t.optIn=!0,t.status=u})),o.map((e=>{n.purposes[y(e)].status=l})),n.source=t,n}function v(e){const n=o((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),t=o((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),s=o((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),r=o((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));var i;return i={advertising:n,analytics:t,marketing:s,personalisation:r},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 _(e,n){const t=n.performance.now();return e?Math.round((t-e)/1e3):null}let h=null,C=!1,w=!1;function P(e,n){if(n&&("object"!=typeof(t=n)||t.constructor!==Object))return console.error("Provided object is incorrect");var t;if(0===Object.keys(v(e)).length){if(!n)return d;const e=JSON.parse(JSON.stringify(n));return e.source=i,e}return b(e,JSON.parse(JSON.stringify(n||d)),a)}function I(e,n){n.events=n.events||{};const s=n.events.onMessageReceiveData,o=n.events.onMessageChoiceSelect,r=n.events.onMessageReady,i=n.events.onPrivacyManagerAction,a=n.events.onConsentReady;let u,l=!0;const p={id:n.userId,realm:n.realm};n.events.onMessageReceiveData=function(e,o){t("Sourcepoint: onMessageReceiveData(data)",o),h=!0,f.pulseCommonData={"@id":"sdrn:privacy:cmp:"+(o.messageId||""),"@type":"CMP",messageName:o.msgDescription||"",propertyId:n.propertyId,messageId:o.messageId||void 0,bucket:o.bucket||void 0,jsSdkVersion:n.jsSdkVersion,partitionId:o.prtnUUID||"",language:n.consentLanguage},"function"==typeof s&&s(o)},n.events.onMessageReady=function(){var n;null==(n=window._tcf_)||null==(n=n.TCFCallbacks)||n.map((e=>{e()})),t("Sourcepoint: onMessageReady()");const s=m();if(t("Pulse: Detected message type: "+s),!h)return f(e,p,"View",{name:"CMP: Privacy Manager displayed",messageType:m()});u=e.performance.now(),f(e,p,"View",{name:"CMP: Message displayed",messageType:s}),"function"==typeof r&&r()},n.events.onMessageChoiceSelect=function(n,s,r){if(t("Sourcepoint: onMessageReady(choice_id="+s+", choice_type_id="+r+")"),5===r){const n=_(u,e);f(e,p,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:h,messageType:m()},n&&{duration:n})),h=null}if(9===r){l=!1,h=null;const n=_(u,e);f(e,p,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:m()},n&&{duration:n}))}if(11===r&&(l=!0),12===r&&(h=!1),15===r){const n=_(u,e);f(e,p,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:m()},n&&{duration:n}))}"function"==typeof o&&o(s,r)},n.events.onPrivacyManagerAction=function(e,n){t("Sourcepoint: onPrivacyManagerAction()"),C=!0,h=!1,"none"===n.purposeConsent?(w=!0,l=!1):(w=!1,l=!0),"function"==typeof i&&i()},n.events.onConsentReady=function(n,s,o,r){var i,y;null==(i=window._tcf_)||null==(i=i.consentedCallback)||i.map((e=>{e(r.consentedToAll)})),null==(y=window._tcf_)||null==(y=y.TCFCallbacks)||y.map((e=>{e()})),t("Sourcepoint: onConsentReady(message_type="+n+", consentUUID="+s+", euconsent)",o),t("Sourcepoint: getMessageType() "+m());const P=_(u,e);if(void 0===m()&&!C)return;const I="PSI"===m()?null:v(e);if(function(e){const n=JSON.parse(JSON.stringify(d));g(e,(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),w&&f(e,p,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:m()},P&&{duration:P})),!l)return!1;f(e,p,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:m(),consentedToAll:null==r?void 0:r.consentedToAll,isFirstLayer:h||!!h},I&&{consents:I},P&&{duration:P})),h=null,C=!1,w=!1,"function"==typeof a&&a(n,s,o,r)}}function O(e,n,s){if(!n.groupPmId)throw new Error("groupPmId is required");const o="CMP:advertising",r="CMP:analytics",i="CMP:marketing",c="CMP:personalisation",a="CMP:performance_marketing";let u=[];const l={};function p(n){return t(s+": getPermissionFromCache(category="+n+")"),e.localStorage.getItem(n)}l[o]=[],l[r]=[],l[i]=[],l[c]=[],l[a]=[];let d=null;const g={subscribers:l,consentedCallback:[],TCFCallbacks:[],showPrivacyManager:function(){t("${type}: showPrivacyManager()"),f.pulseCommonData.messageName="",C=!0,h=!1,e._sp_.gdpr.loadPrivacyManagerModal(n.groupPmId)},getPermissionSync:function(e){return t(s+": getPermissionSync(category="+e+")"),p(e)},getPermission:function(e,n){t(s+": getPermission(category="+e+")");const o=g.subscribe(e,function(){let e=!1;return t=>{e?"function"==typeof o&&o():(e=!0,n(t),n=function(){})}}())},notify:(m={[o]:p(o),[r]:p(r),[i]:p(i),[c]:p(c),[a]:p(a)},(e,n)=>{if(m[e]===n)return void t(s+": notify(category="+e+", value="+n+") - IGNORED");null===d&&(d=setTimeout((()=>{const e=u;for(let n=0;n<e.length;n++)e[n]();d=null}),10)),t(s+": notify(category="+e+", value="+n+")"),m[e]=n;const o=g.subscribers[e];for(let e=0;e<o.length;e++)o[e](n)}),subscribe:function(e,n){if(t(s+": subscribe(category="+e+")"),!g.subscribers[e])return void console.error("Unable to subscribe - undefined consent category!");g.subscribers[e].push(n);const o=p(e);return null!==o&&n(o),()=>{g.subscribers[e]=g.subscribers[e].filter((e=>e!==n))}},subscribeAny:function(e){return t("subscribeAny"),u.push(e),()=>{u=u.filter((n=>n!==e))}},getCachedOrDefaultConsentsForPulse:function(n){return P(e,n)},isConsentedToAll:function(e){g.consentedCallback.push(e)},getConsentedToAllSync:function(){const e=p("_sp_user_consent_"+n.propertyId);try{var t;if(0===Object.keys(localStorage).filter((e=>e.startsWith("CMP:"))).length)return null;const n=null==(t=JSON.parse(e))||null==(t=t.gdpr)?void 0:t.consentStatus;if((null==n?void 0:n.vendorListAdditions)||(null==n?void 0:n.legalBasisChanges))return null;const s=null==n?void 0:n.consentedAll;return void 0===s?null:s}catch(e){return null}},onTcfReady:function(e){g.TCFCallbacks.push(e)}};var m;return e._SourcePoint_Consent_Callback=g.notify,g}function M(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("No Schibsted Account integration detected.")}function j(e){const n=Array.from({length:20},(()=>String.fromCharCode(Math.floor(25*Math.random()+97)))).join(""),t=btoa(JSON.stringify(n));M(e)&&M(e).login({state:t})}const S=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(e=>{try{return Boolean(new URL(e))}catch(e){return!1}})(e)?window.psi.isLoggedInUser?s(e,null,window.psi.clientId,window.psi.referrer,window.psi.state):g(window,(n=>{n.getEnvironmentId().then((n=>{s(e,n,window.psi.clientId,window.psi.referrer,window.psi.state)}))})):console.error("The URL passed to the configuration is invalid. Please, correct it.")},k=function(){return j(window)},N=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)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,n),function(e,n){e._scc_=O(e,n)}(window,n),function(n,s,o,r,i){(function(e,n,t,s,o){var r,i,c;if(!s.baseEndpoint||!s.propertyId||!s.consentLanguage)throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");I(e,s),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)}}}(s),s.accountId=s.accountId||1047,s.custom={},s.gdpr={includeTcfApi:"tcf"===o},s.propertyHref&&(s.joinHref=!0);const a=s.userId||(null==(r=M(e,s))?void 0:r._session.userId);a&&N(s,"loggedin","true");const u=s.clientId||(null==(i=M(e,s))?void 0:i.clientId);function l(){const e=/(_sch_cmp_[_a-z0-9]+)=(.+)/i;n.cookie.split(";").filter((n=>e.test(n))).map((e=>e.split("=").map((e=>e.trim())))).forEach((e=>{let[n,t]=e;return N(s,n,t)}))}(function(e){return A.some((n=>e.userAgent.includes(n)))})(t)&&N(s,"mobile-webview",!s.showInWebview||s.showInWebview);const p=null==n||null==(c=n.referrer)?void 0:c.split("/")[2];p&&N(s,"referrer-hostname",p);l(),s.propertyHref&&n.location.search&&-1===s.propertyHref.indexOf("?")&&(s.propertyHref+=n.location.search);const{pulseTracker:d,referrer:f,state:g,pulseObjectName:m,showInWebview:y,identityObject:b,identityObjectName:v,enableUserCentric:_,groupId:h}=s;e.psi=e.psi||{},e.psi.isLoggedInUser=!!a,e.psi.setUserId=S,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=b,e.psi.identityObjectName=v,e.psi.triggerLogin=k,e.psi.enableUserCentric=_,e.psi.groupId=h})(n,s,o,r,i),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 s in e)if({}.hasOwnProperty.call(e,s)){if(n.includes(s))continue;t[s]=e[s]}return t}(r,e))}(window,document,navigator,n,"scc"),t("dispatching cmp:ready event"),document.dispatchEvent(new Event("cmp:ready"))}();