Skip to content

Commit

Permalink
Add support for trackEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
IHIutch committed Oct 26, 2023
1 parent 7c8f094 commit 24bbd63
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ interface Fathom {
enableTrackingForMe: () => void;
trackPageview: (opts?: PageViewOptions) => void;
trackGoal: (code: string, cents: number) => void;
trackEvent: (code: string, opts?: EventOptions) => void;
setSite: (id: string) => void;
}

Expand All @@ -11,6 +12,11 @@ export type PageViewOptions = {
referrer?: string;
};

export type EventOptions = {
_value?: number,
_site_id?: string
}

// refer to https://usefathom.com/support/tracking-advanced
export type LoadOptions = {
url?: string;
Expand All @@ -25,6 +31,7 @@ export type LoadOptions = {
type FathomCommand =
| { type: 'trackPageview'; opts: PageViewOptions | undefined }
| { type: 'trackGoal'; code: string; cents: number }
| { type: 'trackEvent'; event_name: string; opts: EventOptions | undefined }
| { type: 'blockTrackingForMe' }
| { type: 'enableTrackingForMe' }
| { type: 'setSite'; id: string };
Expand Down Expand Up @@ -61,6 +68,10 @@ const flushQueue = (): void => {
trackGoal(command.code, command.cents);
return;

case 'trackEvent':
trackEvent(command.event_name, command.opts);
return;

case 'enableTrackingForMe':
enableTrackingForMe();
return;
Expand Down Expand Up @@ -155,6 +166,8 @@ export const trackPageview = (opts?: PageViewOptions): void => {
*
* @param code - The goal ID.
* @param cents - The value in cents.
* @deprecated If you are using this to track an existing goal, however, it will continue to work.
* @see https://usefathom.com/docs/features/events
*/
export const trackGoal = (code: string, cents: number) => {
if (window.fathom) {
Expand All @@ -164,6 +177,22 @@ export const trackGoal = (code: string, cents: number) => {
}
};

/**
* Tracks a dynamic event.
*
* @param event_name - This can be nearly anything you want. Avoid special chars and emojis
* @param _value - The value in cents.
* @param _site_id - The value in cents.
* @see https://usefathom.com/docs/features/events
*/
export const trackEvent = (event_name: string, opts?: EventOptions) => {
if (window.fathom) {
window.fathom.trackEvent(event_name, opts);
} else {
enqueue({ type: 'trackEvent', event_name, opts });
}
};

/**
* Blocks tracking for the current visitor.
*
Expand Down

0 comments on commit 24bbd63

Please sign in to comment.