Skip to content
metryo

03Install guide

Add Metryo to a React app

Add the snippet to index.html and you are done. Navigation through React Router or any router built on the History API is picked up without a line of code.

  1. 01

    Create the website in your dashboard

    Choose Add website and enter its name and domain, without https://. You get a site ID that looks like site_k3v9x2m4q1. To try it on your own machine first, register localhost:3000 as the domain.

  2. 02

    Add the snippet to index.html

    index.html
    <!-- Add before the closing </head> tag -->
    <script
      defer
      data-site="site_xxxxx"
      src="https://cdn.metryo.io/m.js">
    </script>

    Replace site_xxxxx with your own site ID — everything else is copied as it is.

    src/SignupButton.tsx
    // 1. Add the HTML snippet to index.html, before </head>.
    // 2. That is the whole install. Route changes made with
    //    history.pushState — React Router, TanStack Router,
    //    wouter — are counted automatically, so do not call
    //    metryo.pageview() from your router as well.
    
    // Tracking an action is one call from any component:
    export function SignupButton() {
      return (
        <button
          onClick={() => window.metryo?.event("Signup started")}
        >
          Start for free
        </button>
      );
    }
  3. 03

    Open your site

    Load any page of your site in a browser. The dashboard's install screen checks every few seconds and unlocks the moment the first pageview arrives.

Do not report route changes yourself

React Router, TanStack Router and wouter all navigate with history.pushState, and the script already records a pageview each time that is called. An effect that also calls metryo.pageview() on every location change counts each page twice — and uses your monthly allowance twice as fast.

Hash routers

If your app routes with the URL fragment — HashRouter, or addresses like /#/settings — add data-hash="on" to the tag. Each fragment change then counts as a pageview. See script options.

TypeScript

src/metryo.d.ts
// metryo.d.ts — so TypeScript knows about window.metryo
type MetryoProps = Record<string, string | number | boolean>;

declare global {
  interface Window {
    metryo?: {
      event(name: string, props?: MetryoProps): void;
      pageview(): void;
    };
  }
}

export {};

Pasted it but nothing arrives?

  • An extension is blocking it. Ad blockers and privacy extensions drop analytics requests. Check in a private window with extensions disabled.
  • It is running on a different domain. A snippet only accepts events from the domain its website was registered with, and that domain's subdomains, so nobody else can send data in your name. Add a separate website for any other domain.
  • You muted yourself earlier. Setting metryo_ignore in localStorage silences the script in that browser. Remove the key to start sending again.
  • The monthly allowance ran out. Once an account passes its plan's allowance, new events stop being recorded until the next month. The allowance is shared across all of an account's websites.

Next: send custom events, turn on click tracking, or see every option the tag accepts.