Install CROforce on React and Next.js

Next.js A/B testing with CROforce needs one snippet, loaded once for every route. Load the CROforce snippet once, in the HTML shell every route shares: next/script in your root layout or _document for Next.js, or index.html for a plain React app. Client-side route changes are tracked automatically.

On this pageNext.jsPlain React (Vite, Create React App)Page views and route changesSend your own eventsVerify the installationNext steps

Updated November 28, 2026

Not set
Find it on the install page of your account. It stays in this browser and is never sent anywhere.

To install CROforce in a React or Next.js app, put your snippet in the HTML that wraps every page — not inside a component. In Next.js, use next/script with strategy="beforeInteractive" in app/layout (App Router) or pages/_document (Pages Router). In a plain React app, paste it before </head> in index.html. You don't need any code for route changes: the script records a page view each time the URL changes.

Copy your own snippet first. In CROforce, click Install Entail CRO, choose React or NextJS and click Copy script. The examples below use YOUR_SITE_TOKEN — use the token from your copied snippet.

Next.js

Load the script with strategy="beforeInteractive" so it runs before the page becomes interactive. That matters for A/B tests: the earlier the script runs, the less chance visitors see the original version flash first. The two <link> tags start the download early.

App Router: app/layout.js

JSX
import Script from "next/script";

// The script part of your CROforce snippet, without the <script> tags.
const croforceSnippet = `!function(t,e){var o,n,p,r;e.__SV||(window.entailInsights=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src="https://cdn.entail-insights.com/js/entail.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="entailInsights",u.toString=function(t){var e="entailInsights";return"entailInsights"!==a&&(e+="."+a),t||(e+=" (stub)"),e},o="trackEvent capture opt_out_capturing has_opted_out_capturing opt_in_capturing reset".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.entailInsights||[]);
entailInsights.init('YOUR_SITE_TOKEN', { disable_decide: false });`;

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <head>
        <link rel="preload" href="https://cdn.entail-insights.com/js/entail.js" as="script" />
        <link rel="preconnect" href="https://widgets.entail.ai/" crossOrigin="" />
      </head>
      <body>
        {children}
        <Script id="croforce" strategy="beforeInteractive">
          {croforceSnippet}
        </Script>
      </body>
    </html>
  );
}
Paste your site token at the top of the page to fill it in.

Pages Router: pages/_document.js

JSX
import { Html, Head, Main, NextScript } from "next/document";
import Script from "next/script";

// The script part of your CROforce snippet, without the <script> tags.
const croforceSnippet = `!function(t,e){ …your snippet… }(document,window.entailInsights||[]);
entailInsights.init('YOUR_SITE_TOKEN', { disable_decide: false });`;

export default function Document() {
  return (
    <Html lang="en">
      <Head>
        <link rel="preload" href="https://cdn.entail-insights.com/js/entail.js" as="script" />
        <link rel="preconnect" href="https://widgets.entail.ai/" crossOrigin="" />
      </Head>
      <body>
        <Main />
        <NextScript />
        <Script id="croforce" strategy="beforeInteractive">
          {croforceSnippet}
        </Script>
      </body>
    </Html>
  );
}
Paste your site token at the top of the page to fill it in.
  • Inline scripts in next/script need an id — keep id="croforce".
  • With beforeInteractive, Next.js puts the script in the page's <head> itself, wherever you place the tag in the layout.
  • Keep it in the root layout or _document only. Adding it to individual pages loads it on those pages only.

Plain React (Vite, Create React App)

A single-page React app has one HTML file that every route uses. Paste the whole snippet there, just before </head>: index.html at the project root for Vite, or public/index.html for Create React App.

HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Your app</title>

  <!-- CROforce: paste your snippet here, just before </head> -->
  <link rel="preload" href="https://cdn.entail-insights.com/js/entail.js" as="script">
  <link rel="preconnect" href="https://widgets.entail.ai/" crossorigin>
  <script> /* …your snippet… */ </script>
</head>
<body>
  <div id="root"></div>
  <script type="module" src="/src/main.jsx"></script>
</body>
</html>

Page views and route changes

In a single-page app the browser doesn't load a new page when visitors navigate, so many tools miss those views. CROforce doesn't: the script watches the URL — history.pushState, replaceState, the back and forward buttons and hash changes — and when it changes, it records a page view and checks again which campaigns, popups and tests apply to the new URL. React Router, the Next.js router and similar routers need no extra code.

Details and edge cases: Single-page apps.

Send your own events

Once the snippet is in the page, window.entailInsights exists right away — even before the script finishes loading, calls are queued. Call it from an event handler, not during render:

JSX
export function SignupButton() {
  const handleClick = () => {
    window.entailInsights?.trackEvent("signup_click", { plan: "pro" });
  };
  return <button onClick={handleClick}>Start free trial</button>;
}

See Track custom events. With server-side rendering, only call it in the browser (in effects or event handlers), since window doesn't exist on the server.

Verify the installation

Deploy, open your site in a browser, then click Verify Installation in CROforce. If your app renders the snippet only in the browser, the HTML check can't see it — the visit you just made lets the check pass instead. See Check your installation.

Next steps

Frequently asked questions

Do I need to track page views on route changes myself?

No. The script notices client-side navigation (history pushState, replaceState, the back button and hash changes) and records a page view for each new URL.

Can I call init inside a React component?

Avoid it. Load the snippet once in the document shell. A component can re-render or mount twice in development, and only the first init counts anyway.

How do I call entailInsights from TypeScript?

Declare it on window, for example declare global { interface Window { entailInsights: any } }, then call window.entailInsights.trackEvent(...). See Track custom events.

Was this article helpful?

Try it on your own site

Add a popup, form or quiz to your site in a few minutes and see what it does for your conversions.