Documentation menuJavaScript frameworks: Nuxt

JavaScript frameworks

Add Loogika to Nuxt

Nuxt builds the head of every page from its config file. In nuxt.config.ts, the array app.head.script takes the Loogika script, and Nuxt then loads it on every route. Route changes in the browser are counted as pageviews without extra code.

Last updated .

What do you need before you start?

Find your site ID in Site Settings in Loogika, under the Tracking Script tab. The tab shows the full script tag, and the address in its src attribute is the part you need here:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

The site ID is a 12-character string. Replace YOUR_SITE_ID with it in the code on this page.

How do you add Loogika in nuxt.config.ts?

Open nuxt.config.ts and add an entry with the script address to the app.head.script array. The defer key makes the browser run the script after it has read the page.

export default defineNuxtConfig({
  app: {
    head: {
      script: [
        {
          src: "https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID",
          defer: true,
        },
      ],
    },
  },
});

How do you add Loogika to Nuxt 2?

Nuxt 2 uses the file nuxt.config.js, where the array is called head.script and sits at the top level. The entry itself is the same, with the src and defer keys shown above.

Does Loogika track Nuxt route changes?

Yes. Nuxt changes routes in the browser without a full page load, and Loogika records each change as a pageview. The script waits 500 milliseconds after a route change before it sends the pageview.

Route tracking depends on the SPA Navigation setting in Site Settings, which is on by default. With the setting turned off, write a client plugin with a router.afterEach hook that calls window.loogika.pageview().

A manual router.afterEach hook must skip the first navigation, because the script has already counted the landing page. On that first navigation the from argument equals START_LOCATION, which you import from vue-router.

How do you track custom events in Nuxt?

Call window.loogika.event() from a component with an event name and optional properties. Nuxt components also run on the server, so check import.meta.client before you touch window.

<template>
  <button @click="handleAction">Track action</button>
</template>

<script setup>
function handleAction() {
  if (import.meta.client && window.loogika) {
    window.loogika.event("nuxt_interaction", { detail: "User clicked the action button" });
  }
}
</script>

On Nuxt 2, use process.client in place of import.meta.client. The custom events guide explains event names, properties and their limits.

How do you check that Loogika is installed?

Load your app in a fresh browser tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds.

If nothing arrives, open the page source and look for script.js?siteId=. The browser Network tab should show script.js with status 200 and requests to /api/track. The script troubleshooting page covers ad blockers and wrong site IDs.