From 2f41e1405fb206a4826e267eb53781b5d307eac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Malfait?= Date: Thu, 26 Sep 2024 17:23:58 +0200 Subject: [PATCH] Change tinybird event format (#7272) Separate pageview analytics from core events --- .../analytics/analytics.service.ts | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/twenty-server/src/engine/core-modules/analytics/analytics.service.ts b/packages/twenty-server/src/engine/core-modules/analytics/analytics.service.ts index 4e60ee74aa..2b2eeb3d68 100644 --- a/packages/twenty-server/src/engine/core-modules/analytics/analytics.service.ts +++ b/packages/twenty-server/src/engine/core-modules/analytics/analytics.service.ts @@ -13,7 +13,7 @@ type CreateEventInput = { @Injectable() export class AnalyticsService { private readonly logger = new Logger(AnalyticsService.name); - private readonly datasource = 'event'; + private readonly defaultDatasource = 'event'; constructor( private readonly environmentService: EnvironmentService, @@ -29,16 +29,31 @@ export class AnalyticsService { return { success: true }; } - const data = { - action: createEventInput.action, - timestamp: new Date().toISOString(), - version: '1', - payload: { - userId: userId, - workspaceId: workspaceId, - ...createEventInput.payload, - }, - }; + let data; + + switch (createEventInput.action) { + case 'pageview': + data = { + timestamp: new Date().toISOString(), + version: '1', + userId: userId, + workspaceId: workspaceId, + ...createEventInput.payload, + }; + break; + default: + data = { + action: createEventInput.action, + timestamp: new Date().toISOString(), + version: '1', + userId: userId, + workspaceId: workspaceId, + payload: { + ...createEventInput.payload, + }, + }; + break; + } const config: AxiosRequestConfig = { headers: { @@ -47,9 +62,14 @@ export class AnalyticsService { }, }; + const datasource = + createEventInput.action === 'pageview' + ? 'pageview' + : this.defaultDatasource; + try { await this.httpService.axiosRef.post( - `/events?name=${this.datasource}`, + `/events?name=${datasource}`, data, config, );