Browse Source
Merge pull request #7135 from Budibase/posthog-reduction
Reduce event frequency
pull/7143/head
Rory Powell
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with
84 additions and
8 deletions
-
packages/backend-core/src/events/processors/PosthogProcessor.ts
-
packages/backend-core/src/events/processors/tests/PosthogProcessor.spec.ts
-
packages/backend-core/src/events/publishers/license.ts
-
packages/backend-core/tests/utilities/mocks/index.js
-
packages/backend-core/tests/utilities/mocks/posthog.ts
-
packages/builder/src/analytics/PosthogClient.js
-
packages/builder/src/stores/portal/auth.js
-
packages/types/src/sdk/events/event.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<void> { |
|
|
|
// don't send excluded events
|
|
|
|
if (EXCLUDED_EVENTS.includes(event)) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
properties.version = pkg.version |
|
|
|
properties.service = env.SERVICE |
|
|
|
properties.environment = identity.environment |
|
|
|
|
|
|
|
@ -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) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
@ -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 = {} |
|
|
|
|
|
|
|
@ -1,7 +1,9 @@ |
|
|
|
const posthog = require("./posthog") |
|
|
|
const events = require("./events") |
|
|
|
const date = require("./date") |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
posthog, |
|
|
|
date, |
|
|
|
events, |
|
|
|
} |
|
|
|
|
|
|
|
@ -0,0 +1,7 @@ |
|
|
|
jest.mock("posthog-node", () => { |
|
|
|
return jest.fn().mockImplementation(() => { |
|
|
|
return { |
|
|
|
capture: jest.fn(), |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
@ -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" }) |
|
|
|
|
|
|
|
|
|
|
|
@ -139,6 +139,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.
|
|
|
|
|
|
|
|
@ -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
|
|
|
|
|