diff --git a/src/_layouts/default.html b/src/_layouts/default.html index b767a6bdc..11ad8eada 100644 --- a/src/_layouts/default.html +++ b/src/_layouts/default.html @@ -1,11 +1,11 @@ - + +/> @@ -21,8 +21,8 @@ {% endif %} - - + + {% include navbar.html %} @@ -31,5 +31,6 @@ {% include page-content.html %} {% include contribute.html %} + diff --git a/src/_webpack/plausible.js b/src/_webpack/plausible.js new file mode 100644 index 000000000..92360ca5e --- /dev/null +++ b/src/_webpack/plausible.js @@ -0,0 +1,28 @@ +const captureAllPageViews = (paramsToRegister) => { + definePlausibleFunction() + window.plausible('pageview', { u: prepareUrl(paramsToRegister) }) +} + +const definePlausibleFunction = () => { + window.plausible = window.plausible || function () { + (window.plausible.q = window.plausible.q || []).push(arguments) + } +} + +const prepareUrl = (paramsToRegister) => { + const url = new URL(window.location.href) + const queryParams = new URLSearchParams(window.location.search) + let customUrl = url.protocol + '//' + url.hostname + url + .pathname + .replace(/\/$/, '') + for (const paramName of paramsToRegister) { + const paramValue = queryParams.get(paramName)?.split(' ').join('+') + + if (paramValue) { + customUrl = customUrl + '/' + paramValue + } + } + return customUrl +} + +captureAllPageViews(['query']) diff --git a/webpack.config.js b/webpack.config.js index 5233e4471..236606f95 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,7 +3,8 @@ const path = require('path') module.exports = { entry: { search: path.join(__dirname, 'src/_webpack/search'), - redirect: path.join(__dirname, 'src/_webpack/redirect') + redirect: path.join(__dirname, 'src/_webpack/redirect'), + plausible: path.join(__dirname, 'src/_webpack/plausible') }, output: { filename: '[name].js',