1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-19 21:40:33 +09:00
namae/web/src/util/analytics.ts

84 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-09-24 13:55:07 +09:00
import ReactGA from 'react-ga';
import * as Sentry from '@sentry/browser';
2020-03-26 20:22:06 +09:00
import {History} from 'history';
2019-09-24 13:55:07 +09:00
const isProduction = process.env.NODE_ENV !== 'development';
2020-03-26 20:22:06 +09:00
export function wrapHistoryWithGA(history: History) {
2019-09-24 13:55:07 +09:00
if (isProduction) {
ReactGA.initialize('UA-28919359-15');
ReactGA.pageview(window.location.pathname + window.location.search);
2020-03-06 00:35:37 +09:00
history.listen((location) => {
2020-03-07 12:06:55 +09:00
ReactGA.pageview(location.pathname + location.search);
2020-03-06 00:35:37 +09:00
});
2019-09-24 13:55:07 +09:00
}
2020-03-06 00:35:37 +09:00
return history;
2019-09-24 13:55:07 +09:00
}
2020-03-07 12:06:55 +09:00
export function trackEvent({
2020-03-05 22:09:12 +09:00
category,
action,
label = undefined,
value = undefined,
}: {
category: string;
action: string;
label?: string;
value?: number;
}) {
2019-09-24 13:55:07 +09:00
if (isProduction) {
ReactGA.event({
2020-03-05 22:09:12 +09:00
category,
action,
label,
value,
2019-09-24 13:55:07 +09:00
});
}
}
2020-03-05 22:09:12 +09:00
export function sendQueryEvent(query: string): void {
2020-03-07 12:34:47 +09:00
trackEvent({category: 'Search', action: 'Invoke New Search', label: query});
2020-03-05 22:09:12 +09:00
}
2020-03-07 12:24:59 +09:00
export function sendGettingStartedEvent(): void {
trackEvent({category: 'Search', action: 'Getting Started'});
2020-03-05 22:09:12 +09:00
}
export function sendExpandEvent(): void {
2020-03-07 12:24:59 +09:00
trackEvent({category: 'Result', action: 'Expand Card'});
2020-03-05 22:09:12 +09:00
}
export function sendAcceptSuggestionEvent(): void {
2020-03-07 12:24:59 +09:00
trackEvent({category: 'Suggestion', action: 'Accept'});
2020-03-05 22:09:12 +09:00
}
export function sendShuffleSuggestionEvent(): void {
2020-03-07 12:24:59 +09:00
trackEvent({category: 'Suggestion', action: 'Shuffle'});
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',
});
}
}
2019-12-24 01:57:07 +09:00
export function sendError(
error: Error,
errorInfo: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
},
): Promise<string> {
2019-09-24 13:55:07 +09:00
return new Promise((resolve) => {
if (isProduction) {
Sentry.withScope((scope) => {
scope.setExtras(errorInfo);
const eventId = Sentry.captureException(error);
resolve(eventId);
});
}
});
}