// Taken from https://github.com/PiwikPRO/react-piwik-pro/blob/master/src/core/index.ts export default function initializeDatalayer( siteId: string, containerUrl: string | undefined = 'https://dap.amsterdam.nl', nonce?: string, ) { if (!siteId) { throw new Error('Empty tracking code for Piwik Pro.') } if (!document) { console.error( 'Was not possible to access Document interface. Make sure this module is running on a Browser w/ access do Document interface.', ) } const s: HTMLScriptElement = document.createElement('script') s.async = false if (nonce) { s.setAttribute('nonce', nonce) } s.text = `(function(window, document, dataLayerName, id) { window[dataLayerName]=window[dataLayerName]||[],window[dataLayerName].push({start:(new Date).getTime(),event:"stg.start"});var scripts=document.getElementsByTagName('script')[0],tags=document.createElement('script'); function stgCreateCookie(a,b,c){var d="";if(c){var e=new Date;e.setTime(e.getTime()+24*c*60*60*1e3),d="; expires="+e.toUTCString()}document.cookie=a+"="+b+d+"; path=/"} var isStgDebug=(window.location.href.match("stg_debug")||document.cookie.match("stg_debug"))&&!window.location.href.match("stg_disable_debug");stgCreateCookie("stg_debug",isStgDebug?1:"",isStgDebug?14:-1); var qP=[];dataLayerName!=="dataLayer"&&qP.push("data_layer_name="+dataLayerName),isStgDebug&&qP.push("stg_debug");var qPString=qP.length>0?("?"+qP.join("&")):""; tags.async=!0,tags.src="${containerUrl}/containers/"+id+".js"+qPString,scripts.parentNode.insertBefore(tags,scripts); !function(a,n,i){a[n]=a[n]||{};for(var c=0;c