2018-05-07 18:29:27 +02:00
|
|
|
import React from 'react'
|
|
|
|
|
2018-05-08 20:52:43 +02:00
|
|
|
function buildTrackingCode(pluginOptions) {
|
2018-05-07 18:29:27 +02:00
|
|
|
const html = `
|
2018-05-08 20:52:43 +02:00
|
|
|
window.dev = ${pluginOptions.dev}
|
|
|
|
|
|
|
|
if (window.dev === true || !(navigator.doNotTrack == '1' || window.doNotTrack == '1')) {
|
2018-05-07 18:29:27 +02:00
|
|
|
window._paq = window._paq || [];
|
2018-05-08 20:52:43 +02:00
|
|
|
window._paq.push(['setTrackerUrl', '${pluginOptions.matomoUrl}/piwik.php']);
|
|
|
|
window._paq.push(['setSiteId', '${pluginOptions.siteId}']);
|
2018-05-07 18:29:27 +02:00
|
|
|
window._paq.push(['enableLinkTracking']);
|
|
|
|
window._paq.push(['trackPageView']);
|
|
|
|
window._paq.push(['enableHeartBeatTimer']);
|
|
|
|
window.start = new Date();
|
|
|
|
|
2018-05-08 20:52:43 +02:00
|
|
|
if (window.dev === true) {
|
|
|
|
console.log('[Matomo] Tracking initialized')
|
|
|
|
console.log('[Matomo] matomoUrl: ${pluginOptions.matomoUrl}, siteId: ${pluginOptions.siteId}')
|
|
|
|
}
|
|
|
|
|
2018-05-07 18:29:27 +02:00
|
|
|
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
|
2018-05-08 20:52:43 +02:00
|
|
|
g.defer=true; g.async=true;
|
|
|
|
g.src='${pluginOptions.matomoUrl}/piwik.js';
|
2018-05-07 18:29:27 +02:00
|
|
|
s.parentNode.insertBefore(g,s);
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
return (
|
|
|
|
<script
|
|
|
|
key={'gatsby-plugin-matomo'}
|
|
|
|
type="text/javascript"
|
|
|
|
dangerouslySetInnerHTML={{ __html: html }}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-08 20:52:43 +02:00
|
|
|
function buildTrackingCodeNoJs(pluginOptions, pathname) {
|
|
|
|
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-05-08 20:52:43 +02:00
|
|
|
if (process.env.NODE_ENV === 'production' || pluginOptions.dev === true) {
|
2018-05-08 12:45:47 +02:00
|
|
|
return setPostBodyComponents([
|
2018-05-08 20:52:43 +02:00
|
|
|
buildTrackingCode(pluginOptions),
|
|
|
|
buildTrackingCodeNoJs(pluginOptions, pathname)
|
2018-05-08 12:45:47 +02:00
|
|
|
])
|
|
|
|
}
|
2018-05-07 18:29:27 +02:00
|
|
|
return null
|
|
|
|
}
|