2020-02-15 14:07:20 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2018-05-07 18:29:27 +02:00
|
|
|
import React from 'react'
|
|
|
|
|
2018-05-08 20:52:43 +02:00
|
|
|
function buildTrackingCode(pluginOptions) {
|
2019-06-10 02:04:28 +02:00
|
|
|
const {
|
|
|
|
matomoUrl,
|
2020-03-21 16:15:27 +01:00
|
|
|
matomoPhpScript = 'piwik.php',
|
|
|
|
matomoJsScript = 'piwik.js',
|
2019-06-10 02:04:28 +02:00
|
|
|
siteId,
|
|
|
|
dev,
|
|
|
|
localScript,
|
|
|
|
requireConsent,
|
2021-05-23 11:06:07 +02:00
|
|
|
requireCookieConsent,
|
2020-02-24 00:48:12 +01:00
|
|
|
disableCookies,
|
2020-12-12 12:20:15 +01:00
|
|
|
cookieDomain,
|
2021-05-23 11:37:14 +02:00
|
|
|
enableJSErrorTracking,
|
2020-12-12 12:20:15 +01:00
|
|
|
respectDnt = true
|
2019-06-10 02:04:28 +02:00
|
|
|
} = pluginOptions
|
|
|
|
|
2020-03-21 16:15:27 +01:00
|
|
|
const script = localScript ? localScript : `${matomoUrl}/${matomoJsScript}`
|
2018-05-10 23:53:41 +02:00
|
|
|
|
2020-12-12 12:20:15 +01:00
|
|
|
const dntCondition = respectDnt
|
|
|
|
? `!(navigator.doNotTrack === '1' || window.doNotTrack === '1')`
|
|
|
|
: `true`
|
|
|
|
|
2018-05-07 18:29:27 +02:00
|
|
|
const html = `
|
2019-06-10 02:04:28 +02:00
|
|
|
window.dev = ${dev}
|
2020-12-12 12:20:15 +01:00
|
|
|
if (window.dev === true || ${dntCondition}) {
|
2018-05-07 18:29:27 +02:00
|
|
|
window._paq = window._paq || [];
|
2019-06-10 02:04:28 +02:00
|
|
|
${requireConsent ? "window._paq.push(['requireConsent']);" : ''}
|
2021-05-23 11:17:34 +02:00
|
|
|
${
|
|
|
|
requireCookieConsent
|
|
|
|
? "window._paq.push(['requireCookieConsent']);"
|
|
|
|
: ''
|
|
|
|
}
|
2019-06-10 02:04:28 +02:00
|
|
|
${disableCookies ? "window._paq.push(['disableCookies']);" : ''}
|
2021-05-23 11:37:14 +02:00
|
|
|
${
|
|
|
|
enableJSErrorTracking
|
|
|
|
? "window._paq.push(['enableJSErrorTracking']);"
|
|
|
|
: ''
|
|
|
|
}
|
2020-02-24 00:48:12 +01:00
|
|
|
${
|
2020-03-22 16:06:09 +01:00
|
|
|
cookieDomain
|
|
|
|
? `window._paq.push(['setCookieDomain', '${cookieDomain}']);`
|
|
|
|
: ''
|
|
|
|
}
|
2020-03-21 16:15:27 +01:00
|
|
|
window._paq.push(['setTrackerUrl', '${matomoUrl}/${matomoPhpScript}']);
|
2019-06-10 02:04:28 +02:00
|
|
|
window._paq.push(['setSiteId', '${siteId}']);
|
2018-05-07 18:29:27 +02:00
|
|
|
window._paq.push(['enableHeartBeatTimer']);
|
|
|
|
window.start = new Date();
|
2018-06-28 19:53:03 +02:00
|
|
|
|
2018-07-29 09:35:46 +02:00
|
|
|
(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);
|
|
|
|
})();
|
2018-06-28 19:53:03 +02:00
|
|
|
|
2018-05-08 20:52:43 +02:00
|
|
|
if (window.dev === true) {
|
2020-02-15 14:07:20 +01:00
|
|
|
console.debug('[Matomo] Tracking initialized')
|
|
|
|
console.debug('[Matomo] matomoUrl: ${matomoUrl}, siteId: ${siteId}')
|
2018-05-08 20:52:43 +02:00
|
|
|
}
|
2018-05-07 18:29:27 +02:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
return (
|
|
|
|
<script
|
2020-02-15 14:07:20 +01:00
|
|
|
key="script-gatsby-plugin-matomo"
|
2018-05-07 18:29:27 +02:00
|
|
|
dangerouslySetInnerHTML={{ __html: html }}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-08 20:52:43 +02:00
|
|
|
function buildTrackingCodeNoJs(pluginOptions, pathname) {
|
2020-03-22 16:06:09 +01:00
|
|
|
const {
|
|
|
|
matomoUrl,
|
|
|
|
matomoPhpScript = 'piwik.php',
|
|
|
|
siteId,
|
|
|
|
siteUrl
|
|
|
|
} = pluginOptions
|
|
|
|
const html = `<img src="${matomoUrl}/${matomoPhpScript}?idsite=${siteId}&rec=1&url=${
|
|
|
|
siteUrl + pathname
|
|
|
|
}" style="border:0" alt="tracker" />`
|
2018-05-07 18:29:27 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<noscript
|
2020-02-15 14:07:20 +01:00
|
|
|
key="noscript-gatsby-plugin-matomo"
|
2018-05-07 18:29:27 +02:00
|
|
|
dangerouslySetInnerHTML={{ __html: html }}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-10 01:47:38 +02:00
|
|
|
function buildHead(pluginOptions) {
|
|
|
|
return (
|
|
|
|
<link
|
|
|
|
rel="preconnect"
|
|
|
|
href={pluginOptions.matomoUrl}
|
2020-02-15 14:07:20 +01:00
|
|
|
key="preconnect-gatsby-plugin-matomo"
|
2019-06-10 01:47:38 +02:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-15 14:07:20 +01:00
|
|
|
export const onRenderBody = (
|
2019-06-10 01:47:38 +02:00
|
|
|
{ setHeadComponents, setPostBodyComponents, pathname },
|
|
|
|
pluginOptions
|
|
|
|
) => {
|
2020-02-15 14:07:20 +01:00
|
|
|
const isProduction = process.env.NODE_ENV === 'production'
|
2018-12-05 20:33:18 +01:00
|
|
|
let excludePaths = ['/offline-plugin-app-shell-fallback/']
|
|
|
|
|
2020-02-15 15:21:24 +01:00
|
|
|
if (pluginOptions && typeof pluginOptions.exclude !== 'undefined') {
|
2020-03-22 16:06:09 +01:00
|
|
|
pluginOptions.exclude.map((exclude) => {
|
2018-12-05 20:33:18 +01:00
|
|
|
excludePaths.push(exclude)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-22 16:06:09 +01:00
|
|
|
const isPathExcluded = excludePaths.some((path) => pathname === path)
|
2018-12-05 20:33:18 +01:00
|
|
|
|
2020-02-15 15:21:24 +01:00
|
|
|
if (
|
|
|
|
(isProduction || (pluginOptions && pluginOptions.dev === true)) &&
|
|
|
|
!isPathExcluded
|
|
|
|
) {
|
2019-06-13 19:42:07 +02:00
|
|
|
setHeadComponents([buildHead(pluginOptions)])
|
|
|
|
setPostBodyComponents([
|
|
|
|
buildTrackingCode(pluginOptions),
|
|
|
|
buildTrackingCodeNoJs(pluginOptions, pathname)
|
|
|
|
])
|
2018-05-08 12:45:47 +02:00
|
|
|
}
|
2018-05-07 18:29:27 +02:00
|
|
|
}
|