React, Next.js and TanStack

No framework package is needed. Put the snippet in your HTML head once, and datacub follows every route change on its own.

The SDK listens to the browser's History API, so React Router, TanStack Router, Next.js, Vue Router, SvelteKit and plain sites all report one page view per route change without extra code.

Where the snippet goes

Use the snippet from install the web SDK (or from Settings, Integrations, with your key filled in) and put it in the page's <head>:

FrameworkWhere
Vite (React, Vue), TanStack Router, plain sitesindex.html, inside <head>
Next.js App Routerapp/layout.tsx: a <script> in <head> with the snippet as dangerouslySetInnerHTML
TanStack Startthe root route's head scripts
SvelteKitsrc/app.html, inside <head>
index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <script>
      (function (w, d, src) {
        var q = (w.datacub = w.datacub || [])
        ;['init', 'track', 'identify', 'reset'].forEach(function (n) {
          q[n] = function () { q.push([n].concat([].slice.call(arguments))) }
        })
        var s = d.createElement('script'); s.async = true; s.src = src; d.head.appendChild(s)
      })(window, document, 'https://ingest.datacub.example/v1/sdk/datacub-0.4.0.min.js')
      datacub.init('pk_live_YOUR_PROJECT_KEY')
    </script>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

Then use the global datacub in client code. For TypeScript, save the type definitions in your project.

Identify after login

Call identify where your app learns who is signed in, and reset on logout. See identifying users.

src/use-analytics-user.ts
import { useEffect } from 'react'

export function useAnalyticsUser(user: { id: string } | null) {
  useEffect(() => {
    if (user) datacub.identify(user.id)
  }, [user])
}

Hash routers

Routers that keep the route in the fragment (/#/orders/12) need hashRouting:

datacub.init('pk_live_YOUR_PROJECT_KEY', { hashRouting: true })