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.js major versions 2
and 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:
- A PostHog account (either Cloud or self-hosted)
- A running Vue.js app
Setting up PostHog
Start by installing posthog-js
using your package manager:
yarn add posthog-js# ornpm 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.
Start by creating a composables
folder as well as a usePosthog.js
file to that folder. In usePosthog.js
, initialize PostHog as a composable with your project API key and host. You can find these in your project settings.
// src/composables/usePostHog.tsimport posthog from 'posthog-js'export function usePostHog() {posthog.init('<ph_project_api_key>', {api_host: 'https://us.i.posthog.com',defaults: '2025-05-24',})return { posthog }}
Next, in router/index.js
, import the usePostHog
composable and call it.
// src/router/index.jsimport { createRouter, createWebHistory } from 'vue-router'import HomeView from '../views/HomeView.vue'import { usePostHog } from '@/composables/usePostHog'const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path: '/',name: 'home',component: HomeView,},{path: '/about',name: 'about',component: () => import('../views/AboutView.vue'),},],})const { posthog } = usePostHog()export default router
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:
// 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' })}const isFeatureEnabled = posthog.isFeatureEnabled('test-flag')</script><template><div><button @click="handleClick">Click me!</button><p>Is feature flag enabled? {{ isFeatureEnabled ? 'Yes' : 'No' }}</p></div><RouterView /></template>
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 add-ons, 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.
Other setup options
With Vue 3, developers can use provide()
and inject()
to pipe global values into any component without prop drilling. And if you don't know what prop drilling is, good for you.
While this method is more declarative, as you need to inject PostHog into every component, it avoids “polluting” globals (like the plugins can do). Some engineers prefer this approach, while others include PostHog in globals since it doesn't need to be reactive and will be called throughout your application.
Step 1: Initialize Vue
Prior to mounting the app, you must:
- Import PostHog
- Initialize it
- Provide it to your app.
This must be done before you mount your app. If you provide PostHog after mounting it, PostHog will not be predictably available.
//app.jsimport posthog from "posthog-js";const app = createApp(App);posthog.init("<ph_project_api_key>", {api_host: "https://us.i.posthog.com",defaults: '2025-05-24',});app.provide("posthog", posthog);
Step 2: Inject into any Vue file
//component.vueexport default {data() {return {greeting: "How are you!",};},inject: ["posthog"], //grab the injection from app-wide providercreated() {console.log("Created", this.posthog); //posthog accessible!},};
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: