Reduce event frequency

This commit is contained in:
Rory Powell 2022-08-02 10:52:09 +01:00
parent fe17ffffb6
commit cc9bc937b6
8 changed files with 84 additions and 8 deletions

View File

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

View File

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

View File

@ -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 = {}

View File

@ -1,7 +1,9 @@
const posthog = require("./posthog")
const events = require("./events")
const date = require("./date")
module.exports = {
posthog,
date,
events,
}

View File

@ -0,0 +1,7 @@
jest.mock("posthog-node", () => {
return jest.fn().mockImplementation(() => {
return {
capture: jest.fn(),
}
})
})

View File

@ -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" })

View File

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

View File

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