Vue web analytics installation
Start by installing posthog-js using your package manager:
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.
Next, in router/index.js, import the usePostHog composable and call it.
Once done, PostHog will begin autocapturing events and pageviews (if enabled) and is ready to use throughout your app.
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 API key and host. You can find these in your project settings.
Next, in main.js, import and use the plugin.
This makes PostHog available as this.$posthog in any Vue component.
Next steps
After installing PostHog and ensuring autocapture is enabled, head to your web analytics dashboard to see your data. And then check out our getting started guide.
PostHog tip: Web analytics works with anonymous events. This means if you are primarily using PostHog for web analytics, it can be significantly cheaper for you.