Next-experiments is an attempt to add A/B testing infrastructure to the Next.js
⚠️ For the time being, you must use our next.js fork (link), which adds permuteStaticPaths support
-
Install library
yarn add next-experiments
-
Install fork of
next.js
that supports A/B tests.- In your
package.json
file replace"next": "<your_version_of_next>"
with"next": "https://registry.npmjs.org/@sheertex/next/-/next-9.4.5-canary.14.tgz"
- Remove
node_modules
folder - Run
yarn install
- In your
-
Change your
next.config.js
to addExperimentExtractorPlugin
and fix forfs
modulesconst { default: ExperimentExtractorPlugin } = require("next-experiments/dist/experimentExtractor"); let config = { distDir: "./dist", target: "serverless", webpack(config, { dev, isServer }) { if (!dev && isServer) { config.plugins.push(new ExperimentExtractorPlugin()); } // Fixes npm packages that depend on `fs-extra` module if (!isServer) { config.node = { fs: "empty", }; } return config; }, };
-
Once you implement the
permuteStaticPaths
andgetStaticProps
hooks,next-experiments
will be able to statically compile separate files for every possible + combination on your page:
import { Experiment, Variant, withPermutationContext } from "next-experiments";
export { permuteStaticPaths, getStaticProps } from "next-experiments";
export default withPermutationContext(() => {
return (
<>
<h1>Welcome to A/B Testing for next.js</h1>
<Experiment name="text-experiment" defaultVariantName="a">
<Variant name="a">Variant A</Variant>
<Variant name="b">Variant B</Variant>
</Experiment>
</>
);
});
This works for both static and dynamic page paths. When you next statically compile the site, variants will be rendered to your out directory:
next build
next export
Check out samples folder to get a basic concepts of next-experiments
You can send A/B experiments events to your favorite analytics tool by subscribing to the EMIT_PLAY and EMIT_WIN hooks.
Place the code listed below somewhere in the _app.js file of your next.js project.
import { emitter, EXPERIMENT_PLAYED, EXPERIMENT_WON } from "next-experiments";
if (typeof window !== "undefined") {
emitter.on(EXPERIMENT_PLAYED, ({ experimentName, variantName }) => {
console.log(
`Playing "${variantName}" variant of "${experimentName}" experiment`
);
});
emitter.on(EXPERIMENT_WON, ({ experimentName, variantName }) => {
console.log(
`"${variantName}" variant is won in "${experimentName}" experiment`
);
});
}
What is play event?
Play event is emitted when component with the Experiment did mount. It is useful when you want to understand what variant of experiment your user actually saw. You can delay triggering of play event by passing triggerPlay
prop to the Experiment component.
<Experiment
name="experiment"
defaultVariantName="a"
triggerPlay={() => {
console.log("Play of this experiment will be delayed for 3 seconds");
return new Promise((resolve) =>
setTimeout(() => {
console.log("Ding-dong! We can call play() for this experiment");
resolve();
}, 3000)
);
}}
>
...
</Experiment>
What is win event?
Win event is passed to the Variant component children. It's useful when you want to track what variant of experiment leads to user action.
<Experiment name="experiment" defaultVariantName="a">
<Variant name="a">
{(win) => <button onClick={() => win()}>Variant A</button>}
</Variant>
...
</Experiment>
For advanced usage examples refer to the sample project
- Run
yarn run test
- Bump version in
package.json
- Run
npm publish