ENES Start free trial

Install FastAnalytics on Next.js, React, Vue and other single-page apps

Load the script once. It listens to the browser history, so every client-side navigation counts as a page view without extra code.

FastAnalytics team · Updated · 4 min read

Next.js (App Router)

In app/layout.tsx:

import Script from 'next/script';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script src="https://e.fastanalytics.site/e.js" data-id="FA-XXXXXXXXXX" strategy="afterInteractive" />
      </body>
    </html>
  );
}

Next.js (Pages Router)

In pages/_app.tsx, render the same <Script /> component once next to <Component {...pageProps} />.

React, Vue, Svelte with Vite

Paste the script in index.html, inside <head>:

<script>window.hm=window.hm||function(){(window.hm.q=window.hm.q||[]).push(arguments)};</script>
<script defer src="https://e.fastanalytics.site/e.js" data-id="FA-XXXXXXXXXX"></script>

Nuxt, Astro, SvelteKit

  • Nuxt: add it to app.head.script in nuxt.config, with defer: true and 'data-id'.
  • Astro: add it to your base layout <head>.
  • SvelteKit: add it to src/app.html, inside <head>.

Load it only once. Do not add it inside a component that re-renders, or page views would be counted twice.

Query parameters

By default only the path is sent (/products/shoes), never the query string, and parts of the path that look like IDs are replaced with :id. If a query parameter really defines the page, list it in data-keep:

<script>window.hm=window.hm||function(){(window.hm.q=window.hm.q||[]).push(arguments)};</script>
<script defer src="https://e.fastanalytics.site/e.js" data-id="FA-XXXXXXXXXX" data-keep="category,page"></script>

FAQ

Do I need to call anything on route change?

No. The script detects pushState, replaceState and back/forward navigation by itself.

Keep reading