33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import type { AnalyticsEventProperties, AnalyticsService } from './analytics-service';
|
|
|
|
type UmamiGlobal = {
|
|
track?: (eventOrUrl: string, props?: AnalyticsEventProperties) => void;
|
|
};
|
|
|
|
export type UmamiAnalyticsServiceOptions = {
|
|
enabled: boolean;
|
|
};
|
|
|
|
export class UmamiAnalyticsService implements AnalyticsService {
|
|
constructor(private readonly options: UmamiAnalyticsServiceOptions) {}
|
|
|
|
track(eventName: string, props?: AnalyticsEventProperties) {
|
|
if (!this.options.enabled) return;
|
|
if (typeof window === 'undefined') return;
|
|
|
|
const umami = (window as unknown as { umami?: UmamiGlobal }).umami;
|
|
umami?.track?.(eventName, props);
|
|
}
|
|
|
|
trackPageview(url?: string) {
|
|
if (!this.options.enabled) return;
|
|
if (typeof window === 'undefined') return;
|
|
|
|
const umami = (window as unknown as { umami?: UmamiGlobal }).umami;
|
|
|
|
// Umami treats `track(url)` as a pageview override.
|
|
if (url) umami?.track?.(url);
|
|
else umami?.track?.(window.location.pathname + window.location.search);
|
|
}
|
|
}
|