-
Notifications
You must be signed in to change notification settings - Fork 669
feat: document new nuxt module #13190
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
81de4a3
feat: add base docs
ablaszkiewicz 11e88d0
feat: wip
ablaszkiewicz 4eb6525
feat: wip
ablaszkiewicz 1dd78fd
feat: add links
ablaszkiewicz 8a2bb70
feat: add link
ablaszkiewicz 7ec118c
feat: wip
ablaszkiewicz 36b8990
feat: wip
ablaszkiewicz 789bbea
feat: wip
ablaszkiewicz 8ff3da9
fix: correct word
ablaszkiewicz d51e245
fix: adjust copyt
ablaszkiewicz cb0b7f0
feat: split versions
edwinyjlim 100b249
feat: split docs pages
ablaszkiewicz 076b538
feat: make 3.7 default page
ablaszkiewicz 916229d
fix: adjust copy
ablaszkiewicz cd57300
fix: fix comments
ablaszkiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| title: Nuxt error tracking installation (3.6 and below) | ||
| showStepsToc: true | ||
| --- | ||
|
|
||
| import { Steps, Step } from 'components/Docs/Steps' | ||
|
|
||
| import CLIUpload from "../_snippets/cli/upload.mdx" | ||
| import CLIAuthenticate from "../_snippets/cli/authenticate.mdx" | ||
| import CLIDownload from "../_snippets/cli/download.mdx" | ||
| import InstallNuxt from "../../integrate/_snippets/install-nuxt.mdx" | ||
| import StepVerifyErrorTracking from "./_snippets/step-verify-error-tracking.mdx" | ||
| import StepUploadSourceMaps from "./_snippets/step-upload-source-maps.tsx" | ||
|
|
||
| For older versions of Nuxt, you'll need to manually set up error tracking and sourcemap uploads. | ||
ablaszkiewicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <Steps> | ||
| <Step title="Installing PostHog SDK" subtitle="Your goal in this step: Install the PostHog SDK." badge="required"> | ||
|
|
||
| <InstallNuxt /> | ||
|
|
||
| </Step> | ||
|
|
||
| <Step checkpoint title="Verify PostHog is initialized" subtitle="Confirm you can capture events with PostHog"> | ||
|
|
||
| Before proceeding, confirm that you can [capture events](/docs/libraries/nuxt-js#capture-custom-events) using `posthog.capture('test_event')`. | ||
|
|
||
| </Step> | ||
|
|
||
| <Step title="Manually capturing exceptions" subtitle="Your goal in this step: Manually capture exceptions in your Nuxt application." badge="required"> | ||
|
|
||
| To send errors directly using the PostHog client, import it and use the `captureException` method like this: | ||
|
|
||
| ```vue component.vue | ||
| <script> | ||
| const { $posthog } = useNuxtApp() | ||
| if ($posthog) { | ||
| const posthog = $posthog() | ||
| posthog.captureException(new Error("Important error message")) | ||
| } | ||
| </script> | ||
| ``` | ||
|
|
||
| On the server side, you can use the `posthog` object directly. | ||
|
|
||
| ```js file=server/api/example.js focusOnLines=4-21 | ||
| export default defineEventHandler(async (event) => { | ||
| const distinctId = getCookie(event, 'distinct_id') | ||
|
|
||
| const { PostHog } = await import('posthog-node'); | ||
| const runtimeConfig = useRuntimeConfig() | ||
|
|
||
| const posthog = new PostHog( | ||
| runtimeConfig.public.posthogPublicKey, | ||
| { | ||
| host: runtimeConfig.public.posthogHost, | ||
| } | ||
| ); | ||
|
|
||
| try { | ||
| const results = await DB.query.users.findMany() | ||
| return results | ||
| } catch (error) { | ||
| posthog.captureException(error) | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| </Step> | ||
|
|
||
| <Step title="Configuring exception autocapture" subtitle="Your goal in this step: Enable automatic exception tracking for your Nuxt application." badge="required"> | ||
|
|
||
| Update your `posthog.client.js` to add an error hook | ||
ablaszkiewicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```js | ||
| export default defineNuxtPlugin((nuxtApp) => { | ||
| ... | ||
| nuxtApp.hook('vue:error', (error) => { | ||
| posthogClient.captureException(error) | ||
| }) | ||
| ... | ||
| }) | ||
| ``` | ||
|
|
||
| </Step> | ||
|
|
||
| <StepVerifyErrorTracking /> | ||
|
|
||
|
|
||
| <Step title="Upload source maps" badge="required"> | ||
|
|
||
| <StepUploadSourceMaps urlPath="nuxt" framework="Nuxt" /> | ||
|
|
||
| </Step> | ||
|
|
||
| </Steps> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| title: Nuxt.js (v2.16 and below) | ||
| icon: >- | ||
| https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/docs/integrate/frameworks/nuxt.svg | ||
| --- | ||
|
|
||
| PostHog makes it easy to get data about usage of your [Nuxt.js](https://nuxt.com/) app. Integrating PostHog into your app enables analytics about user behavior, custom events capture, session replays, feature flags, and more. | ||
|
|
||
| These docs are for Nuxt v2.16 and below. | ||
|
|
||
| We are going to implement PostHog as a [Nuxt.js integration](https://nuxtjs.org/docs/2.x/directory-structure/plugins) which gives us the possibility to inject | ||
| the `posthog` object and make it available across our application. | ||
|
|
||
| ## Installation | ||
|
|
||
| The first thing you want to do is to install the [posthog-js library](/docs/integrate/client/js) in your project - so add it using your package manager: | ||
|
|
||
| import InstallWebPackageManagers from "../integrate/_snippets/install-web-package-managers.mdx" | ||
|
|
||
| <InstallWebPackageManagers /> | ||
|
|
||
| After that we want to create a app in `plugins/posthog/index.js` | ||
ablaszkiewicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```javascript | ||
| import posthog from 'posthog-js' | ||
| import Vue from 'vue' | ||
|
|
||
| export default function({ app: { router } }, inject) { | ||
| // Init PostHog | ||
| posthog.init('<ph_project_api_key>', { | ||
| api_host: '<ph_client_api_host>', | ||
| defaults: '<ph_posthog_js_defaults>', | ||
| 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 change | ||
| router.afterEach(to => { | ||
| Vue.nextTick(() => { | ||
| /* Note: this might also be a good place to call posthog.register(...) in order to update your properties | ||
| on 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` | ||
|
|
||
| ```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](https://nuxtjs.org/docs/2.x/directory-structure/plugins#inject-in-root--context) 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: | ||
|
|
||
| ```js-web | ||
| <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](/docs/libraries/js) and [Node](/docs/libraries/node) SDK docs. | ||
|
|
||
| Alternatively, the following tutorials can help you get started: | ||
|
|
||
| - [How to set up analytics in Nuxt](/tutorials/nuxt-analytics) | ||
| - [How to set up feature flags in Nuxt](/tutorials/nuxt-feature-flags) | ||
| - [How to set up A/B tests in Nuxt](/tutorials/nuxtjs-ab-tests) | ||
| - [How to set up surveys in Nuxt](/tutorials/nuxt-surveys) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.