mirror of https://github.com/Budibase/budibase.git
committed by
GitHub
23 changed files with 149 additions and 24 deletions
@ -1,25 +1,68 @@ |
|||||
import * as Sentry from "@sentry/browser" |
import * as Sentry from "@sentry/browser" |
||||
import posthog from "posthog-js" |
import posthog from "posthog-js" |
||||
|
import api from "builderStore/api" |
||||
|
|
||||
function activate() { |
let analyticsEnabled |
||||
|
|
||||
|
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 |
||||
|
} |
||||
|
if (!analyticsEnabled) return |
||||
Sentry.init({ dsn: process.env.SENTRY_DSN }) |
Sentry.init({ dsn: process.env.SENTRY_DSN }) |
||||
if (!process.env.POSTHOG_TOKEN) return |
if (!process.env.POSTHOG_TOKEN) return |
||||
posthog.init(process.env.POSTHOG_TOKEN, { |
posthog.init(process.env.POSTHOG_TOKEN, { |
||||
api_host: process.env.POSTHOG_URL, |
api_host: process.env.POSTHOG_URL, |
||||
}) |
}) |
||||
|
posthog.set_config({ persistence: "cookie" }) |
||||
|
} |
||||
|
|
||||
|
function identify(id) { |
||||
|
if (!analyticsEnabled) return |
||||
|
if (!id) return |
||||
|
posthog.identify(id) |
||||
|
Sentry.configureScope(scope => { |
||||
|
scope.setUser({ id: id }) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
async function identifyByApiKey(apiKey) { |
||||
|
if (!analyticsEnabled) return true |
||||
|
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 |
||||
} |
} |
||||
|
|
||||
function captureException(err) { |
function captureException(err) { |
||||
|
if (!analyticsEnabled) return |
||||
Sentry.captureException(err) |
Sentry.captureException(err) |
||||
|
captureEvent("Error", { error: err.message ? err.message : err }) |
||||
} |
} |
||||
|
|
||||
function captureEvent(event) { |
function captureEvent(eventName, props = {}) { |
||||
if (!process.env.POSTHOG_TOKEN) return |
if (!analyticsEnabled || !process.env.POSTHOG_TOKEN) return |
||||
posthog.capture(event) |
props.sourceApp = "builder" |
||||
|
posthog.capture(eventName, props) |
||||
} |
} |
||||
|
|
||||
export default { |
export default { |
||||
activate, |
activate, |
||||
|
identify, |
||||
|
identifyByApiKey, |
||||
captureException, |
captureException, |
||||
captureEvent, |
captureEvent, |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,3 @@ |
|||||
|
exports.isEnabled = async function(ctx) { |
||||
|
ctx.body = JSON.stringify(process.env.ENABLE_ANALYTICS === "true") |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
const Router = require("@koa/router") |
||||
|
const authorized = require("../../middleware/authorized") |
||||
|
const { BUILDER } = require("../../utilities/accessLevels") |
||||
|
const controller = require("../controllers/analytics") |
||||
|
|
||||
|
const router = Router() |
||||
|
|
||||
|
router.get("/api/analytics", authorized(BUILDER), controller.isEnabled) |
||||
|
|
||||
|
module.exports = router |
||||
Loading…
Reference in new issue