Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Vue pageleave #9396

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions contents/docs/libraries/vue-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,51 @@ export default new Router({
});
```

## Capturing pageleaves

Similar to pageviews, you need to manually capture pageleaves in a Vue single-page app.

This starts by setting `capture_pageleave` to `false` in the PostHog initialization config to ensure you don’t double-capture pageleaves.

```js
// In the file where you initialize PostHog
posthog.init(
"<ph_project_api_key>",
{
api_host: "<ph_client_api_host>",
person_profiles: 'identified_only',
capture_pageview: false,
capture_pageleave: false
}
);
```

**Vue 3.x:**

You can then use the same `router.afterEach` hook to capture pageleaves. The difference is that you need to manually set the `$current_url` and `path` properties using the `from` object.

```js
import { createApp, nextTick } from 'vue'
import App from './App.vue'
import router from './router'
import posthogPlugin from '../plugins/posthog';


const app = createApp(App);
app.use(posthogPlugin)
.use(router)
.mount('#app');

router.afterEach((to, from, failure) => {
if (!failure) {
nextTick(() => {
app.config.globalProperties.$posthog.capture('$pageleave', { $current_url: window.location.host + from.fullPath, path: from.fullPath });
app.config.globalProperties.$posthog.capture('$pageview', { path: to.fullPath });
});
}
});
```

## Capturing events

Here’s a simple example of using PostHog to capture a barebones login button.
Expand Down
Loading