Report experiment exposures
Send experiment views through a shared analytics pipeline
Use @octopus-energy/flags/reporting to report exposures after a visitor sees
an experiment variant. See the reporting API reference
for the export catalog.
Install React when needed
The reporting entry point has a React peer dependency.
npm install @octopus-energy/flags reactAdd the reporting provider
Wrap the application once. Its emit callback sends a confirmed exposure to
your analytics destination.
"use client";
import { ExperimentsProvider } from "@octopus-energy/flags/reporting";
import type { ReactNode } from "react";
export function AnalyticsProvider({ children }: { children: ReactNode }) {
return (
<ExperimentsProvider
emit={({ experimentId, variantId }) => {
window.dataLayer?.push({
event: "experiment_viewed",
experimentId,
variantId,
});
}}
>
{children}
</ExperimentsProvider>
);
}Mount the provider at the app root when deduplication should span client-side navigations.
Report a client-side exposure
Construct the exposure explicitly and pass it to useExperimentExposure.
"use client";
import { type ExperimentExposure, useExperimentExposure } from "@octopus-energy/flags/reporting";
export function ReportExposures({ exposures }: { exposures: readonly ExperimentExposure[] }) {
useExperimentExposure(exposures);
return null;
}Report Server Component exposures
Evaluate flags on the server, deliberately map selected values to exposures,
then render ExperimentExposureReporter as the client boundary.
import { ExperimentExposureReporter } from "@octopus-energy/flags/reporting";
import { heroFlag } from "./flags";
import { Hero } from "./hero";
export default async function Page() {
const hero = await heroFlag();
return (
<>
<Hero variant={hero} />
<ExperimentExposureReporter exposures={[{ experimentId: heroFlag.key, variantId: hero }]} />
</>
);
}