Skip to main content

Overview

init() sets up the SDK with your API key. It must be called once, at the very start of your app.
React / Next.js users: You don’t need to call init() directly. The useAnalytiq() hook calls it automatically. See the useAnalytiq reference
Any events tracked before init() is called are automatically queued and sent as soon as init() finishes.

Signature

init(apiKey: string, options?: InitOptions): void

Parameters

ParameterTypeRequiredDescription
apiKeystringYesYour project’s API key from the Settings page. Starts with pk_live_
options.autoTrackPageViewsbooleanOptionalIf true, automatically fires a page_view event on every URL/route change via the History API. Default: false
options.debugbooleanOptionalIf true, logs SDK actions to the browser console. Useful during development. Default: false
options.hoststringOptionalOverride the Analytiq backend URL. Leave blank — defaults to the production server automatically.

Example (Vue / Vanilla JS)

import { init } from 'analytiq'

init(import.meta.env.VITE_ANALYTIQ_KEY, {
  autoTrackPageViews: true,  // automatically tracks every page navigation
  debug: true                // remove in production
})

With environment variables

init(import.meta.env.VITE_ANALYTIQ_KEY, { autoTrackPageViews: true })

What it does

  1. Validates and stores your API key
  2. Enables automatic page view tracking if autoTrackPageViews: true
  3. Restores a previously identified userId from localStorage (persists across page refreshes)
  4. Retries any events that were queued offline in a previous session
  5. Logs [analytiq] Initialized successfully. to the browser console

Important rules

Call init() only once — at the root entry file of your app. Don’t call it inside components or on every page.
  • Call it before any track(), identify(), or batchTrack() calls
  • If called a second time, it is safely ignored (guarded against double-init)
  • React / Next.js users: Use useAnalytiq() instead — it handles init() for you

TypeScript

import { init } from 'analytiq'
import type { InitOptions } from 'analytiq'

const options: InitOptions = {
  autoTrackPageViews: true,
  debug: process.env.NODE_ENV === 'development',
  host: 'https://your-custom-backend.com' // only needed if self-hosting
}

init(import.meta.env.VITE_ANALYTIQ_KEY, options)