Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/components/Matomo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { usePathname } from "next/navigation"
import { init, push } from "@socialgouv/matomo-next"

import { IS_PREVIEW_DEPLOY } from "@/lib/utils/env"
import { normalizePathForMatomo } from "@/lib/utils/matomo"

export default function Matomo() {
const pathname = usePathname()
Expand Down Expand Up @@ -41,11 +40,8 @@ export default function Matomo() {
return setPreviousPath(pathname)
}

const normalizedPreviousPath = normalizePathForMatomo(previousPath)
const normalizedPathname = normalizePathForMatomo(pathname)

push(["setReferrerUrl", normalizedPreviousPath])
push(["setCustomUrl", normalizedPathname])
push(["setReferrerUrl", previousPath])
push(["setCustomUrl", pathname])
push(["deleteCustomVariables", "page"])
setPreviousPath(pathname)
// In order to ensure that the page title had been updated,
Expand Down
24 changes: 1 addition & 23 deletions src/lib/utils/matomo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,10 @@ import { push } from "@socialgouv/matomo-next"

import type { MatomoEventOptions } from "@/lib/types"

import { DEFAULT_LOCALE, LOCALES_CODES } from "@/lib/constants"

import { IS_PROD } from "./env"

export const MATOMO_LS_KEY = "ethereum-org.matomo-opt-out"

/**
* Normalizes paths to ensure consistent Matomo tracking.
* With localePrefix: "as-needed", English paths don't have /en prefix,
* but we want to track them as /en paths for analytics consistency.
*/
export const normalizePathForMatomo = (pathname: string): string => {
const hasLocalePrefix = LOCALES_CODES.some((locale) =>
pathname.startsWith(`/${locale}/`)
)

if (hasLocalePrefix) {
return pathname
}

// For paths without locale prefix (English content), add /en prefix
return `/${DEFAULT_LOCALE}${pathname}`
}

export const trackCustomEvent = ({
eventCategory,
eventAction,
Expand All @@ -43,9 +23,7 @@ export const trackCustomEvent = ({

// Set custom URL removing any query params or hash fragments
if (window) {
const normalizedPathname = normalizePathForMatomo(window.location.pathname)
const normalizedUrl = window.location.origin + normalizedPathname
push([`setCustomUrl`, normalizedUrl])
push([`setCustomUrl`, window.location.href.split(/[?#]/)[0]])
}
push([`trackEvent`, eventCategory, eventAction, eventName, eventValue])
}