Skip to content
Install guide · Frameworks

Install Linqry on a React app

To add Linqry to a React single-page app, paste the two-line snippet into the `<head>` of your `index.html`. Page views, including every React Router navigation, are recorded automatically; you only write code for custom events.

Updated · 2 min read

Install in index.html

  1. Add your site to Linqry

    Start a free trial, add your production domain and copy the snippet from the Install page. It already contains your site ID.

  2. Paste it into index.html

    In a Vite project, index.html sits at the project root and is the app's entry point. Put the snippet inside <head>, before your app's own script.

    <!-- index.html -->
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <title>My app</title>
        <script>window.linqry=window.linqry||function(){(window.linqry.q=window.linqry.q||[]).push(arguments)}</script>
        <script defer data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" src="https://linqry.com/t.js"></script>
      </head>
      <body>
        <div id="root"></div>
        <script type="module" src="/src/main.tsx"></script>
      </body>
    </html>
  3. Build, deploy and check Realtime

    Deploy the build, open the live site and move between a few routes. Realtime in Linqry shows each route as a pageview in the live event stream within seconds.

Putting the tracker in index.html rather than in a component has two advantages: it loads in parallel with your bundle instead of after React mounts, and it can never be loaded twice by a component that re-renders or remounts in Strict Mode.

React Router and other client-side routers

createBrowserRouter, <BrowserRouter> and most other routers change the URL with the History API. The tracker hooks history.pushState and replaceState and listens for popstate, so each navigation is a page view, and the time on the previous route is sent as it is left. There's no useLocation effect to write.

If you use createHashRouter or <HashRouter>, your routes live in the URL hash (/#/settings). Add data-hash-routing to the tracker tag so hash changes count as page views:

<script defer data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" data-hash-routing src="https://linqry.com/t.js"></script>

Track custom events

Call window.linqry from handlers and effects. Thanks to the queue stub it's safe to call before the tracker has finished loading. With TypeScript, add a declaration once:

// linqry.d.ts (anywhere your tsconfig includes)
export {};

declare global {
  interface Window {
    linqry?: (
      event: string,
      props?: Record<string, string | number | boolean>,
      revenueCents?: number,
    ) => void;
  }
}
function SignupForm() {
  async function onSubmit(e: React.FormEvent) {
    e.preventDefault();
    await createAccount();
    window.linqry?.("signup-completed", { source: "pricing-page" });
  }
  return <form onSubmit={onSubmit}>{/* ... */}</form>;
}

For plain click tracking you can skip JavaScript and put attributes on the element: <button data-linqry-event="cta-clicked" data-linqry-event-location="hero">. See custom events and the custom event tracking guide for what to track and how to name it.

Different site IDs per environment

Most teams use one Linqry site for production and simply don't track staging. Events from localhost are already ignored, and staging hostnames are rejected unless you add them under Additional hosts in site Settings. If you want a separate staging site, Vite can substitute environment variables into index.html with %NAME% placeholders:

<!-- .env.production: VITE_LINQRY_SITE_ID=your-production-id -->
<script defer data-website-id="%VITE_LINQRY_SITE_ID%" data-domain="yourdomain.com" src="https://linqry.com/t.js"></script>

Exclude your own visits

Open your deployed app, open the browser console and run the command below. That browser stops sending events until you clear site data.

localStorage.setItem('linqry_ignore', 'true')

Troubleshooting

  • Page views missing after the first? Check whether your router uses the hash. If it does, add data-hash-routing.
  • Nothing at all? Confirm the deployed index.html contains the snippet (some hosts cache it) and that the site ID matches the Install page.
  • Server-rendered React? If a framework renders your HTML, follow its guide instead, such as Next.js.

Frequently asked questions

Should I load the tracker in a useEffect instead?

Only if you can't edit index.html. A static tag loads earlier and can't be added twice. If you do inject it from code, create the script element once, set data-website-id and data-domain with setAttribute, and append it to document.head.

Does it work if I don't use Vite?

Yes. The tracker doesn't depend on React or on your bundler. Put the snippet in whichever HTML file serves your app.

Can I track revenue from React?

You can pass a third argument in cents, but browser revenue is capped and can be forged by anyone reading your page. Record real payments from your server with the write key instead.

Sources

  1. Vite: Getting started (index.html as the entry point) (checked Nov 9, 2026)
  2. Vite: Env variables and modes (HTML constant replacement) (checked Nov 9, 2026)
  3. React Router: createHashRouter (checked Nov 9, 2026)

Start measuring your React site

No card required · 30-day trial · No cookies