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
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>
);
}Pages Router: pages/_document.js
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>
);
}- Inline scripts in
next/scriptneed anid— keepid="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
_documentonly. 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.
<!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:
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
- Single-page apps: How route changes are handled.
- init options: Settings you can pass to init.
- Consent and privacy: Wait for consent before tracking.
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.
