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: exposes types and updates example apps for beforeSend #154

Merged
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
1 change: 1 addition & 0 deletions apps/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start": "astro dev"
},
"dependencies": {
"@astrojs/ts-plugin": "^1.10.4",
"@vercel/analytics": "workspace:*",
"astro": "latest"
},
Expand Down
4 changes: 0 additions & 4 deletions apps/astro/src/components/BaseHead.astro

This file was deleted.

8 changes: 8 additions & 0 deletions apps/astro/src/layouts/Base.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import Analytics from '@vercel/analytics/astro';

const { title } = Astro.props;
---

<script is:inline>
function webAnalyticsBeforeSend(data){
console.log("Web Analytics before send", data)
return data;
}
</script>

<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down
3 changes: 2 additions & 1 deletion apps/astro/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"strictNullChecks": true
"strictNullChecks": true,
"plugins": [{ "name": "@astrojs/ts-plugin" }]
}
}
10 changes: 10 additions & 0 deletions apps/nuxt/app.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<script setup lang="ts">
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>
Expand Down
4 changes: 1 addition & 3 deletions apps/nuxt/layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<script setup lang="ts">
import { Analytics } from '@vercel/analytics/nuxt';
import { track } from '@vercel/analytics';

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

<template>
<Analytics />
<header>
<img
alt="Vue logo"
Expand Down
3 changes: 0 additions & 3 deletions apps/sveltekit/src/routes/+layout.js

This file was deleted.

13 changes: 13 additions & 0 deletions apps/sveltekit/src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { dev } from '$app/environment';
import {
injectAnalytics,
type BeforeSendEvent,
} from '@vercel/analytics/sveltekit';

injectAnalytics({
mode: dev ? 'development' : 'production',
beforeSend(event: BeforeSendEvent) {
console.log('beforeSend', event);
return event;
},
});
11 changes: 8 additions & 3 deletions apps/vue/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<script setup>
import { Analytics } from '@vercel/analytics/vue';
<script setup lang="ts">
import { Analytics, type BeforeSendEvent } from '@vercel/analytics/vue';
import HelloWorld from './components/HelloWorld.vue';

const beforeSend = (event: BeforeSendEvent) => {
console.log('Sending event:', event);
return event;
};
</script>

<template>
<Analytics />
<Analytics :before-send="beforeSend" />
<header>
<img
alt="Vue logo"
Expand Down
6 changes: 6 additions & 0 deletions packages/web/src/astro/component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// @ts-expect-error typescript doesn't handle ./index.astro properly, but it's needed to generate types
// eslint-disable-next-line import/no-default-export, no-useless-rename -- Exporting everything doesn't yield the desired outcome
export { default as default } from './index.astro';
export type {
AnalyticsProps,
BeforeSend,
BeforeSendEvent,
// @ts-expect-error this filed is copied to dist, so it's ok to reference the generated index.d.ts
} from '../index.d.ts';
3 changes: 2 additions & 1 deletion packages/web/src/astro/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const paramsStr = JSON.stringify(Astro.params);
inject({
...props,
disableAutoTrack: true,
framework: 'astro'
framework: 'astro',
beforeSend: window.webAnalyticsBeforeSend,
});
const path = this.dataset.pathname;
pageview({ route: computeRoute(path ?? '', params), path });
Expand Down
4 changes: 3 additions & 1 deletion packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type {
AllowedPropertyValues,
AnalyticsProps,
FlagsDataInput,
BeforeSend,
BeforeSendEvent,
} from './types';
import {
isBrowser,
Expand Down Expand Up @@ -149,7 +151,7 @@ function pageview({
}

export { inject, track, pageview, computeRoute };
export type { AnalyticsProps };
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };

// eslint-disable-next-line import/no-default-export -- Default export is intentional
export default {
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/nextjs/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Suspense, type ReactNode } from 'react';
import { Analytics as AnalyticsScript } from '../react';
import type { AnalyticsProps } from '../types';
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
import { useRoute } from './utils';

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

export type { AnalyticsProps };
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };
2 changes: 2 additions & 0 deletions packages/web/src/nuxt/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
import { createComponent } from '../vue/create-component';

// 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 };
4 changes: 2 additions & 2 deletions packages/web/src/react.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';
import { useEffect } from 'react';
import { inject, track, pageview } from './generic';
import type { AnalyticsProps } from './types';
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from './types';

/**
* 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).
Expand Down Expand Up @@ -60,4 +60,4 @@ function Analytics(
}

export { track, Analytics };
export type { AnalyticsProps };
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };
3 changes: 2 additions & 1 deletion packages/web/src/remix/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import { Analytics as AnalyticsScript } from '../react';
import type { AnalyticsProps } from '../types';
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
import { useRoute } from './utils';

export function Analytics(props: Omit<AnalyticsProps, 'route'>): JSX.Element {
return <AnalyticsScript {...useRoute()} {...props} framework="remix" />;
}
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };
5 changes: 3 additions & 2 deletions packages/web/src/sveltekit/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { inject, pageview, track, type AnalyticsProps } from '../generic';
import { inject, pageview, track } from '../generic';
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
import { page } from '$app/stores';
import { browser } from '$app/environment';
import type {} from '@sveltejs/kit';
Expand All @@ -21,4 +22,4 @@ function injectAnalytics(props: Omit<AnalyticsProps, 'framework'> = {}): void {
}

export { injectAnalytics, track };
export type { AnalyticsProps };
export type { AnalyticsProps, BeforeSend, BeforeSendEvent };
2 changes: 2 additions & 0 deletions packages/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ declare global {
vaq?: [string, unknown?][];
vai?: boolean;
vam?: Mode;
/** used by Astro component only */
webAnalyticsBeforeSend?: BeforeSend;
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/vue/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { AnalyticsProps, BeforeSend, BeforeSendEvent } from '../types';
import { createComponent } from './create-component';

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