JavaScript frameworks
Add Loogika to Vue with Vite
When Vite builds a Vue app that runs in the browser, the project root holds a single index.html file. You paste the Loogika tag into the head of that file, and the script loads on every route of the app.
Last updated .
Which Vue projects does this guide cover?
This guide covers Vue apps that run fully in the browser. That includes a project created with npm create vue@latest or with npm create vite@latest -- --template vue. A Nuxt project is set up through its config file, so use the Nuxt guide for it.
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>
How do you add Loogika to index.html?
The file index.html sits in the project root. Open it and put the tag inside the head. The file then looks like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Vue App</title>
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
The tag also works just before the closing </body> tag. We recommend the head, because the browser starts to load the script sooner from there.
Does Loogika track Vue Router navigation?
Yes. Vue Router changes the address without a full page load, and Loogika records each of those changes 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, you record route changes yourself with an afterEach guard in your router setup, for example src/router/index.js:
import { createRouter, createWebHistory, START_LOCATION } from "vue-router";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [/* your routes */],
});
router.afterEach((to, from, failure) => {
if (from === START_LOCATION || failure) return;
if (window.loogika) {
window.loogika.pageview();
}
});
export default router;
The guard skips the first navigation, because the script has already counted the landing page. It also skips a navigation that failed. Test the default behaviour before you add this code.
How do you track custom events in Vue?
Any component or method can send an event through window.loogika.event(), with an event name and optional properties. The check for window.loogika keeps the button working when an ad blocker has stopped the script.
<template>
<button @click="trackAction">Track action</button>
</template>
<script setup>
function trackAction() {
if (window.loogika) {
window.loogika.event("custom_vue_action", { detail: "User performed an action" });
}
}
</script>
A plain click needs no JavaScript at all. Add data-loogika-event="custom_vue_action" to the button, as the custom events guide describes.
How do you check that Loogika is installed?
Load your app in a fresh browser tab and visit a few routes. 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.