2020-07-14 17:00:58 +02:00
|
|
|
import * as Sentry from "@sentry/browser"
|
|
|
|
import posthog from "posthog-js"
|
2020-09-28 11:47:18 +02:00
|
|
|
import api from "builderStore/api"
|
2020-07-14 17:00:58 +02:00
|
|
|
|
2020-09-29 17:23:34 +02:00
|
|
|
let analyticsEnabled
|
2020-10-01 10:29:30 +02:00
|
|
|
const posthogConfigured = process.env.POSTHOG_TOKEN && process.env.POSTHOG_URL
|
|
|
|
const sentryConfigured = process.env.SENTRY_DSN
|
2020-09-29 16:26:56 +02:00
|
|
|
|
2020-10-23 23:38:40 +02:00
|
|
|
const FEEDBACK_SUBMITTED_KEY = "budibase:feedback_submitted"
|
|
|
|
const APP_FIRST_STARTED_KEY = "budibase:first_run"
|
|
|
|
const feedbackHours = 12
|
|
|
|
|
2020-09-29 17:23:34 +02: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")
|
2021-03-10 12:47:39 +01:00
|
|
|
analyticsEnabled = (await response.json()).enabled === true
|
2020-09-29 17:23:34 +02:00
|
|
|
}
|
2020-09-29 16:26:56 +02:00
|
|
|
if (!analyticsEnabled) return
|
2020-10-01 10:29:30 +02:00
|
|
|
if (sentryConfigured) Sentry.init({ dsn: process.env.SENTRY_DSN })
|
|
|
|
if (posthogConfigured) {
|
|
|
|
posthog.init(process.env.POSTHOG_TOKEN, {
|
|
|
|
api_host: process.env.POSTHOG_URL,
|
|
|
|
})
|
|
|
|
posthog.set_config({ persistence: "cookie" })
|
|
|
|
}
|
2020-07-14 17:00:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 11:47:18 +02:00
|
|
|
function identify(id) {
|
2020-10-01 10:29:30 +02:00
|
|
|
if (!analyticsEnabled || !id) return
|
|
|
|
if (posthogConfigured) posthog.identify(id)
|
|
|
|
if (sentryConfigured)
|
2021-05-03 09:31:09 +02:00
|
|
|
Sentry.configureScope((scope) => {
|
2020-10-01 10:29:30 +02:00
|
|
|
scope.setUser({ id: id })
|
|
|
|
})
|
2020-09-28 11:47:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function identifyByApiKey(apiKey) {
|
2020-09-29 16:26:56 +02:00
|
|
|
if (!analyticsEnabled) return true
|
2020-12-16 11:35:12 +01:00
|
|
|
try {
|
|
|
|
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()
|
2020-12-16 15:22:31 +01:00
|
|
|
|
2020-12-16 11:35:12 +01:00
|
|
|
await api.put("/api/keys/userId", { value: id })
|
|
|
|
identify(id)
|
|
|
|
return true
|
|
|
|
}
|
2020-12-16 15:22:31 +01:00
|
|
|
|
2020-12-16 11:35:12 +01:00
|
|
|
return false
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
2020-09-28 11:47:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 17:00:58 +02:00
|
|
|
function captureException(err) {
|
2020-09-29 16:26:56 +02:00
|
|
|
if (!analyticsEnabled) return
|
2020-07-14 17:00:58 +02:00
|
|
|
Sentry.captureException(err)
|
2020-09-29 17:35:47 +02:00
|
|
|
captureEvent("Error", { error: err.message ? err.message : err })
|
2020-07-14 17:00:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:26:56 +02:00
|
|
|
function captureEvent(eventName, props = {}) {
|
|
|
|
if (!analyticsEnabled || !process.env.POSTHOG_TOKEN) return
|
|
|
|
props.sourceApp = "builder"
|
|
|
|
posthog.capture(eventName, props)
|
2020-07-15 18:25:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-23 23:38:40 +02:00
|
|
|
if (!localStorage.getItem(APP_FIRST_STARTED_KEY)) {
|
|
|
|
localStorage.setItem(APP_FIRST_STARTED_KEY, Date.now())
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
const isFeedbackTimeElapsed = (sinceDateStr) => {
|
2020-10-23 23:38:40 +02:00
|
|
|
const sinceDate = parseFloat(sinceDateStr)
|
|
|
|
const feedbackMilliseconds = feedbackHours * 60 * 60 * 1000
|
|
|
|
return Date.now() > sinceDate + feedbackMilliseconds
|
|
|
|
}
|
|
|
|
function submitFeedback(values) {
|
|
|
|
if (!analyticsEnabled || !process.env.POSTHOG_TOKEN) return
|
|
|
|
localStorage.setItem(FEEDBACK_SUBMITTED_KEY, Date.now())
|
|
|
|
|
|
|
|
const prefixedValues = Object.entries(values).reduce((obj, [key, value]) => {
|
|
|
|
obj[`feedback_${key}`] = value
|
|
|
|
return obj
|
|
|
|
}, {})
|
|
|
|
|
|
|
|
posthog.capture("Feedback Submitted", prefixedValues)
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestFeedbackOnDeploy() {
|
|
|
|
if (!analyticsEnabled || !process.env.POSTHOG_TOKEN) return false
|
|
|
|
const lastSubmittedStr = localStorage.getItem(FEEDBACK_SUBMITTED_KEY)
|
|
|
|
if (!lastSubmittedStr) return true
|
|
|
|
return isFeedbackTimeElapsed(lastSubmittedStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
function highlightFeedbackIcon() {
|
|
|
|
if (!analyticsEnabled || !process.env.POSTHOG_TOKEN) return false
|
|
|
|
const lastSubmittedStr = localStorage.getItem(FEEDBACK_SUBMITTED_KEY)
|
|
|
|
if (lastSubmittedStr) return isFeedbackTimeElapsed(lastSubmittedStr)
|
|
|
|
const firstRunStr = localStorage.getItem(APP_FIRST_STARTED_KEY)
|
|
|
|
if (!firstRunStr) return false
|
|
|
|
return isFeedbackTimeElapsed(firstRunStr)
|
|
|
|
}
|
|
|
|
|
2021-01-28 20:32:13 +01:00
|
|
|
// Opt In/Out
|
2021-05-03 09:31:09 +02:00
|
|
|
const ifAnalyticsEnabled = (func) => () => {
|
2021-01-28 20:32:13 +01:00
|
|
|
if (analyticsEnabled && process.env.POSTHOG_TOKEN) {
|
|
|
|
return func()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const disabled = () => posthog.has_opted_out_capturing()
|
|
|
|
const optIn = () => posthog.opt_in_capturing()
|
|
|
|
const optOut = () => posthog.opt_out_capturing()
|
|
|
|
|
2020-07-14 17:00:58 +02:00
|
|
|
export default {
|
|
|
|
activate,
|
2020-09-28 11:47:18 +02:00
|
|
|
identify,
|
|
|
|
identifyByApiKey,
|
2020-07-14 17:00:58 +02:00
|
|
|
captureException,
|
2020-07-16 16:19:46 +02:00
|
|
|
captureEvent,
|
2020-10-23 23:38:40 +02:00
|
|
|
requestFeedbackOnDeploy,
|
|
|
|
submitFeedback,
|
|
|
|
highlightFeedbackIcon,
|
2021-02-23 17:50:41 +01:00
|
|
|
disabled: () => {
|
|
|
|
if (analyticsEnabled == null) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ifAnalyticsEnabled(disabled)
|
|
|
|
},
|
2021-01-28 20:32:13 +01:00
|
|
|
optIn: ifAnalyticsEnabled(optIn),
|
|
|
|
optOut: ifAnalyticsEnabled(optOut),
|
2020-07-14 17:00:58 +02:00
|
|
|
}
|