Skip to content

Commit

Permalink
Use debug script during development mode (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiaslins authored Nov 7, 2022
1 parent 3e85f39 commit 5799c4f
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 196 deletions.
3 changes: 2 additions & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vercel/analytics",
"version": "0.1.3",
"version": "0.1.4",
"keywords": [
"analytics",
"vercel"
Expand Down Expand Up @@ -32,6 +32,7 @@
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"lint": "eslint .",
"lint-fix": "eslint . --fix",
"type-check": "tsc --noEmit"
Expand Down
24 changes: 15 additions & 9 deletions packages/web/src/generic.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { initQueue } from './queue';
import type { BeforeSend } from './types';
import type { AnalyticsProps } from './types';
import { isBrowser, isDevelopment } from './utils';

const isBrowser = typeof window !== 'undefined';

export const inject = ({
beforeSend,
}: { beforeSend?: BeforeSend } = {}): void => {
if (!isBrowser) return;
export const inject = (
{ beforeSend, debug }: AnalyticsProps = { debug: isDevelopment() },
): void => {
if (!isBrowser()) return;
initQueue();

if (beforeSend) {
window.va?.('beforeSend', beforeSend);
}
const src = isDevelopment()
? 'https://cdn.vercel-insights.com/v1/script.debug.js'
: '/_vercel/insights/script.js';

if (document.head.querySelector('script[src="/va/script.js"]')) return;
if (document.head.querySelector(`script[src*="${src}"]`)) return;

const script = document.createElement('script');
script.src = '/va/script.js';
script.src = src;
script.defer = true;

if (isDevelopment() && debug === false) {
script.setAttribute('data-debug', 'false');
}

document.head.appendChild(script);
};
37 changes: 9 additions & 28 deletions packages/web/src/react.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
import React, { useEffect } from 'react';
import { useEffect } from 'react';
import { inject } from './generic';
import type { BeforeSend } from './types';
import type { AnalyticsProps } from './types';
import { isDevelopment } from './utils';

interface AnalyticsProps {
beforeSend?: BeforeSend;
}

export function Analytics(props: AnalyticsProps): JSX.Element {
if (process.env.NODE_ENV !== 'production') {
return <NoopAnalytics />;
}

return <EnabledAnalytics {...props} />;
}

function EnabledAnalytics({ beforeSend }: AnalyticsProps): null {
useEffect(() => {
inject({ beforeSend });
}, [beforeSend]);

return null;
}

function NoopAnalytics(): null {
export function Analytics({
beforeSend,
debug = isDevelopment(),
}: AnalyticsProps): null {
useEffect(() => {
// eslint-disable-next-line no-console
console.warn(
'Vercel Analytics is set up, but detected a non-production environment.\n\nPlease note that no analytics events will be sent.',
);
}, []);
inject({ beforeSend, debug });
}, [beforeSend, debug]);

return null;
}
5 changes: 4 additions & 1 deletion packages/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ interface PageViewEvent {
type IEvent = PageViewEvent;

export type BeforeSend = (event: IEvent) => IEvent | null;

export interface AnalyticsProps {
beforeSend?: BeforeSend;
debug?: boolean;
}
declare global {
interface Window {
// Base interface
Expand Down
9 changes: 9 additions & 0 deletions packages/web/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function isBrowser(): boolean {
return typeof window !== 'undefined';
}

export function isDevelopment(): boolean {
return (
typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'
);
}
Loading

0 comments on commit 5799c4f

Please sign in to comment.