From 701c82cb1ff4f636025625bba9ee16af67b284c2 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Tue, 29 Sep 2020 16:23:34 +0100 Subject: [PATCH] serve determines whether analytics are enabled --- packages/builder/src/analytics.js | 11 +++++++++-- packages/server/src/api/controllers/analytics.js | 3 +++ packages/server/src/api/index.js | 4 ++++ packages/server/src/api/routes/analytics.js | 10 ++++++++++ packages/server/src/api/routes/index.js | 2 ++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/api/controllers/analytics.js create mode 100644 packages/server/src/api/routes/analytics.js diff --git a/packages/builder/src/analytics.js b/packages/builder/src/analytics.js index e51bb79875..64998efd61 100644 --- a/packages/builder/src/analytics.js +++ b/packages/builder/src/analytics.js @@ -2,9 +2,16 @@ import * as Sentry from "@sentry/browser" import posthog from "posthog-js" 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 Sentry.init({ dsn: process.env.SENTRY_DSN }) if (!process.env.POSTHOG_TOKEN) return diff --git a/packages/server/src/api/controllers/analytics.js b/packages/server/src/api/controllers/analytics.js new file mode 100644 index 0000000000..827f83c8ff --- /dev/null +++ b/packages/server/src/api/controllers/analytics.js @@ -0,0 +1,3 @@ +exports.isEnabled = async function(ctx) { + ctx.body = JSON.stringify(process.env.NODE_ENV === "production") +} diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index b7f156fb6a..0ab10e3e4d 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -19,6 +19,7 @@ const { automationRoutes, accesslevelRoutes, apiKeysRoutes, + analyticsRoutes, } = require("./routes") const router = new Router() @@ -109,6 +110,9 @@ router.use(accesslevelRoutes.allowedMethods()) router.use(apiKeysRoutes.routes()) router.use(apiKeysRoutes.allowedMethods()) +router.use(analyticsRoutes.routes()) +router.use(analyticsRoutes.allowedMethods()) + router.use(staticRoutes.routes()) router.use(staticRoutes.allowedMethods()) diff --git a/packages/server/src/api/routes/analytics.js b/packages/server/src/api/routes/analytics.js new file mode 100644 index 0000000000..626e3c2994 --- /dev/null +++ b/packages/server/src/api/routes/analytics.js @@ -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 diff --git a/packages/server/src/api/routes/index.js b/packages/server/src/api/routes/index.js index a2b8d3bb6c..0a5b0b1934 100644 --- a/packages/server/src/api/routes/index.js +++ b/packages/server/src/api/routes/index.js @@ -13,6 +13,7 @@ const automationRoutes = require("./automation") const accesslevelRoutes = require("./accesslevel") const deployRoutes = require("./deploy") const apiKeysRoutes = require("./apikeys") +const analyticsRoutes = require("./analytics") module.exports = { deployRoutes, @@ -30,4 +31,5 @@ module.exports = { automationRoutes, accesslevelRoutes, apiKeysRoutes, + analyticsRoutes, }