2020-07-15 03:00:58 +12:00
|
|
|
import * as Sentry from "@sentry/browser"
|
|
|
|
import posthog from "posthog-js"
|
2020-09-28 22:47:18 +13:00
|
|
|
import api from "builderStore/api"
|
2020-07-15 03:00:58 +12:00
|
|
|
|
2020-09-30 04:23:34 +13:00
|
|
|
let analyticsEnabled
|
2020-09-30 03:26:56 +13:00
|
|
|
|
2020-09-30 04:23:34 +13:00
|
|
|
async function activate() {
|
|
|
|
if (analyticsEnabled === undefined) {
|
|
|
|
// only the server knows the true NODE_ENV
|
|
|
|
// this was an issue as NODE_ENV = 'cypress' on the server,
|
|
|
|
// but 'production' on the client
|
|
|
|
const response = await api.get("/api/analytics")
|
|
|
|
analyticsEnabled = (await response.json()) === true
|
|
|
|
}
|
2020-09-30 03:26:56 +13:00
|
|
|
if (!analyticsEnabled) return
|
2020-07-15 03:00:58 +12:00
|
|
|
Sentry.init({ dsn: process.env.SENTRY_DSN })
|
2020-08-06 02:18:28 +12:00
|
|
|
if (!process.env.POSTHOG_TOKEN) return
|
2020-07-15 03:00:58 +12:00
|
|
|
posthog.init(process.env.POSTHOG_TOKEN, {
|
|
|
|
api_host: process.env.POSTHOG_URL,
|
|
|
|
})
|
2020-09-30 03:26:56 +13:00
|
|
|
posthog.set_config({ persistence: "cookie" })
|
2020-07-15 03:00:58 +12:00
|
|
|
}
|
|
|
|
|
2020-09-28 22:47:18 +13:00
|
|
|
function identify(id) {
|
2020-09-30 03:26:56 +13:00
|
|
|
if (!analyticsEnabled) return
|
2020-09-28 22:47:18 +13:00
|
|
|
if (!id) return
|
|
|
|
posthog.identify(id)
|
|
|
|
Sentry.configureScope(scope => {
|
|
|
|
scope.setUser({ id: id })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function identifyByApiKey(apiKey) {
|
2020-09-30 03:26:56 +13:00
|
|
|
if (!analyticsEnabled) return true
|
2020-09-28 22:47:18 +13:00
|
|
|
const response = await fetch(
|
|
|
|
`https://03gaine137.execute-api.eu-west-1.amazonaws.com/prod/account/id?api_key=${apiKey.trim()}`
|
|
|
|
)
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
const id = await response.json()
|
|
|
|
|
|
|
|
await api.put("/api/keys/userId", { value: id })
|
|
|
|
identify(id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-15 03:00:58 +12:00
|
|
|
function captureException(err) {
|
2020-09-30 03:26:56 +13:00
|
|
|
if (!analyticsEnabled) return
|
2020-07-15 03:00:58 +12:00
|
|
|
Sentry.captureException(err)
|
2020-09-30 04:35:47 +13:00
|
|
|
captureEvent("Error", { error: err.message ? err.message : err })
|
2020-07-15 03:00:58 +12:00
|
|
|
}
|
|
|
|
|
2020-09-30 03:26:56 +13:00
|
|
|
function captureEvent(eventName, props = {}) {
|
|
|
|
if (!analyticsEnabled || !process.env.POSTHOG_TOKEN) return
|
|
|
|
props.sourceApp = "builder"
|
|
|
|
posthog.capture(eventName, props)
|
2020-07-16 04:25:08 +12:00
|
|
|
}
|
|
|
|
|
2020-07-15 03:00:58 +12:00
|
|
|
export default {
|
|
|
|
activate,
|
2020-09-28 22:47:18 +13:00
|
|
|
identify,
|
|
|
|
identifyByApiKey,
|
2020-07-15 03:00:58 +12:00
|
|
|
captureException,
|
2020-07-17 02:19:46 +12:00
|
|
|
captureEvent,
|
2020-07-15 03:00:58 +12:00
|
|
|
}
|