Skip to content

Commit c09b649

Browse files
authored
docs: exposes types and updates example apps for beforeSend (#154)
1 parent 81d0eed commit c09b649

File tree

20 files changed

+3215
-8866
lines changed

20 files changed

+3215
-8866
lines changed

apps/astro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"start": "astro dev"
1212
},
1313
"dependencies": {
14+
"@astrojs/ts-plugin": "^1.10.4",
1415
"@vercel/analytics": "workspace:*",
1516
"astro": "latest"
1617
},

apps/astro/src/components/BaseHead.astro

Lines changed: 0 additions & 4 deletions
This file was deleted.

apps/astro/src/layouts/Base.astro

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import Analytics from '@vercel/analytics/astro';
33
44
const { title } = Astro.props;
55
---
6+
7+
<script is:inline>
8+
function webAnalyticsBeforeSend(data){
9+
console.log("Web Analytics before send", data)
10+
return data;
11+
}
12+
</script>
13+
614
<html lang="en">
715
<head>
816
<meta charset="utf-8" />

apps/astro/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "astro/tsconfigs/base",
33
"compilerOptions": {
4-
"strictNullChecks": true
4+
"strictNullChecks": true,
5+
"plugins": [{ "name": "@astrojs/ts-plugin" }]
56
}
67
}

apps/nuxt/app.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
<script setup lang="ts">
2+
import { Analytics, type BeforeSendEvent } from '@vercel/analytics/nuxt';
3+
4+
const beforeSend = (event: BeforeSendEvent) => {
5+
console.log('Sending event:', event);
6+
return event;
7+
};
8+
</script>
9+
110
<template>
11+
<Analytics :before-send="beforeSend" />
212
<NuxtLayout>
313
<NuxtPage />
414
</NuxtLayout>

apps/nuxt/layouts/default.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<script setup lang="ts">
2-
import { Analytics } from '@vercel/analytics/nuxt';
32
import { track } from '@vercel/analytics';
43
5-
function navigate(event) {
4+
function navigate(event: { target: { href: string } }) {
65
track('navigation', { to: event.target.href });
76
}
87
</script>
98

109
<template>
11-
<Analytics />
1210
<header>
1311
<img
1412
alt="Vue logo"

apps/sveltekit/src/routes/+layout.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

apps/sveltekit/src/routes/+layout.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { dev } from '$app/environment';
2+
import {
3+
injectAnalytics,
4+
type BeforeSendEvent,
5+
} from '@vercel/analytics/sveltekit';
6+
7+
injectAnalytics({
8+
mode: dev ? 'development' : 'production',
9+
beforeSend(event: BeforeSendEvent) {
10+
console.log('beforeSend', event);
11+
return event;
12+
},
13+
});

apps/vue/src/App.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
<script setup>
2-
import { Analytics } from '@vercel/analytics/vue';
1+
<script setup lang="ts">
2+
import { Analytics, type BeforeSendEvent } from '@vercel/analytics/vue';
33
import HelloWorld from './components/HelloWorld.vue';
4+
5+
const beforeSend = (event: BeforeSendEvent) => {
6+
console.log('Sending event:', event);
7+
return event;
8+
};
49
</script>
510

611
<template>
7-
<Analytics />
12+
<Analytics :before-send="beforeSend" />
813
<header>
914
<img
1015
alt="Vue logo"

packages/web/src/astro/component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
// @ts-expect-error typescript doesn't handle ./index.astro properly, but it's needed to generate types
22
// eslint-disable-next-line import/no-default-export, no-useless-rename -- Exporting everything doesn't yield the desired outcome
33
export { default as default } from './index.astro';
4+
export type {
5+
AnalyticsProps,
6+
BeforeSend,
7+
BeforeSendEvent,
8+
// @ts-expect-error this filed is copied to dist, so it's ok to reference the generated index.d.ts
9+
} from '../index.d.ts';

packages/web/src/astro/index.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const paramsStr = JSON.stringify(Astro.params);
2626
inject({
2727
...props,
2828
disableAutoTrack: true,
29-
framework: 'astro'
29+
framework: 'astro',
30+
beforeSend: window.webAnalyticsBeforeSend,
3031
});
3132
const path = this.dataset.pathname;
3233
pageview({ route: computeRoute(path ?? '', params), path });

packages/web/src/generic.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
AllowedPropertyValues,
55
AnalyticsProps,
66
FlagsDataInput,
7+
BeforeSend,
8+
BeforeSendEvent,
79
} from './types';
810
import {
911
isBrowser,
@@ -149,7 +151,7 @@ function pageview({
149151
}
150152

151153
export { inject, track, pageview, computeRoute };
152-
export type { AnalyticsProps };
154+
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };
153155

154156
// eslint-disable-next-line import/no-default-export -- Default export is intentional
155157
export default {

packages/web/src/nextjs/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Suspense, type ReactNode } from 'react';
22
import { Analytics as AnalyticsScript } from '../react';
3-
import type { AnalyticsProps } from '../types';
3+
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
44
import { useRoute } from './utils';
55

66
type Props = Omit<AnalyticsProps, 'route' | 'disableAutoTrack'>;
@@ -21,4 +21,4 @@ export function Analytics(props: Props): null {
2121
) as never;
2222
}
2323

24-
export type { AnalyticsProps };
24+
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };

packages/web/src/nuxt/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
12
import { createComponent } from '../vue/create-component';
23

34
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any
45
export const Analytics = createComponent('nuxt');
6+
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };

packages/web/src/react.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22
import { useEffect } from 'react';
33
import { inject, track, pageview } from './generic';
4-
import type { AnalyticsProps } from './types';
4+
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from './types';
55

66
/**
77
* Injects the Vercel Web Analytics script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/concepts/analytics/package).
@@ -60,4 +60,4 @@ function Analytics(
6060
}
6161

6262
export { track, Analytics };
63-
export type { AnalyticsProps };
63+
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };

packages/web/src/remix/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
22
import { Analytics as AnalyticsScript } from '../react';
3-
import type { AnalyticsProps } from '../types';
3+
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
44
import { useRoute } from './utils';
55

66
export function Analytics(props: Omit<AnalyticsProps, 'route'>): JSX.Element {
77
return <AnalyticsScript {...useRoute()} {...props} framework="remix" />;
88
}
9+
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };

packages/web/src/sveltekit/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { inject, pageview, track, type AnalyticsProps } from '../generic';
1+
import { inject, pageview, track } from '../generic';
2+
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
23
import { page } from '$app/stores';
34
import { browser } from '$app/environment';
45
import type {} from '@sveltejs/kit';
@@ -21,4 +22,4 @@ function injectAnalytics(props: Omit<AnalyticsProps, 'framework'> = {}): void {
2122
}
2223

2324
export { injectAnalytics, track };
24-
export type { AnalyticsProps };
25+
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };

packages/web/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ declare global {
3636
vaq?: [string, unknown?][];
3737
vai?: boolean;
3838
vam?: Mode;
39+
/** used by Astro component only */
40+
webAnalyticsBeforeSend?: BeforeSend;
3941
}
4042
}
4143

packages/web/src/vue/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
12
import { createComponent } from './create-component';
23

34
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- vue's defineComponent return type is any
45
export const Analytics = createComponent();
6+
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };

0 commit comments

Comments
 (0)