budibase/packages/cli/src/analytics/Client.js

39 lines
950 B
JavaScript
Raw Normal View History

2021-03-30 11:50:42 +02:00
const PostHog = require("posthog-node")
2021-03-30 12:50:49 +02:00
const {
BUDIBASE_POSTHOG_URL,
BUDIBASE_POSTHOG_TOKEN,
AnalyticsEvents,
} = require("../constants")
2021-03-30 11:50:42 +02:00
const ConfigManager = require("../structures/ConfigManager")
class AnalyticsClient {
constructor() {
2021-03-30 12:50:49 +02:00
this.client = new PostHog(BUDIBASE_POSTHOG_TOKEN, {
host: BUDIBASE_POSTHOG_URL,
})
2021-03-30 11:50:42 +02:00
this.configManager = new ConfigManager()
}
capture(event) {
2021-05-20 11:26:23 +02:00
if (this.configManager.config.analyticsDisabled) return
2021-03-30 11:50:42 +02:00
this.client.capture(event)
}
enable() {
this.configManager.removeKey("analyticsDisabled")
this.client.capture({ event: AnalyticsEvents.OptIn, distinctId: "cli" })
}
disable() {
this.client.capture({ event: AnalyticsEvents.OptOut, distinctId: "cli" })
this.configManager.setValue("analyticsDisabled", true)
}
status() {
return this.configManager.config.analyticsDisabled ? "disabled" : "enabled"
}
}
2021-03-30 12:50:49 +02:00
module.exports = AnalyticsClient