// eslint-disable-next-line no-unused-vars
import React from 'react'
function buildTrackingCode(pluginOptions) {
const {
matomoUrl,
siteId,
dev,
localScript,
requireConsent,
disableCookies
} = pluginOptions
const script = localScript ? localScript : `${matomoUrl}/piwik.js`
const html = `
window.dev = ${dev}
if (window.dev === true || !(navigator.doNotTrack === '1' || window.doNotTrack === '1')) {
window._paq = window._paq || [];
${requireConsent ? "window._paq.push(['requireConsent']);" : ''}
${disableCookies ? "window._paq.push(['disableCookies']);" : ''}
window._paq.push(['setTrackerUrl', '${matomoUrl}/piwik.php']);
window._paq.push(['setSiteId', '${siteId}']);
window._paq.push(['enableHeartBeatTimer']);
window.start = new Date();
(function() {
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src='${script}'; s.parentNode.insertBefore(g,s);
})();
if (window.dev === true) {
console.debug('[Matomo] Tracking initialized')
console.debug('[Matomo] matomoUrl: ${matomoUrl}, siteId: ${siteId}')
}
}
`
return (
)
}
function buildTrackingCodeNoJs(pluginOptions, pathname) {
const { matomoUrl, siteId, siteUrl } = pluginOptions
const html = ``
return (
)
}
function buildHead(pluginOptions) {
return (
)
}
export const onRenderBody = (
{ setHeadComponents, setPostBodyComponents, pathname },
pluginOptions
) => {
const { exclude, dev } = pluginOptions
const isProduction = process.env.NODE_ENV === 'production'
let excludePaths = ['/offline-plugin-app-shell-fallback/']
if (typeof exclude !== 'undefined') {
exclude.map(exclude => {
excludePaths.push(exclude)
})
}
const isPathExcluded = excludePaths.some(path => pathname === path)
if ((isProduction || dev === true) && !isPathExcluded) {
setHeadComponents([buildHead(pluginOptions)])
setPostBodyComponents([
buildTrackingCode(pluginOptions),
buildTrackingCodeNoJs(pluginOptions, pathname)
])
}
}