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

84 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-08-20 00:57:33 +09:00
import ReactGA from 'react-ga'
import * as Sentry from '@sentry/browser'
import { History } from 'history'
2019-09-24 13:55:07 +09:00
2020-08-20 00:57:33 +09:00
const isProduction = process.env.NODE_ENV !== 'development'
2019-09-24 13:55:07 +09:00
2020-03-26 20:22:06 +09:00
export function wrapHistoryWithGA(history: History) {
2019-09-24 13:55:07 +09:00
if (isProduction) {
2020-08-20 00:57:33 +09:00
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-08-20 00:57:33 +09:00
ReactGA.pageview(location.pathname + location.search)
})
2019-09-24 13:55:07 +09:00
}
2020-08-20 00:57:33 +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,
}: {
2020-08-20 00:57:33 +09:00
category: string
action: string
label?: string
value?: number
2020-03-05 22:09:12 +09:00
}) {
2019-09-24 13:55:07 +09:00
if (isProduction) {
ReactGA.event({
2020-03-05 22:09:12 +09:00
category,
action,
label,
value,
2020-08-20 00:57:33 +09:00
})
2019-09-24 13:55:07 +09:00
}
}
2020-03-05 22:09:12 +09:00
export function sendQueryEvent(query: string): void {
2020-08-20 00:57:33 +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 {
2020-08-20 00:57:33 +09:00
trackEvent({ category: 'Search', action: 'Getting Started' })
2020-03-05 22:09:12 +09:00
}
export function sendExpandEvent(): void {
2020-08-20 00:57:33 +09:00
trackEvent({ category: 'Result', action: 'Expand Card' })
2020-03-05 22:09:12 +09:00
}
export function sendAcceptSuggestionEvent(): void {
2020-08-20 00:57:33 +09:00
trackEvent({ category: 'Suggestion', action: 'Accept' })
2020-03-05 22:09:12 +09:00
}
export function sendShuffleSuggestionEvent(): void {
2020-08-20 00:57:33 +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',
2020-08-20 00:57:33 +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-20 00:57:33 +09:00
[key: string]: any
}
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-20 00:57:33 +09:00
scope.setExtras(errorInfo)
const eventId = Sentry.captureException(error)
resolve(eventId)
})
2019-09-24 13:55:07 +09:00
}
2020-08-20 00:57:33 +09:00
})
2019-09-24 13:55:07 +09:00
}