diff --git a/package.json b/package.json index a6b1fbe644a63dd1d587f804097a064f79a66819..cac37568eca67b975aae29996c5f5eae5891fbd6 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build-export": "webpack --config webpack.export.js", "build-export-min": "webpack --config webpack.export.min.js", "build-export-aot": "webpack --config webpack.export.aot.js", - "build-aot": "PRODUCTION=true GIT_HASH=`git rev-parse --short HEAD` webpack --config webpack.aot.js", + "build-aot": "PRODUCTION=true GIT_HASH=`git rev-parse --short HEAD` webpack --config webpack.aot.js && node ./third_party/matomo/processMatomo.js", "plugin-server": "node ./src/plugin_examples/server.js", "dev-server": "BACKEND_URL=${BACKEND_URL:-http://localhost:3000/} webpack-dev-server --config webpack.dev.js --mode development", "dev": "npm run dev-server & (cd deploy; node server.js)", diff --git a/src/main-aot.ts b/src/main-aot.ts index c7e6456d2b3cc06db7d7a2b6d303bc31990fc895..08e0ac7705422ad472e4806f06b53a95ac280900 100644 --- a/src/main-aot.ts +++ b/src/main-aot.ts @@ -1,19 +1 @@ -import './main-common' -if (MATOMO_URL && MATOMO_ID) { - const _paq = window['_paq'] || []; - /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ - - // See DNT header and not counting visitor: https://github.com/matomo-org/matomo/issues/12001 - // Should also use main.js numbers to find number of actual visitors - _paq.push(["setDoNotTrack", true]); - - _paq.push(['trackPageView']); - _paq.push(['enableLinkTracking']); - (function() { - const u=MATOMO_URL; - _paq.push(['setTrackerUrl', u+'matomo.php']); - _paq.push(['setSiteId', MATOMO_ID]); - const d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; - g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); - })(); -} \ No newline at end of file +import './main-common' \ No newline at end of file diff --git a/third_party/matomo/includeMatomo.js b/third_party/matomo/includeMatomo.js new file mode 100644 index 0000000000000000000000000000000000000000..3bde68517e7a035692c287243b0dec4f54974497 --- /dev/null +++ b/third_party/matomo/includeMatomo.js @@ -0,0 +1,18 @@ +if (MATOMO_URL && MATOMO_ID) { + const _paq = window['_paq'] || []; + /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ + + // See DNT header and not counting visitor: https://github.com/matomo-org/matomo/issues/12001 + // Should also use main.js numbers to find number of actual visitors + _paq.push(["setDoNotTrack", true]); + + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + (function() { + const u=MATOMO_URL; + _paq.push(['setTrackerUrl', u+'matomo.php']); + _paq.push(['setSiteId', MATOMO_ID]); + const d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; + g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); + })(); +} diff --git a/third_party/matomo/processMatomo.js b/third_party/matomo/processMatomo.js new file mode 100644 index 0000000000000000000000000000000000000000..dbf145073d1e2ee4af987a5edbb8d77f244614cf --- /dev/null +++ b/third_party/matomo/processMatomo.js @@ -0,0 +1,48 @@ +// because includeMatomo.js is included via file loader +// webpack define plugin was not able to transform the variables +// hence, this script needs to be run post build-aot to transform the included includeMatomo.js + +(() => { + + if (!process.env.MATOMO_URL) return + if (!process.env.MATOMO_ID) return + + const fs = require('fs') + const path = require('path') + + const currPathMamotoJs = path.join(__dirname, './includeMatomo.js') + const pathToMatomoJs = path.join(__dirname, '../../dist/aot/includeMatomo.js') + const matomoJs = fs.readFileSync( + currPathMamotoJs, + 'utf-8' + ) + + const processed = matomoJs + .replace(/MATOMO_URL/g, JSON.stringify(process.env.MATOMO_URL || null)) + .replace(/MATOMO_ID/g, JSON.stringify(process.env.MATOMO_ID || null)) + + fs.writeFileSync( + pathToMatomoJs, + processed, + 'utf-8' + ) + + const pathToIndexHtml = path.join( + __dirname, + '../../dist/aot/index.html' + ) + + const indexHtml = fs.readFileSync( + pathToIndexHtml, + 'utf-8' + ) + + const processedIndexHtml = indexHtml + .replace('</head>', s => `<script src="includeMatomo.js"></script>${s}`) + + fs.writeFileSync( + pathToIndexHtml, + processedIndexHtml, + 'utf-8' + ) +})()