This commit is contained in:
10
lib/services/analytics/analytics-service.ts
Normal file
10
lib/services/analytics/analytics-service.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export type AnalyticsEventProperties = Record<
|
||||
string,
|
||||
string | number | boolean | null | undefined
|
||||
>;
|
||||
|
||||
export interface AnalyticsService {
|
||||
track(eventName: string, props?: AnalyticsEventProperties): void;
|
||||
trackPageview(url?: string): void;
|
||||
}
|
||||
|
||||
11
lib/services/analytics/noop-analytics-service.ts
Normal file
11
lib/services/analytics/noop-analytics-service.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import type { AnalyticsEventProperties, AnalyticsService } from './analytics-service';
|
||||
|
||||
export class NoopAnalyticsService implements AnalyticsService {
|
||||
track(_eventName: string, _props?: AnalyticsEventProperties) {
|
||||
// intentionally noop
|
||||
}
|
||||
|
||||
trackPageview(_url?: string) {
|
||||
// intentionally noop
|
||||
}
|
||||
}
|
||||
32
lib/services/analytics/umami-analytics-service.ts
Normal file
32
lib/services/analytics/umami-analytics-service.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user