Documentation menuDocumentation tools: VitePress

Documentation tools

Add Loogika to VitePress

You add Loogika to VitePress with the head option in .vitepress/config.ts. VitePress adds every tag in that option to all pages. A custom theme is the second method, for sites that must decide in the browser whether the script loads.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:

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

Both methods on this page use the address from the src attribute, not the tag itself.

How do you add Loogika in the VitePress config file?

Add the tag to head in .vitepress/config.ts. The file can also end in .js or .mts.

import { defineConfig } from "vitepress";

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

The empty string for defer is on purpose. VitePress prints it as a plain defer attribute without a value.

How do you track the production site only?

Make head depend on the build environment. With the check below, the tag is only written when NODE_ENV is production.

const isProd = process.env.NODE_ENV === "production";

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

The tag then appears in vitepress build and stays out of vitepress dev. Remove the check for a moment when you want to test tracking locally.

How do you add Loogika from a custom theme?

The theme file .vitepress/theme/index.ts can add the tag from its enhanceApp function. Create the file if your site does not have one yet. Choose this method when your own code must decide at runtime whether the script loads.

import { type Theme } from "vitepress";
import DefaultTheme from "vitepress/theme";

export default {
  extends: DefaultTheme,
  enhanceApp() {
    if (typeof window === "undefined") return;

    const el = document.createElement("script");
    el.src = "https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID";
    el.defer = true;
    document.head.appendChild(el);
  },
} satisfies Theme;

The window check matters because VitePress also runs the theme during the server-side build. No browser exists at that point, so the function returns early.

Does Loogika count page changes in VitePress?

Yes. VitePress changes pages in the browser through the History API, without a full page load. Loogika records those changes as pageviews when SPA Navigation is on in Site Settings. The setting is on for a new site.

How do you check that Loogika is installed?

Load your deployed docs 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=. A tag that is missing in vitepress dev usually means the production check is active. The script troubleshooting page lists other causes, such as ad blockers.