Generated banner for Integration Vue 3
Generated logo for Integration Vue 3

Integration Vue 3

PostHogOfficialCreated by PostHog

  • Skill
  • Tool integration
  • Data and AI
  • MIT
  • No issues found

From the author

PostHog integration for Vue 3 applications
— the description by PostHog

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a systematic, step-by-step procedure for adding PostHog analytics to a Vue 3 project. It covers initialization, user identification, custom event tracking, and error handling. By following the defined workflow, developers can ensure consistent implementation patterns, such as using environment variables for configuration and properly resetting session state upon user logout.

What it does

  • Guides the user through a multi-step PostHog integration process.
  • Provides patterns for initializing the PostHog SDK in Vue 3.
  • Defines methods for identifying users during login and signup.
  • Demonstrates how to capture custom events and track errors.
  • Ensures secure handling of project tokens via environment variables.
  • Includes instructions for resetting session state on logout.

When to use it

  • When you need to add product analytics to a new or existing Vue 3 application.
  • When you want to implement session replay and error tracking in Vue 3.
  • When you need to ensure your analytics implementation follows established best practices.
  • When you are setting up user identification flows within a Vue 3 authentication system.

Advantages

  • Provides a clear, ordered path for integration to reduce implementation errors.
  • Promotes consistent coding patterns across the application.
  • Emphasizes security by mandating the use of environment variables.
  • Includes specific guidance for handling critical events like authentication and errors.

Usage examples

“How do I start integrating PostHog into my Vue 3 project?”
The agent will guide you through the initial setup phase, including verifying dependencies and creating an event tracking plan.
“How should I handle user identification in my Vue 3 login flow?”
The agent will provide the pattern for calling posthog.identify() upon successful login and explain how to maintain session correlation.

Install

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/posthog/integration-vue-3/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip integration-vue-3.zip -d .agents/skills

Documentation by PostHog

PostHog integration for Vue 3

This skill helps you add PostHog analytics to Vue 3 applications.

Workflow

Follow these steps in order to complete the integration:

  1. basic-integration-1.0-begin.md - PostHog Setup - Begin ← Start here

  2. basic-integration-1.1-edit.md - PostHog Setup - Edit

  3. basic-integration-1.2-revise.md - PostHog Setup - Revise

  4. basic-integration-1.3-conclude.md - PostHog Setup - Conclusion

Reference files

  • references/EXAMPLE.md - Vue 3 example project code

  • references/vue-js.md - Vue.js - docs

  • references/identify-users.md - Identify users - docs

  • references/basic-integration-1.0-begin.md - PostHog setup - begin

  • references/basic-integration-1.1-edit.md - PostHog setup - edit

  • references/basic-integration-1.2-revise.md - PostHog setup - revise

  • references/basic-integration-1.3-conclude.md - PostHog setup - conclusion

The example project shows the target implementation pattern. Consult the documentation for API details.

Key principles

  • Environment variables: Always use environment variables for PostHog keys. Never hardcode them.

  • Minimal changes: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.

  • Match the example: Your implementation should follow the example project's patterns as closely as possible.

Framework guidelines

No specific framework guidelines.

Identifying users

Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using X-POSTHOG-DISTINCT-ID and X-POSTHOG-SESSION-ID headers to maintain correlation.

Error tracking

Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.

Files

references/vue-js.md

Vue.js - Docs

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:

PostHog AI

npm
npm install --save posthog-js
Yarn
yarn add posthog-js
pnpm
pnpm add posthog-js
Bun
bun add posthog-js

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

Vue 3: Composition API

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

PostHog AI

// 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_TOKEN || '<ph_project_token>', {
  api_host: import.meta.env.VITE_POSTHOG_HOST || 'https://us.i.posthog.com',
  defaults: '2026-01-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

PostHog AI

// 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.

Vue 2: Plugins

Start by creating a plugins folder and adding a posthog.js file to that folder. In posthog.js, initialize PostHog using the install method with your project token and host. You can find these in your project settings.

JavaScript

PostHog AI

// src/plugins/posthog.js
import posthog from 'posthog-js'
export default {
  install(Vue) {
    posthog.init('<ph_project_token>', {
      api_host: 'https://us.i.posthog.com',
      defaults: '2026-01-30'
    })
    Vue.prototype.$posthog = posthog
  }
}

Next, in main.js, import and use the plugin.

JavaScript

PostHog AI

// src/main.js
import Vue from 'vue'
import App from './App.vue'
import PosthogPlugin from './plugins/posthog'
Vue.config.productionTip = false
Vue.use(PosthogPlugin)
new Vue({
  render: h => h(App),
}).$mount('#app')

This makes PostHog available as this.$posthog in any Vue component.

Identifying users

Identifying users is required. Call posthog.identify('your-user-id') after login to link events to a known user. This is what connects frontend event captures, session replays, LLM traces, and error tracking to the same person — and lets backend events link back too.

See our guide on identifying users for how to set this up.

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.

Vue 3: Composition API

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

PostHog AI

// 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

PostHog AI

// 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

PostHog AI

// 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.

Vue 2: Plugins

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

JavaScript

PostHog AI

// src/components/AboutPage.vue
<template>
  <div class="about">
    <h1>About Page</h1>
    <button @click="handleClick">Test PostHog</button>
    <router-link to="/">Go to Home</router-link>
    <p>Feature enabled? {{ isFeatureEnabled ? 'Yes' : 'No' }}</p>
  </div>
</template>
<script>
export default {
  name: 'AboutPage',
  data() {
    return {
      isFeatureEnabled: false
    }
  },
  created() {
    this.isFeatureEnabled = this.$posthog.isFeatureEnabled('test-flag')
  },
  methods: {
    handleClick() {
      this.$posthog.capture('button_clicked')
    }
  }
}
</script>

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, which is free for all PostHog Cloud users, 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

Ask a question

Was this page useful?

HelpfulCould be better

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
PostHog
Publisher
PostHog (Official)
Source
PostHog/skills · skills/posthog/integration/skills/vue-3 · commit a5d30e7
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced
Also distributed in
PostHog/skills

AutoGineer is not affiliated with or endorsed by PostHog or PostHog.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download integration-vue-3.zip