1
0
mirror of https://github.com/uetchy/namae.git synced 2025-07-06 16:05:59 +09:00
namae/src/util/analytics.ts

71 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-08-31 08:41:53 +09:00
import * as Sentry from '@sentry/browser';
2019-09-24 13:55:07 +09:00
2020-08-31 08:41:53 +09:00
const isProduction = process.env.NODE_ENV !== 'development';
2019-09-24 13:55:07 +09:00
2021-10-07 19:03:51 +09:00
declare namespace umami {
var trackEvent: (
value: string,
type: string,
url?: string,
websiteId?: string
) => void;
2019-09-24 13:55:07 +09:00
}
2020-03-07 12:06:55 +09:00
export function trackEvent({
2021-10-07 19:03:51 +09:00
value,
type = 'custom',
2020-03-05 22:09:12 +09:00
}: {
2021-10-07 19:03:51 +09:00
value: string;
type?: string;
2020-03-05 22:09:12 +09:00
}) {
2019-09-24 13:55:07 +09:00
if (isProduction) {
2021-10-07 19:03:51 +09:00
umami.trackEvent(value, type);
2019-09-24 13:55:07 +09:00
}
}
2020-03-05 22:09:12 +09:00
export function sendQueryEvent(query: string): void {
2021-10-07 19:03:51 +09:00
trackEvent({ value: 'Invoke new search', type: 'search' });
2020-03-05 22:09:12 +09:00
}
2020-03-07 12:24:59 +09:00
export function sendGettingStartedEvent(): void {
2021-10-07 19:03:51 +09:00
trackEvent({ value: 'Click getting started button', type: 'search' });
2020-03-05 22:09:12 +09:00
}
export function sendExpandEvent(): void {
2021-10-07 19:03:51 +09:00
trackEvent({ value: 'Expand card', type: 'search' });
2020-03-05 22:09:12 +09:00
}
export function sendAcceptSuggestionEvent(): void {
2021-10-07 19:03:51 +09:00
trackEvent({ value: 'Accept suggestion', type: 'suggest' });
2020-03-05 22:09:12 +09:00
}
export function sendShuffleSuggestionEvent(): void {
2021-10-07 19:03:51 +09:00
trackEvent({ value: 'Shuffle suggestion', type: 'suggest' });
2020-03-05 22:09:12 +09:00
}
2019-12-24 01:57:07 +09:00
export function initSentry(): void {
2019-09-24 13:55:07 +09:00
if (isProduction) {
Sentry.init({
dsn: 'https://7ab2df74aead499b950ebef190cc40b7@sentry.io/1759299',
2020-08-31 08:41:53 +09:00
});
2019-09-24 13:55:07 +09:00
}
}
2019-12-24 01:57:07 +09:00
export function sendError(
error: Error,
errorInfo: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2020-08-31 08:41:53 +09:00
[key: string]: any;
2020-08-20 00:57:33 +09:00
}
2019-12-24 01:57:07 +09:00
): Promise<string> {
2019-09-24 13:55:07 +09:00
return new Promise((resolve) => {
if (isProduction) {
Sentry.withScope((scope) => {
2020-08-31 08:41:53 +09:00
scope.setExtras(errorInfo);
const eventId = Sentry.captureException(error);
resolve(eventId);
});
2019-09-24 13:55:07 +09:00
}
2020-08-31 08:41:53 +09:00
});
2019-09-24 13:55:07 +09:00
}