Skip to content

Commit c769cee

Browse files
committed
Add global analytics with client module
- Replace page-specific Matomo component with global client module - Analytics runs on all pages automatically via clientModules - Restrict analytics to localhost and Netlify preview deployments only - Use site ID 6 for all environments - Support SPA navigation tracking for page views Signed-off-by: Pete Cheslock <[email protected]>
1 parent c9ee5fa commit c769cee

File tree

4 files changed

+100
-17
lines changed

4 files changed

+100
-17
lines changed

docusaurus.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ const config = {
5959
locales: ["en"],
6060
},
6161

62+
// Analytics configuration
63+
customFields: {
64+
analytics: {
65+
matomo: {
66+
baseUrl: "//analytics.ossupstream.org/",
67+
siteId: "6",
68+
enabled: true,
69+
},
70+
},
71+
},
72+
6273
presets: [
6374
[
6475
"classic",
@@ -95,6 +106,11 @@ const config = {
95106
}),
96107
],
97108
],
109+
110+
// Client modules - run on every page
111+
clientModules: [
112+
require.resolve('./src/clientModules/analytics.js'),
113+
],
98114

99115
// Plugins configuration
100116
plugins: [

src/clientModules/analytics.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Client module for global Matomo analytics - runs on every page
2+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
3+
4+
let isInitialized = false;
5+
6+
function initializeAnalytics() {
7+
// Only run analytics on development or Netlify preview deployments
8+
const isLocalDevelopment = window.location.hostname === 'localhost';
9+
const isNetlifyPreview = window.location.hostname.includes('netlify.app') ||
10+
window.location.hostname.includes('netlify.com');
11+
12+
if (!isLocalDevelopment && !isNetlifyPreview) {
13+
console.log('Matomo: Analytics disabled - not running on localhost or Netlify preview');
14+
return;
15+
}
16+
17+
console.log('Matomo: Initializing analytics with site ID 6 on', window.location.hostname);
18+
19+
// Initialize Matomo (only once)
20+
if (!isInitialized) {
21+
window._paq = window._paq || [];
22+
window._paq.push(['trackPageView']);
23+
window._paq.push(['enableLinkTracking']);
24+
25+
(function() {
26+
const u = "//analytics.ossupstream.org/";
27+
window._paq.push(['setTrackerUrl', u + 'matomo.php']);
28+
window._paq.push(['setSiteId', '6']);
29+
const d = document,
30+
g = d.createElement('script'),
31+
s = d.getElementsByTagName('script')[0];
32+
g.async = true;
33+
g.src = u + 'matomo.js';
34+
s.parentNode.insertBefore(g, s);
35+
})();
36+
37+
isInitialized = true;
38+
39+
// Listen for route changes to track page views
40+
const originalPushState = history.pushState;
41+
const originalReplaceState = history.replaceState;
42+
43+
history.pushState = function() {
44+
originalPushState.apply(history, arguments);
45+
setTimeout(() => {
46+
if (window._paq) {
47+
window._paq.push(['setCustomUrl', window.location.href]);
48+
window._paq.push(['setDocumentTitle', document.title]);
49+
window._paq.push(['trackPageView']);
50+
}
51+
}, 100);
52+
};
53+
54+
history.replaceState = function() {
55+
originalReplaceState.apply(history, arguments);
56+
setTimeout(() => {
57+
if (window._paq) {
58+
window._paq.push(['setCustomUrl', window.location.href]);
59+
window._paq.push(['setDocumentTitle', document.title]);
60+
window._paq.push(['trackPageView']);
61+
}
62+
}, 100);
63+
};
64+
65+
window.addEventListener('popstate', () => {
66+
setTimeout(() => {
67+
if (window._paq) {
68+
window._paq.push(['setCustomUrl', window.location.href]);
69+
window._paq.push(['setDocumentTitle', document.title]);
70+
window._paq.push(['trackPageView']);
71+
}
72+
}, 100);
73+
});
74+
}
75+
}
76+
77+
// Initialize analytics when DOM is ready
78+
if (ExecutionEnvironment.canUseDOM) {
79+
if (document.readyState === 'loading') {
80+
document.addEventListener('DOMContentLoaded', initializeAnalytics);
81+
} else {
82+
initializeAnalytics();
83+
}
84+
}

src/components/Matomo/index.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/pages/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Install from '@site/src/components/Install'
66
import Demo from '@site/src/components/Demo'
77
import About from '@site/src/components/About'
88

9-
import Matomo from '@site/src/components/Matomo'
109
import VideoEmbed from '@site/src/components/VideoEmbed'
1110

1211
export default function Home() {
@@ -15,7 +14,6 @@ export default function Home() {
1514
<Layout
1615
title={`Welcome to the ${siteConfig.title} website!`}
1716
description="llm-d: a Kubernetes-native high-performance distributed LLM inference framework">
18-
<script src={Matomo} />
1917
<main>
2018
<Welcome />
2119

0 commit comments

Comments
 (0)