serve determines whether analytics are enabled

This commit is contained in:
Michael Shanks 2020-09-29 16:23:34 +01:00
parent c336605529
commit 701c82cb1f
5 changed files with 28 additions and 2 deletions

View File

@ -2,9 +2,16 @@ import * as Sentry from "@sentry/browser"
import posthog from "posthog-js" import posthog from "posthog-js"
import api from "builderStore/api" import api from "builderStore/api"
const analyticsEnabled = process.env.NODE_ENV === "production" let analyticsEnabled
function activate() { 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 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

View File

@ -0,0 +1,3 @@
exports.isEnabled = async function(ctx) {
ctx.body = JSON.stringify(process.env.NODE_ENV === "production")
}

View File

@ -19,6 +19,7 @@ const {
automationRoutes, automationRoutes,
accesslevelRoutes, accesslevelRoutes,
apiKeysRoutes, apiKeysRoutes,
analyticsRoutes,
} = require("./routes") } = require("./routes")
const router = new Router() const router = new Router()
@ -109,6 +110,9 @@ router.use(accesslevelRoutes.allowedMethods())
router.use(apiKeysRoutes.routes()) router.use(apiKeysRoutes.routes())
router.use(apiKeysRoutes.allowedMethods()) router.use(apiKeysRoutes.allowedMethods())
router.use(analyticsRoutes.routes())
router.use(analyticsRoutes.allowedMethods())
router.use(staticRoutes.routes()) router.use(staticRoutes.routes())
router.use(staticRoutes.allowedMethods()) router.use(staticRoutes.allowedMethods())

View File

@ -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

View File

@ -13,6 +13,7 @@ const automationRoutes = require("./automation")
const accesslevelRoutes = require("./accesslevel") const accesslevelRoutes = require("./accesslevel")
const deployRoutes = require("./deploy") const deployRoutes = require("./deploy")
const apiKeysRoutes = require("./apikeys") const apiKeysRoutes = require("./apikeys")
const analyticsRoutes = require("./analytics")
module.exports = { module.exports = {
deployRoutes, deployRoutes,
@ -30,4 +31,5 @@ module.exports = {
automationRoutes, automationRoutes,
accesslevelRoutes, accesslevelRoutes,
apiKeysRoutes, apiKeysRoutes,
analyticsRoutes,
} }