Skip to content
  • There are no suggestions because the search field is empty.

Enhancing Your React Application with Convert A/B Testing Using @convertcom/tracking-react

How to install Convert Experiences on a React or Next.js site, with the tracking code or the experimental tracking-react component

 THIS ARTICLE WILL HELP YOU:


How Convert Works With React and Next.js

In Convert Experiences, the recommended way to run web experiments on a React or Next.js site is to install the tracking code in the <head> of your pages. The tracking script has SPA optimizations built in, and they're on by default in every Web project. When a visitor moves to another route, the script checks your experiences against the new URL and applies the ones that match. You don't need extra Convert code in your components. See SPA Optimizations.

@convertcom/tracking-react is an npm package with a React component, ConvertTrackingScript, that loads the same tracking script from inside your app. Convert labels it experimental, and the tracking code in the <head> stays the recommended install.

Both run Web projects, where Convert applies variations in the visitor's browser. To decide variations on your server, see Test on your server or in a native mobile app.

📒Note: If your project shows Use Legacy Script under Configuration > Environments & Tracking Code, it still runs the legacy tracking script, which is deprecated. Follow Migrate from the Legacy Tracking Script to the New Tracking Script (Step-by-Step) first.

Install the Tracking Code in the Page Head (Recommended)

  1. Open your project and click the Configuration tab.
  2. In the left menu, click Environments & Tracking Code.
  3. On the row of the environment you want, for example your production environment, open the row's menu and click Install Code Snippet.

    Row menu of the Production environment under Environments & Tracking Code, with Install Code Snippet, Edit this Environment and Delete this Environment

  4. The Code Snippet window opens on the Basic snippet tab. Copy the code.

    The Code Snippet window on the Basic snippet tab, with the tracking code to add in the head section of your pages

  5. Paste it into the <head> of the HTML file that loads your app, just after </title>:
    • Vite: index.html in the project root.
    • Create React App: public/index.html.
  6. Deploy your app.

The window gives the same instruction: Add the following project code in the <head> section, just after </title>, on all your experiences and goal pages.

If your app is rendered on the server, for example with Next.js, add the code to the <head> of the HTML your server sends for every page.

⚠️ Important: Keep the tracking code synchronous: don't add async or defer to it, and don't load it later from your own code. From the <head>, the script covers the page as soon as it runs and shows it once your experiences are applied, so visitors don't see the original page first. If the script loads later, visitors can see the original page for a moment before the variation. This is called flicker.

For more on where the code goes and the difference between the basic and advanced snippet, see How to Install the Main Tag / Convert Tracking Code / JavaScript.

Add the @convertcom/tracking-react Component

⚠️ Important: Convert labels @convertcom/tracking-react experimental. It may cause flicker if it isn't used correctly. Convert strongly recommends installing the tracking code in the <head> of your pages instead, as described in Install the tracking code in the page head.

The component needs React 16.8 or later.

  1. Install the package:
    npm install @convertcom/tracking-react

    Or, with Yarn:

    yarn add @convertcom/tracking-react
  2. Find your account ID and project ID. They're the two numbers in the file name at the end of the script address in your tracking code. In 1234567-7654321.js, 1234567 is the account ID and 7654321 is the project ID. To see your tracking code, follow steps 1 to 4 of Install the tracking code in the page head.
  3. Render the component once, at the start of your root component or layout, so it stays on the page for the whole visit. In a React app built with Vite or Create React App, that's usually App:
    import ConvertTrackingScript from '@convertcom/tracking-react';

    export default function App() {
    return (
    <>
    <ConvertTrackingScript accountId="1234567" projectId="7654321" />
    {/* the rest of your app */}
    </>
    );
    }

For Next.js, see Use the component in Next.js.

Use the Component in Next.js

In the Next.js App Router, layouts and pages are Server Components by default. ConvertTrackingScript runs in the browser, so it has to be rendered from a Client Component: a file that starts with the 'use client' directive. The package doesn't include that directive, so wrap the component in a small file of your own.

  1. Create app/convert-tracking.tsx:
    'use client';

    import ConvertTrackingScript from '@convertcom/tracking-react';

    export default function ConvertTracking() {
    return <ConvertTrackingScript accountId="1234567" projectId="7654321" />;
    }
  2. Render it at the start of <body> in your root layout, app/layout.tsx:
    import ConvertTracking from './convert-tracking';

    export default function RootLayout({ children }: { children: React.ReactNode }) {
    return (
    <html lang="en">
    <body>
    <ConvertTracking />
    {children}
    </body>
    </html>
    );
    }

Keep 'use client' in the wrapper file only. Your root layout can stay a Server Component.

With the Pages Router, you don't need 'use client'. Render the component in pages/_app.tsx, which Next.js uses to initialize every page:

import type { AppProps } from 'next/app';
import ConvertTrackingScript from '@convertcom/tracking-react';

export default function App({ Component, pageProps }: AppProps) {
return (
<>
<ConvertTrackingScript accountId="1234567" projectId="7654321" />
<Component {...pageProps} />
</>
);
}

Set the Component's Props

PropRequiredDefaultWhat it does
accountIdYesNoneYour Convert account ID, as text.
projectIdYesNoneYour Convert project ID, as text.
timeoutNo2500The longest time, in milliseconds, that the page stays covered while the script loads. Write it in braces, for example timeout={1500}.
selectorNobodyThe element to cover while the script loads, as a CSS selector, for example #layout. Use it to cover only part of the page.
backgroundNowhiteWhat covers that element while the script loads. It takes any CSS background, such as a color or an image.

For example:

<ConvertTrackingScript
accountId="1234567"
projectId="7654321"
timeout={1500}
selector="#layout"
background="#0a0a0a"
/>

What the Component Does and Doesn't Do

When the component first renders, it covers the page, or the element you set in selector, and loads the Convert tracking script. The page shows once Convert has applied your experiences, or when the timeout runs out, whichever comes first.

  • It works in web pages only. It loads the script into the page in the visitor's browser, so it doesn't work in React Native apps, which have no web page to load it into.
  • Variations are applied in the browser. With server rendering, as in Next.js, your server sends the original page, and Convert changes it in the visitor's browser.
  • It loads the script after your app starts, which is later than the tracking code in the <head>. If the script hasn't applied your experiences when the timeout runs out, the page shows anyway, and visitors can see the original page before the variation.
  • It loads your project's tracking script without an environment. Without an environment, every active experience can run, whatever environment it's assigned to. If you use environments, for example to keep experiences you're still testing off your live site, install that environment's code snippet in the <head> instead. See The Environments Feature.
  • It always loads the script from Convert's own domain. It doesn't use a custom domain (CNAME) you set up for the tracking script. If you use one, install the code snippet, which uses your custom domain. See Custom Domains (CNAME) for the Convert Tracking Script.
  • Render it once, in a component that stays on the page, such as your root layout. When the component unmounts, it removes the script tag it added. If it mounts again, it adds the script again.
  • Use either the tracking code in the <head> or the component, not both.

Test on Your Server or in a Native Mobile App

The tracking script and the component run Web projects in the browser. To decide which variation a visitor gets in your own code, for example in Next.js server code or a Node.js backend, use a Full Stack project with Convert's JavaScript SDK. The SDK reference is in the Convert JavaScript SDK documentation.

Full Stack projects also run in native iOS and Android apps. See iOS Quickstart and Android Quickstart.

A Full Stack project is separate from your Web project, with its own experiences and reports. See Which Convert Product Should I Use? Web Testing Tracking Script vs JavaScript SDK and Full Stack Experiments on Convert.

Check That Convert Is Running

After you deploy, open your site and check that the tracking script loads and your experiences run. See Check if Convert Experiences Is Installed Correctly and Tracking Script Installation Verification.

If a variation doesn't show after the visitor moves to another route, or stays on the page after they move on, see Single Page Application (SPA) Troubleshooting Guide.