chore(tracker): 🏷️ could not use string in EventData

- EventData was incorrectly typed `Record<string, object>` which did not allow for the most common `string` attribute
- UmamiTracker is now exported to be used elsewhere if needed
- A note has been added on event names being truncated to 50 characters
This commit is contained in:
C-A de Salaberry 2023-06-02 15:06:19 +02:00
parent 1869a809cf
commit d575b9c377

37
tracker/index.d.ts vendored
View File

@ -1,4 +1,4 @@
type TrackedProperties = {
export type TrackedProperties = {
/**
* Hostname of server
*
@ -55,7 +55,7 @@ type TrackedProperties = {
website: string;
};
type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] }
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
/**
*
@ -65,15 +65,23 @@ type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] }
* - Arrays are converted to a String, with the same max length of 500.
* - Objects have a max of 50 properties. Arrays are considered 1 property.
*/
type EventData = Record<string, object>;
type EventProperties = {
export interface EventData {
[key: string]: number | string | EventData | number[] | string[] | EventData[];
}
export type EventProperties = {
/**
* NOTE: event names will be truncated past 50 characters
*/
name: string;
data?: EventData;
} & WithRequired<TrackedProperties, 'website'>
| WithRequired<TrackedProperties, 'website'>;
} & WithRequired<TrackedProperties, 'website'>;
export type PageViewProperties = WithRequired<TrackedProperties, 'website'>;
export type CustomEventFunction = (
props: PageViewProperties,
) => EventProperties | PageViewProperties;
interface Window {
umami: {
export type UmamiTracker = {
track: {
/**
* Track a page view
@ -87,6 +95,8 @@ interface Window {
/**
* Track an event with a given name
*
* NOTE: event names will be truncated past 50 characters
*
* @example ```
* umami.track('signup-button');
* ```
@ -96,6 +106,8 @@ interface Window {
/**
* Tracks an event with dynamic data.
*
* NOTE: event names will be truncated past 50 characters
*
* When tracking events, the default properties are included in the payload. This is equivalent to running:
*
* ```js
@ -122,7 +134,7 @@ interface Window {
* umami.track({ website: 'e676c9b4-11e4-4ef1-a4d7-87001773e9f2', url: '/home', title: 'Home page' });
* ```
*/
(properties: WithRequired<Partial<TrackedProperties>, 'website'>): Promise<string>;
(properties: PageViewProperties): Promise<string>;
/**
* Tracks an event with fully customizable dynamic data
@ -132,7 +144,10 @@ interface Window {
* umami.track((props) => ({ ...props, url: path }));
* ```
*/
(eventFunction: (prop: TrackedProperties) => EventProperties): Promise<string>;
};
(eventFunction: CustomEventFunction): Promise<string>;
};
};
interface Window {
umami: UmamiTracker;
}