PostHog makes it easy to get data about usage of your Nuxt.js app. Integrating PostHog into your app enables analytics about user behavior, custom events capture, session replays, feature flags, and more.
These docs are aimed at Nuxt.js users who run Nuxt in spa
or universal
mode. You can see a working example of the Nuxt v3.0 integration in our Nuxt.js demo app
Nuxt v3.0 and above
Setting up PostHog on the client side
- Install
posthog-js
using your package manager:
yarn add posthog-js# ornpm install --save posthog-js
- Add your PostHog API key and host to your
nuxt.config.js
file. You can find your project API key in the PostHog app under Project Settings > Project Variables.
export default defineNuxtConfig({runtimeConfig: {public: {posthogPublicKey: '<ph_project_api_key>',posthogHost: https://us.i.posthog.com}}})
- Create a new plugin by creating a new file
posthog.client.js
in your plugins directory.
import { defineNuxtPlugin } from '#app'import posthog from 'posthog-js'export default defineNuxtPlugin(nuxtApp => {const runtimeConfig = useRuntimeConfig();const posthogClient = posthog.init(runtimeConfig.public.posthogPublicKey, {api_host: runtimeConfig.public.posthogHost || https://us.i.posthog.com,capture_pageview: false, // we add manual pageview capturing belowloaded: (posthog) => {if (import.meta.env.MODE === 'development') posthog.debug();}})// Make sure that pageviews are captured with each route changeconst router = useRouter();router.afterEach((to) => {nextTick(() => {posthog.capture('$pageview', {current_url: to.fullPath});});});return {provide: {posthog: () => posthogClient}}})
PostHog can then be accessed throughout your Nuxt.js using the provider accessor, for example:
<script setup>const { $posthog } = useNuxtApp()if ($posthog) {const posthog = $posthog()posthog.capture('<event_name>')}</script>
See the JavaScript SDK docs for all usable functions, such as:
- Capture custom event capture, identify users, and more.
- Feature flags including variants and payloads.
Setting up PostHog on the server side
- Install
posthog-node
using your package manager:
yarn add posthog-node# ornpm install --save posthog-node
- Add your PostHog API key and host to your
nuxt.config.js
file. If you've already done this when adding PostHog to the client side, you can skip this step.
export default defineNuxtConfig({runtimeConfig: {public: {posthogPublicKey: '<ph_project_api_key>',posthogHost: https://us.i.posthog.com}}})
- Initialize the PostHog Node client where you'd like to use it on the server side. For example, in [`useAsyncData(https://nuxt.com/docs/api/composables/use-async-data)]`:
<!-- ...rest of code --><script setup>import { useAsyncData, useCookie, useRuntimeConfig } from 'nuxt/app';import { PostHog } from 'posthog-node';const { data: someData, error } = await useAsyncData('ctaText', async () => {const runtimeConfig = useRuntimeConfig();const posthog = new PostHog(runtimeConfig.public.posthogPublicKey,{ host: runtimeConfig.public.posthogHost });const cookies = useCookie(`ph_${runtimeConfig.public.posthogPublicKey}_posthog`);if (cookies && cookies.value) {try {const distinctId = cookies.value.distinct_id; // or you can use your user's email, for example.posthog.capture({distinctId: distinctId,event: 'user_did_something"',})await posthog.shutdown()} catch (error) {console.log(error);}}return "Some data";});</script>
Note: Make sure to always call
posthog.shutdown()
after capturing events from the server-side. PostHog queues events into larger batches, and this call forces all batched events to be flushed immediately.
See the Node SDK docs for all usable functions, such as:
- Capture custom event capture, identify users, and more.
- Feature flags including variants and payloads.
Nuxt v2.16 and below
We are going to implement PostHog as a Nuxt.js integration which gives us the possibility to inject the posthog object and make it available across our application.
The first thing you want to do is to install the posthog-js library in your project - so add it using your package manager:
yarn add posthog-js
or
npm install --save posthog-js
After that we want to create a app in plugins/posthog/index.js
import posthog from 'posthog-js'import Vue from 'vue'export default function({ app: { router } }, inject) {// Init PostHogposthog.init('<ph_project_api_key>', {api_host: https://us.i.posthog.com,capture_pageview: false,loaded: () => posthog.identify('unique_id') // If you can already identify your user})// Inject PostHog into the application and make it available via this.$posthog (or app.$posthog)inject('posthog', posthog)// Make sure that pageviews are captured with each route changerouter.afterEach(to => {Vue.nextTick(() => {/* Note: this might also be a good place to call posthog.register(...) in order to update your propertieson each page view*/posthog.capture('$pageview', {$current_url: to.fullPath})})})}
Finally, we need to activate it on the client side in our nuxt.config.js
plugins: [...{ src: './plugins/posthog', mode: 'client' }],
Usage
By using the example code above you can now use PostHog across your app with this.$posthog
or app.$posthog
- depending on the context.
Compare with the Nuxt.js docs on further details when to use app.$posthog
or this.$posthog
.
Let's say for example the user makes a purchase you could track an event like that:
<template><button @click="purchase">Buy</button></template><script>...methods: {purchase() {this.$posthog.capture('purchase')}}...</script>
Next steps
For any technical questions for how to integrate specific PostHog features into Nuxt (such as analytics, feature flags, A/B testing, surveys, etc.), have a look at our JavaScript Web and Node SDK docs.
Alternatively, the following tutorials can help you get started: