1- // Client module for global Matomo analytics - runs on every page
1+ // Client module for global Matomo analytics
22import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment' ;
33
44let isInitialized = false ;
55
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' ) ;
6+ function resolveMatomoSiteId ( hostname ) {
7+ if ( ! hostname ) return null ;
8+ const host = hostname . toLowerCase ( ) ;
9+ const isPreview = host === 'localhost' || host . includes ( 'netlify.app' ) || host . includes ( 'netlify.com' ) ;
10+ const isProd = host === 'llm-d.ai' || host . endsWith ( '.llm-d.ai' ) ;
11+ if ( isPreview ) return '6' ;
12+ if ( isProd ) return '7' ;
13+ return null ; // unknown host -> do not track
14+ }
15+
16+ function ensureMatomoInitialized ( ) {
17+ if ( isInitialized ) return ;
18+ const siteId = resolveMatomoSiteId ( window . location . hostname ) ;
19+ if ( ! siteId ) {
20+ // Not a recognized host; keep analytics disabled
1421 return ;
1522 }
1623
17- console . log ( 'Matomo: Initializing analytics with site ID 6 on' , window . location . hostname ) ;
24+ window . _paq = window . _paq || [ ] ;
25+ window . _paq . push ( [ 'disableCookies' ] ) ;
26+ window . _paq . push ( [ 'alwaysUseSendBeacon' , true ] ) ;
27+ window . _paq . push ( [ 'enableLinkTracking' ] ) ;
1828
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 ;
29+ const baseUrl = '//analytics.ossupstream.org/' ;
30+ window . _paq . push ( [ 'setTrackerUrl' , baseUrl + 'matomo.php' ] ) ;
31+ window . _paq . push ( [ 'setSiteId' , siteId ] ) ;
3832
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- } ;
33+ const d = document ;
34+ const g = d . createElement ( 'script' ) ;
35+ const s = d . getElementsByTagName ( 'script' ) [ 0 ] ;
36+ g . async = true ;
37+ g . src = baseUrl + 'matomo.js' ;
38+ s . parentNode . insertBefore ( g , s ) ;
6439
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- }
40+ isInitialized = true ;
7541}
7642
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 ( ) ;
43+ export function onRouteDidUpdate ( { location, previousLocation} ) {
44+ if ( ! ExecutionEnvironment . canUseDOM ) return ;
45+ ensureMatomoInitialized ( ) ;
46+ if ( ! window . _paq || ! isInitialized ) return ;
47+
48+ // Track SPA navigations
49+ if ( previousLocation ) {
50+ const referrer = `${ previousLocation . pathname } ${ previousLocation . search || '' } ${ previousLocation . hash || '' } ` ;
51+ window . _paq . push ( [ 'setReferrerUrl' , referrer ] ) ;
8352 }
53+ window . _paq . push ( [ 'setCustomUrl' , window . location . href ] ) ;
54+ window . _paq . push ( [ 'setDocumentTitle' , document . title ] ) ;
55+ window . _paq . push ( [ 'trackPageView' ] ) ;
8456}
0 commit comments