From a2a9bc88a29ca271babfb83de54b44c61f489439 Mon Sep 17 00:00:00 2001 From: Hendrik Eeckhaut Date: Thu, 15 May 2025 21:31:54 +0200 Subject: [PATCH] Added matomo tracking --- docusaurus.config.ts | 4 ++++ plugins/matomo-plugin/index.ts | 35 +++++++++++++++++++++++++++++ src/components/MatomoHead/index.tsx | 23 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 plugins/matomo-plugin/index.ts create mode 100644 src/components/MatomoHead/index.tsx diff --git a/docusaurus.config.ts b/docusaurus.config.ts index a7d22b3..2cd5fd9 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -3,6 +3,7 @@ import type { Config } from '@docusaurus/types'; import type * as Preset from '@docusaurus/preset-classic'; import remarkMath from 'remark-math'; import rehypeKatex from 'rehype-katex'; +import matomoPlugin from './plugins/matomo-plugin'; // This runs in Node.js - Don't use client-side code here (browser APIs, JSX...) @@ -25,6 +26,9 @@ const config: Config = { onBrokenLinks: 'throw', onBrokenMarkdownLinks: 'warn', + + plugins: [matomoPlugin], + // Even if you don't use internationalization, you can use this field to set // useful metadata like html lang. For example, if your site is Chinese, you // may want to replace "en" with "zh-Hans". diff --git a/plugins/matomo-plugin/index.ts b/plugins/matomo-plugin/index.ts new file mode 100644 index 0000000..3934403 --- /dev/null +++ b/plugins/matomo-plugin/index.ts @@ -0,0 +1,35 @@ +import type { Plugin } from '@docusaurus/types'; + +export default function matomoHeadPlugin(): Plugin { + const isProd = process.env.NODE_ENV === 'production'; + + return { + name: 'matomo-plugin', + injectHtmlTags() { + if (!isProd) { + return {}; + } + return { + headTags: [ + { + tagName: 'script', + attributes: { + id: 'matomo-tracking', + strategy: 'afterInteractive', + }, + innerHTML: `var _paq = window._paq = window._paq || []; +_paq.push(['trackPageView']); +_paq.push(['enableLinkTracking']); +(function() { +var u="https://psedev.matomo.cloud/"; +_paq.push(['setTrackerUrl', u+'matomo.php']); +_paq.push(['setSiteId', '16']); +var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; +g.async=true; g.src='//cdn.matomo.cloud/psedev.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s); +})();`, + }, + ], + }; + }, + }; +} \ No newline at end of file diff --git a/src/components/MatomoHead/index.tsx b/src/components/MatomoHead/index.tsx new file mode 100644 index 0000000..6710506 --- /dev/null +++ b/src/components/MatomoHead/index.tsx @@ -0,0 +1,23 @@ +import React from 'react'; +import Head from '@docusaurus/Head'; + +export default function MatomoHead() { + return ( + + + + ); +} \ No newline at end of file