# Nuxt quickstart

> Add Insights Orbit to a Nuxt website, send your own events, wait for consent and check that visits arrive.

Source: https://docs.bifrostorbit.com/insights/developers/quickstart-nuxt/

In about 10 minutes you add Insights Orbit to a Nuxt website for Northwilds. Page views count on every page, also when Nuxt changes pages without reloading, a button sends a **Brochure requested** event, and an optional banner asks visitors before anything is counted.

**Tested with:** Nuxt 4.6.4, Vue 3.5.45, Node.js 24, in `nuxi dev` and a production build.

## Before you start

You need:

- **A site in Insights Orbit** with your website's addresses under **Site settings**. Add `localhost` too while you build on your computer, or events from it answer `403`. See [Add your first site](https://docs.bifrostorbit.com/insights/everyone/add-a-site/).
- **The site key**, from the site's **Install** screen. It isn't a secret.
- **Node.js 20 or later.**

## 1. Create the project

```bash
npx create-nuxt@latest northwilds-site --template minimal --packageManager npm --gitInit false --no-modules
cd northwilds-site
mkdir -p app/pages app/components app/composables
```

Make the site key a public runtime setting in `nuxt.config.ts`:

```ts
// nuxt.config.ts
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },
  runtimeConfig: {
    public: { insightsSite: '' },
  },
})
```

```bash
# .env
NUXT_PUBLIC_INSIGHTS_SITE=SITE_KEY
```

`NUXT_PUBLIC_INSIGHTS_SITE` is read when the server starts, so the same build can run with a different key on staging and in production.

## 2. Add the script

Add the script with `useHead` in `app/app.vue`, so it is on every page. Replace the file:

```vue
<!-- app/app.vue -->
<script setup lang="ts">
const { insightsSite } = useRuntimeConfig().public

useHead({
  script: insightsSite
    ? [{ src: 'https://api.bifrostorbit.com/insights/v1/script.js', defer: true, 'data-site': insightsSite }]
    : [],
})
</script>

<template>
  <header>
    <NuxtLink to="/">Northwilds</NuxtLink> <NuxtLink to="/trips">Trips</NuxtLink>
  </header>
  <NuxtPage />
</template>
```

- **The script follows Nuxt's page changes** through `history.pushState`, so you don't need a page view call or a plugin.
- **Without a key**, no script is added.

Add a home page:

```vue
<!-- app/pages/index.vue -->
<template>
  <main>
    <h1>Northwilds</h1>
    <p>Guided trips in the far north.</p>
    <NuxtLink to="/trips">See the trips</NuxtLink>
  </main>
</template>
```

## 3. Send your own events

A composable calls the script, or queues the call when the script hasn't loaded yet. Nuxt imports functions from `app/composables` for you.

```ts
// app/composables/insights.ts
export type Details = Record<string, string | number | boolean>
export type Money = { revenue: number; currency: string }

type Insights = {
  track?: (name: string, details?: Details, money?: Money) => void
  consent?: (given?: boolean) => void
  q?: unknown[][]
}

declare global {
  interface Window {
    insights?: Insights
  }
}

export function track(name: string, details?: Details, money?: Money) {
  if (window.insights?.track) window.insights.track(name, details, money)
  else (window.insights ??= { q: [] }).q?.push(['track', name, details, money])
}

export function consent(given = true) {
  if (window.insights?.consent) window.insights.consent(given)
  else (window.insights ??= { q: [] }).q?.push(['consent', given])
}
```

Call `track` from the browser only, such as in a click handler or `onMounted`, never while the page renders on the server:

```vue
<!-- app/pages/trips.vue -->
<script setup lang="ts">
const sent = ref(false)

function requestBrochure() {
  track('Brochure requested', { trip: 'lofoten-ski-touring' })
  sent.value = true
}
</script>

<template>
  <main>
    <h1>Lofoten ski touring week</h1>
    <p>Seven days of ski touring above the fjords, from €2,450.</p>
    <button type="button" :disabled="sent" @click="requestBrochure">
      {{ sent ? 'On its way' : 'Send me the brochure' }}
    </button>
  </main>
</template>
```

- **Event names** are what goals look for, so use plain words. Details are text, numbers or true or false; never names or email addresses. [Send your own events](https://docs.bifrostorbit.com/insights/developers/send-events/) has the limits.
- **Revenue** goes in the third argument: `track('Trip booked', { trip: 'lofoten-ski-touring' }, { revenue: 2450, currency: 'EUR' })`.
- **To count it as success**, an Editor adds a goal under **Goals** that sends the event `Brochure requested`. See [Set up goals](https://docs.bifrostorbit.com/insights/everyone/goals/).

## 4. Wait for consent (optional)

Insights Orbit uses no cookies and keeps no personal data. Whether you still ask first is your decision with your adviser. See [What to write in your privacy notice](https://docs.bifrostorbit.com/insights/everyone/privacy-notice/). To ask first, add `'data-consent': 'wait'` to the script in `app/app.vue`:

```ts
[{ src: 'https://api.bifrostorbit.com/insights/v1/script.js', defer: true, 'data-site': insightsSite, 'data-consent': 'wait' }]
```

The script now counts nothing until the page calls `insights.consent()`. It doesn't remember the answer, so your banner keeps it and calls `consent()` on every page:

```vue
<!-- app/components/ConsentBanner.vue -->
<script setup lang="ts">
const KEY = 'northwilds-analytics'
const open = ref(false)

onMounted(() => {
  const saved = localStorage.getItem(KEY)
  if (saved === 'yes') consent()
  open.value = saved === null
})

function choose(answer: 'yes' | 'no') {
  localStorage.setItem(KEY, answer)
  open.value = false
  consent(answer === 'yes')
}
</script>

<template>
  <aside v-if="open" aria-label="Analytics">
    <p>May we count your visit? We use no cookies and keep no personal data.</p>
    <button type="button" @click="choose('yes')">Yes</button>
    <button type="button" @click="choose('no')">No thanks</button>
  </aside>
</template>
```

Put `<ConsentBanner />` after `<NuxtPage />` in `app/app.vue`.

- **Page views before the answer wait in memory**, up to 50, and are sent the moment the visitor says yes, so the page where they agreed still counts.
- **No thanks** drops what is waiting, and nothing is sent on later pages.
- **If you already have a consent tool**, call `consent()` from it instead of this banner.

## 5. Check that events arrive

Run `npm run dev`, open `http://localhost:3000`, go to **Trips** and select **Send me the brochure**. If you added the banner, select **Yes** first.

- **In the browser:** the **Network** tab shows `POST` requests to `collect` answering `202`, with your page views and the event. Events are sent two seconds after they happen, or at once when the page is hidden.
- **In Insights Orbit:** **Install** says **Visits are arriving**, **Right now** on the overview shows you on `/trips`, and **Events** lists **Brochure requested** within a few seconds.
- **From the API**, on the Growth plan, with a key that reads events:

```bash
curl "https://api.bifrostorbit.com/insights/v1/events?site=SITE_KEY&name=Brochure%20requested" \
  -H "Authorization: Bearer ins_live_…"
```

Nothing arrives? Browsers with Do Not Track or Global Privacy Control on, and automated browsers such as Playwright, send nothing on purpose. [Troubleshooting for developers](https://docs.bifrostorbit.com/insights/developers/troubleshooting/) lists every other cause.

## Next steps

- [The script](https://docs.bifrostorbit.com/insights/developers/the-script/): every option, opting out and labels.
- [Serve Insights Orbit from your own domain](https://docs.bifrostorbit.com/insights/developers/first-party-proxy/): so ad blockers don't hide visits.
- [Send events from a server](https://docs.bifrostorbit.com/insights/developers/server-api/): payments and bookings your server confirms.
- [Read raw events](https://docs.bifrostorbit.com/insights/developers/raw-event-api/): every event for your own data tools.
