Integrate Matomo's A/B Testing plugin with your Next.js application.
- Matomo instance with the A/B Testing plugin enabled
- Experiments configured in your Matomo dashboard
Create a configuration file for your experiments:
// config/ab-tests.ts
import type { ABTestDefinition } from "@socialgouv/matomo-next";
export const AB_TESTS: ABTestDefinition[] = [
{
name: "homepage-hero", // Must match Matomo experiment name
percentage: 100, // % of visitors included
variations: [{ name: "original" }, { name: "new-hero" }],
},
];A/B testing is a first-class config option — just pass abTests to trackAppRouter or trackPagesRouter:
// app/MatomoProvider.tsx
"use client";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { trackAppRouter } from "@socialgouv/matomo-next";
import { AB_TESTS } from "@/config/ab-tests";
export function MatomoProvider() {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
trackAppRouter({
url: process.env.NEXT_PUBLIC_MATOMO_URL!,
siteId: process.env.NEXT_PUBLIC_MATOMO_SITE_ID!,
pathname,
searchParams,
abTests: AB_TESTS,
});
}, [pathname, searchParams]);
return null;
}For the Pages Router:
// pages/_app.tsx
import { trackPagesRouter } from "@socialgouv/matomo-next";
import { AB_TESTS } from "@/config/ab-tests";
trackPagesRouter({
url: process.env.NEXT_PUBLIC_MATOMO_URL!,
siteId: process.env.NEXT_PUBLIC_MATOMO_SITE_ID!,
abTests: AB_TESTS,
});"use client";
import { useABTestVariant } from "@socialgouv/matomo-next";
export function HeroSection() {
const variant = useABTestVariant("homepage-hero");
if (variant === "new-hero") {
return <NewHeroSection />;
}
// Default / original / loading state
return <OriginalHeroSection />;
}Pass A/B test definitions directly to trackAppRouter or trackPagesRouter. The library automatically calls initABTesting() during initialization.
trackAppRouter({
url: "https://matomo.example.com",
siteId: "1",
pathname,
searchParams,
abTests: [
{
name: "homepage-hero",
percentage: 100,
variations: [{ name: "original" }, { name: "new-hero" }],
},
],
});For advanced use cases (e.g. conditional initialization), you can call initABTesting manually:
| Parameter | Type | Required | Description |
|---|---|---|---|
enabled |
boolean |
✅ | Master switch |
pathname |
string |
✅ | Current pathname |
tests |
ABTestDefinition[] |
✅ | Array of experiment definitions |
excludeUrlsPatterns |
RegExp[] |
❌ | URL patterns to skip |
| Field | Type | Required | Description |
|---|---|---|---|
name |
string |
✅ | Experiment name (must match Matomo dashboard) |
percentage |
number |
✅ | Percentage of visitors included (0–100) |
variations |
ABTestVariation[] |
✅ | Array of { name: string } |
startDateTime |
string |
❌ | ISO 8601 start date |
endDateTime |
string |
❌ | ISO 8601 end date |
trigger |
() => boolean |
❌ | Custom participation rule (default: all visitors) |
React hook that returns the activated variant name.
- Returns
nullwhile the test is loading or the visitor isn't participating - Returns the variant
stringonce Matomo activates a variation timeoutMsdefaults to5000ms
const variant = useABTestVariant("homepage-hero");
// variant: "original" | "new-hero" | nullReact 18+ hook using useSyncExternalStore for synchronous reads. Same API as useABTestVariant but without polling — it uses a small interval-based subscription internally.
Non-hook utility to read the A/B test state imperatively.
const state = getABTestState("homepage-hero");
// { abTest: "homepage-hero", variant: "new-hero", isReady: true }Alias for getABTestState — reads the full MatomoABTestState object.
{
name: "summer-sale",
percentage: 100,
variations: [{ name: "original" }, { name: "sale-banner" }],
startDateTime: "2025-06-01T00:00:00Z",
endDateTime: "2025-08-31T23:59:59Z",
}Only include logged-in users:
{
name: "premium-feature",
percentage: 100,
variations: [{ name: "original" }, { name: "new-feature" }],
trigger: () => {
return document.cookie.includes("logged_in=true");
},
}Pass excludeUrlsPatterns to the tracker — both page tracking and A/B testing respect the same patterns:
trackAppRouter({
url: "https://matomo.example.com",
siteId: "1",
pathname,
searchParams,
excludeUrlsPatterns: [/^\/admin/, /^\/api/],
abTests: AB_TESTS,
});Or when using initABTesting manually:
initABTesting({
enabled: true,
pathname,
excludeUrlsPatterns: [/^\/admin/, /^\/api/],
tests: AB_TESTS,
});You can run multiple experiments simultaneously — each test gets its own entry in the global store:
const heroVariant = useABTestVariant("homepage-hero");
const ctaVariant = useABTestVariant("cta-color");- When you pass
abTeststotrackAppRouter/trackPagesRouter, the library callsinitABTesting()automatically during initialization initABTesting()pushesAbTesting::createcommands to Matomo's_paqqueue- Matomo's A/B Testing plugin evaluates participation and assigns a variant
- The assigned variant is stored in
window.__MATOMO_AB_TEST__ useABTestVariant()polls this store until a variant is ready (or timeout)- Your component re-renders with the correct variant