From 6102ace3a98032cd39f6b7abe4fafc54a62791fa Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Tue, 2 Aug 2022 10:52:09 +0100 Subject: [PATCH] Reduce event frequency --- .../src/events/processors/PosthogProcessor.ts | 21 ++++++++++ .../processors/tests/PosthogProcessor.spec.ts | 40 +++++++++++++++++++ .../src/events/publishers/license.ts | 6 --- .../tests/utilities/mocks/index.js | 2 + .../tests/utilities/mocks/posthog.ts | 7 ++++ .../builder/src/analytics/PosthogClient.js | 10 ++++- packages/builder/src/stores/portal/auth.js | 5 +++ packages/types/src/sdk/events/event.ts | 1 - 8 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 packages/backend-core/src/events/processors/tests/PosthogProcessor.spec.ts create mode 100644 packages/backend-core/tests/utilities/mocks/posthog.ts diff --git a/packages/backend-core/src/events/processors/PosthogProcessor.ts b/packages/backend-core/src/events/processors/PosthogProcessor.ts index eb12db1dc4..9d68d3919a 100644 --- a/packages/backend-core/src/events/processors/PosthogProcessor.ts +++ b/packages/backend-core/src/events/processors/PosthogProcessor.ts @@ -5,6 +5,22 @@ import env from "../../environment" import * as context from "../../context" const pkg = require("../../../package.json") +const EXCLUDED_EVENTS: Event[] = [ + Event.USER_UPDATED, + Event.EMAIL_SMTP_UPDATED, + Event.AUTH_SSO_UPDATED, + Event.APP_UPDATED, + Event.ROLE_UPDATED, + Event.DATASOURCE_UPDATED, + Event.QUERY_UPDATED, + Event.TABLE_UPDATED, + Event.VIEW_UPDATED, + Event.VIEW_FILTER_UPDATED, + Event.VIEW_CALCULATION_UPDATED, + Event.AUTOMATION_TRIGGER_UPDATED, + Event.USER_GROUP_UPDATED, +] + export default class PosthogProcessor implements EventProcessor { posthog: PostHog @@ -21,6 +37,11 @@ export default class PosthogProcessor implements EventProcessor { properties: BaseEvent, timestamp?: string | number ): Promise { + // don't send excluded events + if (EXCLUDED_EVENTS.includes(event)) { + return + } + properties.version = pkg.version properties.service = env.SERVICE properties.environment = identity.environment diff --git a/packages/backend-core/src/events/processors/tests/PosthogProcessor.spec.ts b/packages/backend-core/src/events/processors/tests/PosthogProcessor.spec.ts new file mode 100644 index 0000000000..4a6d55b272 --- /dev/null +++ b/packages/backend-core/src/events/processors/tests/PosthogProcessor.spec.ts @@ -0,0 +1,40 @@ +import PosthogProcessor from "../PosthogProcessor" +import { Event, IdentityType, Hosting } from "@budibase/types" + +const newIdentity = () => { + return { + id: "test", + type: IdentityType.USER, + hosting: Hosting.SELF, + environment: "test", + } +} + +describe("PosthogProcessor", () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + describe("processEvent", () => { + it("processes event", () => { + const processor = new PosthogProcessor("test") + + const identity = newIdentity() + const properties = {} + + processor.processEvent(Event.APP_CREATED, identity, properties) + + expect(processor.posthog.capture).toHaveBeenCalledTimes(1) + }) + + it("honours exclusions", () => { + const processor = new PosthogProcessor("test") + + const identity = newIdentity() + const properties = {} + + processor.processEvent(Event.AUTH_SSO_UPDATED, identity, properties) + expect(processor.posthog.capture).toHaveBeenCalledTimes(0) + }) + }) +}) diff --git a/packages/backend-core/src/events/publishers/license.ts b/packages/backend-core/src/events/publishers/license.ts index 44dafd84ce..1adc71652e 100644 --- a/packages/backend-core/src/events/publishers/license.ts +++ b/packages/backend-core/src/events/publishers/license.ts @@ -20,12 +20,6 @@ export async function downgraded(license: License) { await publishEvent(Event.LICENSE_DOWNGRADED, properties) } -// TODO -export async function updated(license: License) { - const properties: LicenseUpdatedEvent = {} - await publishEvent(Event.LICENSE_UPDATED, properties) -} - // TODO export async function activated(license: License) { const properties: LicenseActivatedEvent = {} diff --git a/packages/backend-core/tests/utilities/mocks/index.js b/packages/backend-core/tests/utilities/mocks/index.js index 3dd5c854c0..6aa1c4a54f 100644 --- a/packages/backend-core/tests/utilities/mocks/index.js +++ b/packages/backend-core/tests/utilities/mocks/index.js @@ -1,7 +1,9 @@ +const posthog = require("./posthog") const events = require("./events") const date = require("./date") module.exports = { + posthog, date, events, } diff --git a/packages/backend-core/tests/utilities/mocks/posthog.ts b/packages/backend-core/tests/utilities/mocks/posthog.ts new file mode 100644 index 0000000000..e9cc653ccc --- /dev/null +++ b/packages/backend-core/tests/utilities/mocks/posthog.ts @@ -0,0 +1,7 @@ +jest.mock("posthog-node", () => { + return jest.fn().mockImplementation(() => { + return { + capture: jest.fn(), + } + }) +}) diff --git a/packages/builder/src/analytics/PosthogClient.js b/packages/builder/src/analytics/PosthogClient.js index 0b9f052906..c1e596358f 100644 --- a/packages/builder/src/analytics/PosthogClient.js +++ b/packages/builder/src/analytics/PosthogClient.js @@ -1,5 +1,7 @@ import posthog from "posthog-js" import { Events } from "./constants" +import { get } from "svelte/store" +import { admin } from "../stores/portal" export default class PosthogClient { constructor(token) { @@ -9,9 +11,15 @@ export default class PosthogClient { init() { if (!this.token) return + // enable page views in cloud only + let capturePageViews = false + if (get(admin).cloud) { + capturePageViews = true + } + posthog.init(this.token, { autocapture: false, - capture_pageview: true, + capture_pageview: capturePageViews, }) posthog.set_config({ persistence: "cookie" }) diff --git a/packages/builder/src/stores/portal/auth.js b/packages/builder/src/stores/portal/auth.js index d6f4fc140f..7075b40d51 100644 --- a/packages/builder/src/stores/portal/auth.js +++ b/packages/builder/src/stores/portal/auth.js @@ -131,6 +131,11 @@ export function createAuthStore() { await setOrganisation(tenantId) }, getSelf: async () => { + // for analytics, we need to make sure the environment has been loaded + // before setting the user + if (!get(admin).loaded) { + await admin.init() + } // We need to catch this locally as we never want this to fail, even // though normally we never want to swallow API errors at the store level. // We're either logged in or we aren't. diff --git a/packages/types/src/sdk/events/event.ts b/packages/types/src/sdk/events/event.ts index f0e023df51..d7086370ec 100644 --- a/packages/types/src/sdk/events/event.ts +++ b/packages/types/src/sdk/events/event.ts @@ -135,7 +135,6 @@ export enum Event { // LICENSE LICENSE_UPGRADED = "license:upgraded", LICENSE_DOWNGRADED = "license:downgraded", - LICENSE_UPDATED = "license:updated", LICENSE_ACTIVATED = "license:activated", // ACCOUNT