Step 1 — Install the SDK
Step 2 — Initialize in src/main.js (One-time setup)
Open src/main.js. This is the only file you need to change for the core setup. Add the init() call before you mount the app.
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { init, track } from 'analytiq'
// Initialize the SDK once before mounting the app
init(import.meta.env.VITE_ANALYTIQ_KEY)
// Optional: Track page views on every Vue Router navigation
router.afterEach((to) => {
track('page_view', { path: to.path })
})
const app = createApp(App)
app.use(router)
app.mount('#app')
Add your API key to .env:
VITE_ANALYTIQ_KEY=pk_live_your_actual_key_here
That’s the core setup! You can now track events from anywhere in your app.
What the SDK handles automatically
| Action | What happens |
|---|
| App initializes | init() runs once, anonymous tracking begins |
| User goes offline | Events saved to localStorage, retried on reconnect |
Step 3 — Identify users and reset on logout
Vue doesn’t use hooks, so you call identify() and reset() directly. The best place is your Pinia store or auth composable:
import { defineStore } from 'pinia'
import { identify, reset } from 'analytiq'
export const useAuthStore = defineStore('auth', {
state: () => ({ user: null }),
actions: {
async login(email, password) {
const res = await api.post('/auth/login', { email, password })
this.user = res.data.user
identify(this.user) // call immediately after login succeeds
},
logout() {
this.user = null
reset() // call immediately on logout
router.push('/login')
}
}
})
<script setup>
import { identify } from 'analytiq'
import { useRouter } from 'vue-router'
const router = useRouter()
async function handleLogin(email, password) {
const res = await api.post('/auth/login', { email, password })
identify(res.data.user) // call right after login
router.push('/dashboard')
}
</script>
<template>
<form @submit.prevent="handleLogin(email, password)">
<!-- your form fields -->
<button type="submit">Login</button>
</form>
</template>
Step 4 — Track Custom Events
For specific actions (button clicks, purchases, etc.), import track() in any component:
Step 5 — Verify it’s working
- Run:
npm run dev
- Open your browser, Press F12 and go to Console
- You should see:
[analytiq] Initialized successfully. {host: "...", userId: null}
[analytiq] Auto page view tracking enabled.
- Navigate between pages in your app
- Go to your Analytiq Dashboard → Events —
page_view events appear within 10 seconds.