
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
matomo-tracker-for-react
Advanced tools
A minimal yet powerful React package that integrates Matomo analytics with any React router.
A minimal yet powerful React package that integrates Matomo analytics with any React router, including React Router, TanStack Router, and Next.js, enabling automatic page tracking and custom event tracking out of the box.
Written in TypeScript but designed to be fully compatible with JavaScript projects as well.
path propuseMatomo() hookMatomoProvidernpm install matomo-tracker-for-react
Version 2.0 introduces a breaking change to support any router (Next.js, TanStack Router, React Router, etc.).
The package no longer has a hard dependency on react-router-dom. Instead, you must pass the current route path to the path prop of <MatomoProvider>. If you do not provide the path prop, automatic page view tracking will be disabled.
MatomoProviderYou can use MatomoProvider with any router by passing the current path to the path prop.
import { MatomoProvider } from "matomo-tracker-for-react";
import { BrowserRouter, useLocation } from "react-router-dom";
const AppWithTracking = () => {
const location = useLocation();
const currentPath = location.pathname + location.search + location.hash;
return (
<MatomoProvider
urlBase="https://matomo.example.com"
siteId="1"
path={currentPath}
>
<App />
</MatomoProvider>
);
};
const Root = () => (
<BrowserRouter>
<AppWithTracking />
</BrowserRouter>
);
import { MatomoProvider } from "matomo-tracker-for-react";
import {
RouterProvider,
createRouter,
useRouterState,
} from "@tanstack/react-router";
const router = createRouter({ routeTree });
const AppWithTracking = () => {
const location = useRouterState({ select: (s) => s.location });
const currentPath = location.pathname + location.search + location.hash;
return (
<MatomoProvider
urlBase="https://matomo.example.com"
siteId="1"
path={currentPath}
>
<RouterProvider router={router} />
</MatomoProvider>
);
};
"use client";
import { MatomoProvider } from "matomo-tracker-for-react";
import { usePathname, useSearchParams } from "next/navigation";
import { Suspense } from "react";
const TrackingContent = ({ children }: { children: React.ReactNode }) => {
const pathname = usePathname();
const searchParams = useSearchParams();
// Construct the full path including search parameters
const currentPath = `${pathname}${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
return (
<MatomoProvider
urlBase="https://matomo.example.com"
siteId="1"
path={currentPath}
>
{children}
</MatomoProvider>
);
};
const AppWithTracking = ({ children }: { children: React.ReactNode }) => {
return (
<Suspense fallback={children}>
<TrackingContent>{children}</TrackingContent>
</Suspense>
);
};
import { MatomoProvider } from "matomo-tracker-for-react";
import { useRouter } from "next/router";
const AppWithTracking = ({ children }: { children: React.ReactNode }) => {
const router = useRouter();
const currentPath = router.asPath;
return (
<MatomoProvider
urlBase="https://matomo.example.com"
siteId="1"
path={currentPath}
>
{children}
</MatomoProvider>
);
};
The library will detect page changes automatically when the path prop changes.
import { useMatomo } from "matomo-tracker-for-react";
const MyComponent = () => {
const { trackEvent } = useMatomo();
const handleClick = () => {
trackEvent("Button", "Click", "My CTA Button");
};
return <button onClick={handleClick}>Click Me</button>;
};
<MatomoProvider> Props| Prop | Type | Required | Description |
|---|---|---|---|
children | ReactNode | ✅ | Your application components. |
urlBase | string | ✅ | Base URL of your Matomo instance (e.g., https://your-matomo-domain.com). |
siteId | string or number | ✅ | Your Matomo website ID. |
path | string | ❌ | The current path of the router. Used for automatic page view tracking. |
trackCookies? | boolean | ❌ | If false, disables cookies (disableCookies: true). Default: true. |
disabled? | boolean | ❌ | If true, disables all tracking. Default: false. |
useMatomo() HookReturns an object with:
trackEvent(category: string, action: string, name?: string, value?: number): Tracks a custom event.trackPageView(customTitle?: string): Tracks a page view. Useful for SPAs if automatic tracking needs fine-tuning or if you want to set a custom title.trackGoal(goalId: number | string, revenue?: number): Tracks a conversion for a specific goal.setUserId(userId: string): Sets or updates a User ID for the current visitor.trackLink(url: string, linkType: 'link' | 'download'): Tracks an outbound link click or a download.pushInstruction(instruction: any[]): Allows pushing any raw instruction to the Matomo _paq array for advanced use cases (e.g., pushInstruction(['setUserId', 'USER_ID_HERE'])).matomo.js script fails to loadIf you see an error in your browser console like "Laden fehlgeschlagen für das , even if you can access the matomo.js URL directly in your browser, consider these common causes:
CORS (Cross-Origin Resource Sharing):
http://localhost:3000) and your Matomo instance (e.g., https://matomo.example.com) are on different origins.Access-Control-Allow-Origin header, allowing requests from your React app's domain. For example, Access-Control-Allow-Origin: http://localhost:3000.Mixed Content:
https but matomo.js is requested via http.urlBase/srcUrl provided) use https.Content Security Policy (CSP):
script-src (e.g., script-src 'self' https://matomo.example.com;).Ad Blockers/Browser Extensions:
trackGoal)setUserId)trackLink)If you find this package helpful, consider supporting its development:
Inspired by:
@datapunt/matomo-tracker-react (now deprecated)MPL-2.0
FAQs
A minimal yet powerful React package that integrates Matomo analytics with any React router.
We found that matomo-tracker-for-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.