1
1
import { push } from "@socialgouv/matomo-next"
2
2
3
+ import { DEFAULT_LOCALE , LOCALES_CODES } from "@/lib/constants"
4
+
3
5
import { IS_PROD } from "./env"
4
6
5
7
export const MATOMO_LS_KEY = "ethereum-org.matomo-opt-out"
6
8
9
+ /**
10
+ * Normalizes paths to ensure consistent Matomo tracking.
11
+ * With localePrefix: "as-needed", English paths don't have /en prefix,
12
+ * but we want to track them as /en paths for analytics consistency.
13
+ */
14
+ export const normalizePathForMatomo = ( pathname : string ) : string => {
15
+ const hasLocalePrefix = LOCALES_CODES . some ( ( locale ) =>
16
+ pathname . startsWith ( `/${ locale } /` )
17
+ )
18
+
19
+ if ( hasLocalePrefix ) {
20
+ return pathname
21
+ }
22
+
23
+ // For paths without locale prefix (English content), add /en prefix
24
+ return `/${ DEFAULT_LOCALE } ${ pathname } `
25
+ }
26
+
7
27
export interface MatomoEventOptions {
8
28
eventCategory : string
9
29
eventAction : string
@@ -27,6 +47,10 @@ export const trackCustomEvent = ({
27
47
if ( isOptedOut ) return
28
48
29
49
// Set custom URL removing any query params or hash fragments
30
- window && push ( [ `setCustomUrl` , window . location . href . replace ( / [ ? # ] .* $ / , "" ) ] )
50
+ if ( window ) {
51
+ const normalizedPathname = normalizePathForMatomo ( window . location . pathname )
52
+ const normalizedUrl = window . location . origin + normalizedPathname
53
+ push ( [ `setCustomUrl` , normalizedUrl ] )
54
+ }
31
55
push ( [ `trackEvent` , eventCategory , eventAction , eventName , eventValue ] )
32
56
}
0 commit comments