Skip to content

Commit

Permalink
Add JSDoc comments for easier usage (#73)
Browse files Browse the repository at this point in the history
* Add JSDoc comments for easier usage

* Disable default linting rules to be able to document an object
* Unfortunately we have some duplication, which we can't avoid if don't go all in on JSDoc types

* Remove doc type from prop object

* Remove added whitespace

* Fix invalid import in example
  • Loading branch information
timolins committed Apr 27, 2023
1 parent 9724c67 commit 7f4de7e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"extends": [
"@vercel/eslint-config"
],
"rules": {
"tsdoc/syntax": "off"
},
"ignorePatterns": [
"jest.setup.ts"
]
Expand Down
18 changes: 16 additions & 2 deletions packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {
isProduction,
} from './utils';

/**
* 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).
* @param [props] - Analytics options.
* @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.
* - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.
* - `production` - Always use the production script. (Sends events to the server)
* - `development` - Always use the development script. (Logs events to the console)
* @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
* @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
*/
export function inject(
props: AnalyticsProps = {
debug: true,
Expand Down Expand Up @@ -43,6 +53,12 @@ export function inject(
document.head.appendChild(script);
}

/**
* Tracks a custom event. Please refer to the [documentation](https://vercel.com/docs/concepts/analytics/custom-events) for more information on custom events.
* @param name - The name of the event.
* * Examples: `Purchase`, `Click Button`, or `Play Video`.
* @param [properties] - Additional properties of the event. Nested objects are not supported. Allowed values are `string`, `number`, `boolean`, and `null`.
*/
export function track(
name: string,
properties?: Record<string, AllowedPropertyValues>,
Expand Down Expand Up @@ -82,5 +98,3 @@ export default {
inject,
track,
};

export type { BeforeSendEvent } from './types';
23 changes: 23 additions & 0 deletions packages/web/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ import { useEffect } from 'react';
import { inject, track } from './generic';
import type { AnalyticsProps } 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).
* @param [props] - Analytics options.
* @param [props.mode] - The mode to use for the analytics script. Defaults to `auto`.
* - `auto` - Automatically detect the environment. Uses `production` if the environment cannot be determined.
* - `production` - Always use the production script. (Sends events to the server)
* - `development` - Always use the development script. (Logs events to the console)
* @param [props.debug] - Whether to enable debug logging in development. Defaults to `true`.
* @param [props.beforeSend] - A middleware function to modify events before they are sent. Should return the event object or `null` to cancel the event.
* @example
* ```js
* import { Analytics } from '@vercel/analytics/react';
*
* export default function App() {
* return (
* <div>
* <Analytics />
* <h1>My App</h1>
* </div>
* );
* }
* ```
*/
function Analytics({
beforeSend,
debug = true,
Expand Down

0 comments on commit 7f4de7e

Please sign in to comment.