Skip to content

Commit

Permalink
Merge pull request #387 from cofacts/batch-ga
Browse files Browse the repository at this point in the history
feat: add ga to processBatch
  • Loading branch information
MrOrz committed Feb 15, 2024
2 parents 1d089c5 + 5040038 commit f476d7c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,6 @@ We use dimension `Message Source` (Custom Dimemsion1) to classify different even

1. User sends a message to us
- `UserInput` / `MessageType` / `<text | image | video | ...>`
- 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` / `<article id>` for each article found
Expand Down
13 changes: 9 additions & 4 deletions src/lib/ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ export default function ga(
let events: EventBatch['events'] = [];
const extra: Record<string, unknown> = {};

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) {
Expand All @@ -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() {
Expand All @@ -71,7 +73,10 @@ export default function ga(
rollbar.error(`[insertAnalytics] ${e.message}`, e);
});
events = [];
return visitor.send();
visitor.send();
return visitorToReturn;
},
};

return visitorToReturn;
}
19 changes: 18 additions & 1 deletion src/webhook/handlers/processBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,32 @@ 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,
};

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({
Expand Down
2 changes: 1 addition & 1 deletion src/webhook/handlers/singleUserHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f476d7c

Please sign in to comment.