From d4351e295c933aa983c157adac69d5d5e11ac018 Mon Sep 17 00:00:00 2001 From: MrOrz Date: Thu, 8 Feb 2024 21:46:21 +0800 Subject: [PATCH 1/2] feat: add ga to processBatch --- README.md | 3 --- src/webhook/handlers/processBatch.ts | 19 ++++++++++++++++++- src/webhook/handlers/singleUserHandler.ts | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d2a6bbd7..1a6d7946 100644 --- a/README.md +++ b/README.md @@ -295,9 +295,6 @@ We use dimension `Message Source` (Custom Dimemsion1) to classify different even 1. User sends a message to us - `UserInput` / `MessageType` / `` - - For the time being, we only process message with "text" type. The following events only applies - for text messages. - - If we found a articles in database that matches the message: - `UserInput` / `ArticleSearch` / `ArticleFound` - `Article` / `Search` / `
` for each article found diff --git a/src/webhook/handlers/processBatch.ts b/src/webhook/handlers/processBatch.ts index 7a5c1784..6eac5930 100644 --- a/src/webhook/handlers/processBatch.ts +++ b/src/webhook/handlers/processBatch.ts @@ -9,8 +9,9 @@ import { createPostbackAction, createTextMessage, } from './utils'; +import ga from 'src/lib/ga'; -async function processBatch(messages: CooccurredMessage[]) { +async function processBatch(messages: CooccurredMessage[], userId: string) { const context: Context = { sessionId: Date.now(), msgs: messages, @@ -18,6 +19,22 @@ async function processBatch(messages: CooccurredMessage[]) { const msgCount = messages.length; + const visitor = ga( + userId, + '__PROCESS_BATCH__', + `Batch: ${msgCount} messages` + ); + + // Track media message type send by user + messages.forEach((message) => { + visitor.event({ + ec: 'UserInput', + ea: 'MessageType', + el: message.type, + }); + }); + visitor.send(); + const replies: Message[] = [ { ...createTextMessage({ diff --git a/src/webhook/handlers/singleUserHandler.ts b/src/webhook/handlers/singleUserHandler.ts index 417cb27c..ec310bc5 100644 --- a/src/webhook/handlers/singleUserHandler.ts +++ b/src/webhook/handlers/singleUserHandler.ts @@ -195,7 +195,7 @@ const singleUserHandler = async ( // we wait first and check if there are new messages. // await sleep(TIMEOUT_BEFORE_ASKING_COOCCURRENCES); - return send(await processBatch(messages), msg); + return send(await processBatch(messages, userId), msg); } // Now there is only one message in the batch; From 50400387a4dcacb092925c8c7280e84e4d424543 Mon Sep 17 00:00:00 2001 From: MrOrz Date: Thu, 8 Feb 2024 22:06:22 +0800 Subject: [PATCH 2/2] fix(ga): fix the bug that insertEventBatch is not called on ga()event().send() --- src/lib/ga.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib/ga.ts b/src/lib/ga.ts index d9311482..13f7b6fb 100644 --- a/src/lib/ga.ts +++ b/src/lib/ga.ts @@ -36,10 +36,11 @@ export default function ga( let events: EventBatch['events'] = []; const extra: Record = {}; - return { + const visitorToReturn = { set(key: string, value: unknown) { extra[key] = value; - return visitor.set(key, value); + visitor.set(key, value); + return visitorToReturn; }, event(evt: EventParams) { @@ -50,7 +51,8 @@ export default function ga( value: evt.ev === undefined ? null : +evt.ev, time: new Date(), }); - return visitor.event(evt); + visitor.event(evt); + return visitorToReturn; }, send() { @@ -71,7 +73,10 @@ export default function ga( rollbar.error(`[insertAnalytics] ${e.message}`, e); }); events = []; - return visitor.send(); + visitor.send(); + return visitorToReturn; }, }; + + return visitorToReturn; }