Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 0 additions & 15 deletions apps/nuxt/app.vue

This file was deleted.

20 changes: 20 additions & 0 deletions apps/nuxt/app/app.vue
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/runtime';

// 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.
1 change: 1 addition & 0 deletions apps/nuxt/app/assets/logo.svg
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.
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
<script setup lang="ts">
import { track } from '@vercel/analytics';

function navigate(event: { target: { href: string } }) {
track('navigation', { to: event.target.href });
}
</script>

<template>
<header>
<img
alt="Vue logo"
class="logo"
src="@/assets/logo.svg"
width="125"
height="125"
/>
<img alt="Nuxt logo" class="logo" src="@/assets/logo.svg" width="180" />
<div class="wrapper">
<nav>
<NuxtLink @click="navigate" to="/">Home</NuxtLink>
<NuxtLink @click="navigate" to="/blog/various/hi">Hi!</NuxtLink>
<NuxtLink @click="navigate" to="/blog/various/hallo">Hallo!</NuxtLink>
<NuxtLink to="/">Home</NuxtLink>
<NuxtLink to="/blog/various/hi">Hi!</NuxtLink>
<NuxtLink to="/blog/various/hallo">Hallo!</NuxtLink>
</nav>
</div>
<slot />
Expand All @@ -32,11 +18,12 @@ function navigate(event: { target: { href: string } }) {
header {
line-height: 1.5;
max-height: 100vh;
text-align: center;
}

.logo {
display: block;
margin: 0 auto 2rem;
margin: 2rem auto 2rem;
}

nav {
Expand Down
4 changes: 4 additions & 0 deletions apps/nuxt/app/pages/blog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<h2>Blog</h2>
<NuxtPage />
</template>
9 changes: 9 additions & 0 deletions apps/nuxt/app/pages/blog/[category].vue
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>
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<script setup lang="ts">
import { track } from '@vercel/analytics/nuxt/runtime';

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.
16 changes: 16 additions & 0 deletions apps/nuxt/app/plugins/analytics.ts
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/runtime"

// export default defineNuxtPlugin(() => {
// injectAnalytics({
// beforeSend(event) {
// console.log('beforeSend', event.url)
// if (event.url.includes('/private')) {
// return null;
// }
// return event
// }
// })
// })
1 change: 0 additions & 1 deletion apps/nuxt/assets/logo.svg

This file was deleted.

3 changes: 2 additions & 1 deletion apps/nuxt/nuxt.config.ts
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'],
devtools: { enabled: true },
});
3 changes: 1 addition & 2 deletions apps/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"postinstall": "nuxt prepare",
"preview": "nuxt preview"
},
"dependencies": {
"@vercel/analytics": "workspace:*",
"nuxt": "^4.2.1",
"nuxt": "^4.2.0",
"vue": "latest",
"vue-router": "latest"
}
Expand Down
3 changes: 0 additions & 3 deletions apps/nuxt/server/tsconfig.json

This file was deleted.

17 changes: 15 additions & 2 deletions apps/nuxt/tsconfig.json
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"
}
]
}
14 changes: 12 additions & 2 deletions packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vercel/analytics",
"version": "1.6.1",
"version": "2.0.0",
"description": "Gain real-time traffic insights with Vercel Web Analytics",
"keywords": [
"analytics",
Expand All @@ -27,10 +27,14 @@
"require": "./dist/next/index.js"
},
"./nuxt": {
"browser": "./dist/nuxt/index.mjs",
"import": "./dist/nuxt/index.mjs",
"require": "./dist/nuxt/index.js"
},
"./nuxt/runtime": {
"browser": "./dist/nuxt/runtime/index.mjs",
"import": "./dist/nuxt/runtime/index.mjs",
"require": "./dist/nuxt/runtime/index.js"
},
"./react": {
"browser": "./dist/react/index.mjs",
"import": "./dist/react/index.mjs",
Expand Down Expand Up @@ -71,6 +75,9 @@
"nuxt": [
"dist/nuxt/index.d.ts"
],
"nuxt/runtime": [
"dist/nuxt/runtime/index.d.ts"
],
"react": [
"dist/react/index.d.ts"
],
Expand Down Expand Up @@ -109,6 +116,8 @@
]
},
"devDependencies": {
"@nuxt/kit": "^4.2.0",
"@nuxt/schema": "^4.2.0",
"@swc/core": "^1.9.2",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
Expand All @@ -126,6 +135,7 @@
"@remix-run/react": "^2",
"@sveltejs/kit": "^1 || ^2",
"next": ">= 13",
"nuxt": ">= 3",
"react": "^18 || ^19 || ^19.0.0-rc",
"svelte": ">= 4",
"vue": "^3",
Expand Down
6 changes: 0 additions & 6 deletions packages/web/src/nuxt/index.ts

This file was deleted.

29 changes: 29 additions & 0 deletions packages/web/src/nuxt/module.ts
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/runtime'
import { defineNuxtPlugin } from '#imports'

export default defineNuxtPlugin(() => {
injectAnalytics()
})
`,
});

addPlugin({
src: template.dst,
mode: 'client',
});
},
}) as NuxtModule;
43 changes: 43 additions & 0 deletions packages/web/src/nuxt/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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 };
12 changes: 12 additions & 0 deletions packages/web/src/nuxt/runtime/utils.ts
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
}
}
19 changes: 17 additions & 2 deletions packages/web/tsup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ export default defineConfig([
{
...cfg,
entry: {
index: 'src/nuxt/index.ts',
index: 'src/nuxt/module.ts',
},
external: ['vue', 'vue-router'],
external: [
'vue',
'vue-router',
'nuxt',
'@nuxt/kit',
'@nuxt/schema',
'#imports',
],
outDir: 'dist/nuxt',
},
{
...cfg,
entry: {
index: 'src/nuxt/runtime/index.ts',
},
external: ['vue', 'vue-router', 'nuxt'],
outDir: 'dist/nuxt/runtime',
},
{
...cfg,
entry: {
Expand Down
Loading
Loading