Vue.js

PostHog makes it easy to get data about usage of your Vue.js app. Integrating PostHog into your app enables analytics about user behavior, custom events capture, session replays, feature flags, and more.

This guide walks you through integrating PostHog into your app for both Vue 2 and Vue 3. We'll use the JavaScript Web SDK for this.

For integrating PostHog into a Nuxt.js app, see our Nuxt guide.

Prerequisites

To follow this guide along, you need:

  1. A PostHog account
  2. A running Vue.js app

Setting up PostHog

Start by installing posthog-js using your package manager:

npm install --save posthog-js

Next, depending on your Vue version, we recommend initializing PostHog using the composition API or as a plugin.

We use the Composition API as it provides better accessibility, maintainability, and type safety.

PostHog initializes as a singleton, so you can initialize it in your main.ts file before you mount your app. This ensures PostHog is initialized before any other code runs.

src/main.ts
// src/main.ts
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import posthog from "posthog-js";
const app = createApp(App);
posthog.init(import.meta.env.VITE_POSTHOG_KEY || '<ph_project_api_key>', {
api_host: import.meta.env.VITE_POSTHOG_HOST || 'https://us.i.posthog.com',
defaults: '2025-11-30',
});
app.use(createPinia())
app.use(router)
app.config.errorHandler = (err, instance, info) => {
posthog.captureException(err)
}
app.mount('#app')

Then, you can access PostHog throughout your app just by importing it from posthog-js.

TypeScript
// src/App.vue
<script setup>
import posthog from 'posthog-js'
const handleClick = () => {
posthog.capture('button_clicked')
}
</script>

Once done, PostHog will begin autocapturing events and pageviews (if enabled) and is ready to use throughout your app.

Capturing custom events, using feature flags, and more

Once you have PostHog initialized, there is a lot more you can do with it beyond autocapture, pageviews, and pageleaves. You can find the full details in our JavaScript SDK docs, but we'll cover a few examples here.

To capture custom events, evaluate feature flags, and use any of the other PostHog features, you can use the posthog object returned from the usePostHog composable like this:

JavaScript
// src/App.vue
<script setup>
import { RouterView } from 'vue-router'
import { usePostHog } from './composables/usePostHog'
const { posthog } = usePostHog()
const handleClick = () => {
posthog.capture('button_clicked', { location: 'homepage' })
}
</script>
<template>
<div>
<button @click="handleClick">Click me!</button>
</div>
<RouterView />
</template>

Feature flags with reactive updates

When using feature flags on pages that users navigate to directly, the flags may not be loaded when the component first renders. To ensure your UI updates reactively when flags load, create a composable that returns a reactive ref:

JavaScript
// src/composables/usePostHogFeatureFlag.ts
import { ref, type Ref } from 'vue'
import { usePostHog } from './usePostHog'
export function usePostHogFeatureFlag(
feature: string,
): Ref<string | boolean | undefined> {
const { posthog } = usePostHog()
const flag = ref(posthog.getFeatureFlag(feature))
posthog.onFeatureFlags(() => {
flag.value = posthog.getFeatureFlag(feature)
})
return flag
}

Then use it in your components:

JavaScript
// src/App.vue
<script setup>
import { RouterView } from 'vue-router'
import { usePostHog } from './composables/usePostHog'
import { usePostHogFeatureFlag } from './composables/usePostHogFeatureFlag'
const { posthog } = usePostHog()
const isFeatureEnabled = usePostHogFeatureFlag('test-flag')
const handleClick = () => {
posthog.capture('button_clicked', { location: 'homepage' })
}
</script>
<template>
<div>
<button @click="handleClick">Click me!</button>
<p>Is feature flag enabled? {{ isFeatureEnabled ? 'Yes' : 'No' }}</p>
</div>
<RouterView />
</template>

This ensures your component will automatically update when feature flags load, even if the page is accessed directly.

Set up a reverse proxy (recommended)

We recommend setting up a reverse proxy, so that events are less likely to be intercepted by tracking blockers.

We have our own managed reverse proxy service included in the platform packages, which routes through our infrastructure and makes setting up your proxy easy.

If you don't want to use our managed service then there are several other options for creating a reverse proxy, including using Cloudflare, AWS Cloudfront, and Vercel.

Grouping products in one project (recommended)

If you have multiple customer-facing products (e.g. a marketing website + mobile app + web app), it's best to install PostHog on them all and group them in one project.

This makes it possible to track users across their entire journey (e.g. from visiting your marketing website to signing up for your product), or how they use your product across multiple platforms.

Add IPs to Firewall/WAF allowlists (recommended)

For certain features like heatmaps, your Web Application Firewall (WAF) may be blocking PostHog’s requests to your site. Add these IP addresses to your WAF allowlist or rules to let PostHog access your site.

EU: 3.75.65.221, 18.197.246.42, 3.120.223.253

US: 44.205.89.55, 52.4.194.122, 44.208.188.173

These are public, stable IPs used by PostHog services (e.g., Celery tasks for snapshots).

Next steps

For any technical questions for how to integrate specific PostHog features into Vue (such as analytics, feature flags, A/B testing, or surveys), have a look at our JavaScript Web SDK docs.

Alternatively, the following tutorials can help you get started:

Community questions

Was this page useful?

Questions about this page? or post a community question.