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,
|
|
|
|
siteId,
|
|
|
|
dev,
|
|
|
|
localScript,
|
|
|
|
requireConsent,
|
|
|
|
disableCookies
|
|
|
|
} = pluginOptions
|
|
|
|
|
|
|
|
const script = localScript ? localScript : `${matomoUrl}/piwik.js`
|
2018-05-10 23:53:41 +02:00
|
|
|
|
2018-05-07 18:29:27 +02:00
|
|
|
const html = `
|
2019-06-10 02:04:28 +02:00
|
|
|
window.dev = ${dev}
|
2018-12-05 20:33:18 +01:00
|
|
|
if (window.dev === true || !(navigator.doNotTrack === '1' || window.doNotTrack === '1')) {
|
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']);" : ''}
|
|
|
|
${disableCookies ? "window._paq.push(['disableCookies']);" : ''}
|
|
|
|
window._paq.push(['setTrackerUrl', '${matomoUrl}/piwik.php']);
|
|
|
|
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) {
|
|
|
|
console.log('[Matomo] Tracking initialized')
|
2019-06-10 02:04:28 +02:00
|
|
|
console.log('[Matomo] matomoUrl: ${matomoUrl}, siteId: ${siteId}')
|
2018-05-08 20:52:43 +02:00
|
|
|
}
|
2018-05-07 18:29:27 +02:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
return (
|
|
|
|
<script
|
|
|
|
key={'gatsby-plugin-matomo'}
|
|
|
|
dangerouslySetInnerHTML={{ __html: html }}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-08 20:52:43 +02:00
|
|
|
function buildTrackingCodeNoJs(pluginOptions, pathname) {
|
2019-06-10 02:04:28 +02:00
|
|
|
const html = `<img src="${pluginOptions.matomoUrl}/piwik.php?idsite=${
|
|
|
|
pluginOptions.siteId
|
|
|
|
}&rec=1&url=${pluginOptions.siteUrl +
|
|
|
|
pathname}" style="border:0" alt="tracker" />`
|
2018-05-07 18:29:27 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<noscript
|
|
|
|
key={'gatsby-plugin-matomo'}
|
|
|
|
dangerouslySetInnerHTML={{ __html: html }}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.onRenderBody = ({ setPostBodyComponents, pathname }, pluginOptions) => {
|
2018-12-05 20:33:18 +01:00
|
|
|
let excludePaths = ['/offline-plugin-app-shell-fallback/']
|
|
|
|
|
|
|
|
if (typeof pluginOptions.exclude !== 'undefined') {
|
|
|
|
pluginOptions.exclude.map(exclude => {
|
|
|
|
excludePaths.push(exclude)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const isPathExcluded = excludePaths.some(path => pathname === path)
|
|
|
|
|
|
|
|
if (
|
|
|
|
(process.env.NODE_ENV === 'production' || pluginOptions.dev === true) &&
|
|
|
|
!isPathExcluded
|
|
|
|
) {
|
2019-06-10 02:04:28 +02:00
|
|
|
return setPostBodyComponents([
|
|
|
|
buildTrackingCode(pluginOptions),
|
|
|
|
buildTrackingCodeNoJs(pluginOptions, pathname)
|
|
|
|
])
|
2018-05-08 12:45:47 +02:00
|
|
|
}
|
2018-05-07 18:29:27 +02:00
|
|
|
return null
|
|
|
|
}
|