mirror of https://github.com/Budibase/budibase.git
7 changed files with 114 additions and 53 deletions
@ -0,0 +1,35 @@ |
|||
const PostHog = require("posthog-node") |
|||
const path = require("path") |
|||
const fs = require("fs") |
|||
const os = require("os") |
|||
const { BUDIBASE_POSTHOG_URL, BUDIBASE_POSTHOG_TOKEN, AnalyticsEvents } = require("../constants") |
|||
const ConfigManager = require("../structures/ConfigManager") |
|||
|
|||
class AnalyticsClient { |
|||
constructor() { |
|||
this.client = new PostHog(BUDIBASE_POSTHOG_TOKEN, { host: BUDIBASE_POSTHOG_URL }) |
|||
this.configManager = new ConfigManager() |
|||
} |
|||
|
|||
capture(event) { |
|||
if (this.manager.config.analyticsDisabled) return |
|||
|
|||
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" |
|||
} |
|||
} |
|||
|
|||
module.exports = AnalyticsClient |
|||
@ -0,0 +1,45 @@ |
|||
const fs = require("fs") |
|||
const path = require("path") |
|||
const os = require("os") |
|||
const { error } = require("../utils") |
|||
|
|||
class ConfigManager { |
|||
constructor() { |
|||
this.path = path.join(os.homedir(), ".budibase.json") |
|||
if (!fs.existsSync(this.path)) { |
|||
fs.writeFileSync(this.path, "{}") |
|||
} |
|||
} |
|||
|
|||
get config() { |
|||
try { |
|||
return JSON.parse(fs.readFileSync(this.path, "utf8")) |
|||
} catch (err) { |
|||
console.log(error(("Error parsing configuration file. Please check your .budibase.json is valid."))) |
|||
} |
|||
} |
|||
|
|||
set config(json) { |
|||
fs.writeFileSync(this.path, JSON.stringify(json)) |
|||
} |
|||
|
|||
getValue(key) { |
|||
return this.config[key] |
|||
} |
|||
|
|||
setValue(key, value) { |
|||
const updated = { |
|||
...this.config, |
|||
[key]: value |
|||
} |
|||
this.config = updated |
|||
} |
|||
|
|||
removeKey(key) { |
|||
const updated = { ...this.config } |
|||
delete updated[key] |
|||
this.config = updated |
|||
} |
|||
} |
|||
|
|||
module.exports = ConfigManager |
|||
Loading…
Reference in new issue