mirror of https://github.com/Budibase/budibase.git
9 changed files with 251 additions and 3 deletions
@ -0,0 +1,38 @@ |
|||||
|
const PostHog = require("posthog-node") |
||||
|
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,63 @@ |
|||||
|
const Command = require("../structures/Command") |
||||
|
const { CommandWords } = require("../constants") |
||||
|
const { success, error } = require("../utils") |
||||
|
const AnalyticsClient = require("./Client") |
||||
|
|
||||
|
const client = new AnalyticsClient() |
||||
|
|
||||
|
async function optOut() { |
||||
|
try { |
||||
|
// opt them out
|
||||
|
client.disable() |
||||
|
console.log( |
||||
|
success( |
||||
|
"Successfully opted out of budibase analytics. You can opt in at any time by running 'budi analytics opt-in'" |
||||
|
) |
||||
|
) |
||||
|
} catch (err) { |
||||
|
console.log( |
||||
|
error( |
||||
|
"Error opting out of budibase analytics. Please try again later.", |
||||
|
err |
||||
|
) |
||||
|
) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
async function optIn() { |
||||
|
try { |
||||
|
// opt them in
|
||||
|
client.enable() |
||||
|
console.log( |
||||
|
success( |
||||
|
"Successfully opted in to budibase analytics. Thank you for helping us make budibase better!" |
||||
|
) |
||||
|
) |
||||
|
} catch (err) { |
||||
|
console.log( |
||||
|
error("Error opting in to budibase analytics. Please try again later.") |
||||
|
) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
async function status() { |
||||
|
try { |
||||
|
console.log(success(`Budibase analytics ${client.status()}`)) |
||||
|
} catch (err) { |
||||
|
console.log( |
||||
|
error("Error fetching analytics status. Please try again later.") |
||||
|
) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
const command = new Command(`${CommandWords.ANALYTICS}`) |
||||
|
.addHelp("Control the analytics you send to budibase.") |
||||
|
.addSubOption("--optin", "Opt in to sending analytics to budibase", optIn) |
||||
|
.addSubOption("--optout", "Opt out of sending analytics to budibase.", optOut) |
||||
|
.addSubOption( |
||||
|
"--status", |
||||
|
"Check whether you are currently opted in to budibase analytics.", |
||||
|
status |
||||
|
) |
||||
|
|
||||
|
exports.command = command |
||||
@ -1,5 +1,6 @@ |
|||||
|
const analytics = require("./analytics") |
||||
const hosting = require("./hosting") |
const hosting = require("./hosting") |
||||
|
|
||||
exports.getCommands = () => { |
exports.getCommands = () => { |
||||
return [hosting.command] |
return [hosting.command, analytics.command] |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,50 @@ |
|||||
|
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." |
||||
|
) |
||||
|
) |
||||
|
return {} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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