-
Notifications
You must be signed in to change notification settings - Fork 42
feat(nuxt): Add support for injectAnalytics() and Nuxt module
#178
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
+6,231
−4,512
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f669d3d
feat(nuxt): use a module instead
HugoRCD 2256fc6
refactor(nuxt): update module import and enhance plugin setup
HugoRCD 6dcf180
feat: nuxt module ready + add injectAnalytics()
atinux 2ef9b29
update example
atinux 85e7d3a
lint fix
atinux 3f56d60
update lock
atinux b2e6e73
remove postinstall
atinux 016b460
fix: use children path and fix based on Vade suggestions
atinux ee4fb63
fix: use vue-router instead
atinux 1ced7e6
lint fix
atinux d456767
chore: update based on review
atinux 1f84078
update to latest LTS version
atinux 81a673b
fix: move to node 22 for sveltekit
atinux 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| 18.19.0 | ||
| 22.21.0 |
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
| <script setup lang="ts"> | ||
| /** | ||
| * Uncomment only when disabling the module in nuxt.config.ts | ||
| * As well as using the deprected <Analytics> component | ||
| * We recommend using the module or the injectAnalytics function in a Nuxt plugin | ||
| */ | ||
| // import { Analytics, type BeforeSendEvent } from '@vercel/analytics/nuxt'; | ||
|
|
||
| // const beforeSend = (event: BeforeSendEvent) => { | ||
| // console.log('Sending event:', event); | ||
| // return event; | ||
| // }; | ||
| </script> | ||
|
|
||
| <template> | ||
| <!-- <Analytics :before-send="beforeSend" /> --> | ||
| <NuxtLayout> | ||
| <NuxtPage /> | ||
| </NuxtLayout> | ||
| </template> | ||
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
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,4 @@ | ||
| <template> | ||
| <h2>Blog</h2> | ||
| <NuxtPage /> | ||
| </template> |
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,9 @@ | ||
| <script setup lang="ts"> | ||
| const route = useRoute(); | ||
| const category = route.params.category; | ||
| </script> | ||
|
|
||
| <template> | ||
| <h2>Category: {{ category }}</h2> | ||
| <NuxtPage /> | ||
| </template> |
6 changes: 5 additions & 1 deletion
6
apps/nuxt/pages/blog/[category]/[slug].vue → ...nuxt/app/pages/blog/[category]/[slug].vue
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 |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| <script setup lang="ts"> | ||
| import { track } from '@vercel/analytics/nuxt'; | ||
|
|
||
| const route = useRoute(); | ||
| </script> | ||
|
|
||
| <template> | ||
| <div class="greetings"> | ||
| <h1 class="green">{{ route.params.slug }}!</h1> | ||
| <h3>Category {{ route.params.category }}</h3> | ||
| <button @click="track('click-button', { route: $route.path })"> | ||
| Button with tracking | ||
| </button> | ||
| </div> | ||
| </template> |
File renamed without changes.
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,16 @@ | ||
| /** | ||
| * Uncomment only when disabling the module in nuxt.config.ts | ||
| */ | ||
| // import { injectAnalytics } from "@vercel/analytics/nuxt" | ||
|
|
||
| // export default defineNuxtPlugin(() => { | ||
| // injectAnalytics({ | ||
| // beforeSend(event) { | ||
| // console.log('beforeSend', event.url) | ||
| // if (event.url.includes('/private')) { | ||
| // return null; | ||
| // } | ||
| // return event | ||
| // } | ||
| // }) | ||
| // }) |
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| // https://nuxt.com/docs/api/configuration/nuxt-config | ||
| export default defineNuxtConfig({ | ||
| compatibilityDate: '2024-04-03', | ||
| compatibilityDate: '2025-11-06', | ||
| modules: ['@vercel/analytics/nuxt/module'], | ||
| devtools: { enabled: true }, | ||
| }); |
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| { | ||
| // https://nuxt.com/docs/guide/concepts/typescript | ||
| "extends": "./.nuxt/tsconfig.json" | ||
| "files": [], | ||
| "references": [ | ||
| { | ||
| "path": "./.nuxt/tsconfig.app.json" | ||
| }, | ||
| { | ||
| "path": "./.nuxt/tsconfig.server.json" | ||
| }, | ||
| { | ||
| "path": "./.nuxt/tsconfig.shared.json" | ||
| }, | ||
| { | ||
| "path": "./.nuxt/tsconfig.node.json" | ||
| } | ||
| ] | ||
| } |
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 |
|---|---|---|
| @@ -1,6 +1,43 @@ | ||
| import { onNuxtReady, useRouter, useRoute } from 'nuxt/app'; | ||
| import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types'; | ||
| import { createComponent } from '../vue/create-component'; | ||
| import { inject, pageview, track } from '../generic'; | ||
| import { isBrowser, computeRoute } from '../utils'; | ||
| import { getBasePath } from './utils'; | ||
|
|
||
| // Export the Analytics component | ||
| // Not recommended as must be used in both app.vue and error.vue | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any | ||
| export const Analytics = createComponent('nuxt'); | ||
| export type { AnalyticsProps, BeforeSend, BeforeSendEvent }; | ||
|
|
||
| // Export the injectAnalytics script with automatic tracking on page changes | ||
| function injectAnalytics(props: Omit<AnalyticsProps, 'framework'> = {}): void { | ||
| if (isBrowser()) { | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks -- we are not using a React here | ||
| const router = useRouter(); | ||
|
|
||
| onNuxtReady(() => { | ||
| inject({ | ||
| ...props, | ||
| framework: 'nuxt', | ||
| disableAutoTrack: true, | ||
| basePath: getBasePath(), | ||
| }); | ||
| const route = useRoute(); | ||
| pageview({ | ||
| route: computeRoute(route.path, route.params), | ||
| path: route.path, | ||
| }); | ||
| }); | ||
| // On navigation to a new page | ||
| router.afterEach((to) => { | ||
| pageview({ | ||
| route: computeRoute(to.path, to.params), | ||
| path: to.path, | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export { injectAnalytics, track }; |
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,29 @@ | ||
| import { defineNuxtModule, addPlugin, addTemplate } from '@nuxt/kit'; | ||
| import type { NuxtModule } from '@nuxt/schema'; | ||
|
|
||
| // eslint-disable-next-line import/no-default-export -- default export is required for nuxt module | ||
| export default defineNuxtModule({ | ||
| meta: { | ||
| name: '@vercel/analytics', | ||
| configKey: 'analytics', | ||
| docs: 'https://vercel.com/docs/analytics/quickstart', | ||
| }, | ||
| setup() { | ||
| const template = addTemplate({ | ||
| filename: 'vercel-analytics.client.ts', | ||
| getContents: () => ` | ||
| import { injectAnalytics } from '@vercel/analytics/nuxt' | ||
| import { defineNuxtPlugin } from '#imports' | ||
|
|
||
| export default defineNuxtPlugin(() => { | ||
| injectAnalytics() | ||
| }) | ||
| `, | ||
| }); | ||
|
|
||
| addPlugin({ | ||
| src: template.dst, | ||
| mode: 'client', | ||
| }); | ||
| }, | ||
| }) as NuxtModule; |
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,12 @@ | ||
| export function getBasePath(): string | undefined { | ||
| // !! important !! | ||
| // do not access env variables using import.meta.env[varname] | ||
| // some bundles won't replace the value at build time. | ||
| try { | ||
| return import.meta.env.VITE_VERCEL_OBSERVABILITY_BASEPATH as | ||
| | string | ||
| | undefined; | ||
| } catch { | ||
| // do nothing | ||
| } | ||
| } |
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
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.