Skip to content

Commit

Permalink
[GA] add group join/leave event
Browse files Browse the repository at this point in the history
  • Loading branch information
nonumpa committed Jan 26, 2021
1 parent 1f948f4 commit 90ddb0a
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/webhook/handlers/groupHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './utils';
import ga from 'src/lib/ga';
import rollbar from 'src/lib/rollbar';
import lineClient from 'src/webhook/lineClient';

const SIMILARITY_THRESHOLD = 0.95;

Expand All @@ -23,12 +24,36 @@ export default async function({ req, type, replyToken, groupId, otherFields }) {
};

// Handle join/leave event
// To calculate total group bot joined, we set ga event value 1 as join, -1 as leave
// Details see Implicit Count in https://support.google.com/analytics/answer/1033068?hl=en
if (type === 'join') {
//
return;
// https://developers.line.biz/en/reference/messaging-api/#get-members-group-count
// Note: leave group cannot get the number
const { count: numberOfGroupMembers } = await lineClient.get(
`/group/${groupId}/members/count`
);
console.log(
'Joined group, numberOfGroupMembers: ' +
JSON.stringify(numberOfGroupMembers)
);

const visitor = ga(groupId, 'N/A', '', otherFields.source.type);

visitor.event({
ec: 'Group',
ea: 'Join',
ev: 1,
});
visitor.set('cd2', numberOfGroupMembers);
visitor.send();
} else if (type === 'leave') {
//
return;
const visitor = ga(groupId, 'N/A', '', otherFields.source.type);
visitor.event({
ec: 'Group',
ea: 'Leave',
ev: -1,
});
visitor.send();
}

// React to certain type of events
Expand Down

0 comments on commit 90ddb0a

Please sign in to comment.