Skip to content

Commit

Permalink
fix: split crisp origin report routes (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Oct 29, 2024
1 parent cde6bbc commit 6f1f45a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
14 changes: 10 additions & 4 deletions src/crisp/crisp.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { SuperAdminAuthGuard } from 'src/partner-admin/super-admin-auth.guard';
import { CrispService } from './crisp.service';
Expand All @@ -8,9 +8,15 @@ import { CrispService } from './crisp.service';
export class CrispController {
constructor(private readonly crispService: CrispService) {}

@Get('/analytics-message-origin')
@Get('/conversations')
@UseGuards(SuperAdminAuthGuard)
async getCrispMessageOriginAnalytics() {
return this.crispService.getCrispMessageOriginAnalytics();
async getAllConversationSessionIds() {
return this.crispService.getAllConversationSessionIds();
}

@Post('/analytics-message-origin')
@UseGuards(SuperAdminAuthGuard)
async getCrispMessageOriginAnalytics(@Body() sessionIds: string[]) {
return this.crispService.getCrispMessageOriginAnalytics(sessionIds);
}
}
53 changes: 33 additions & 20 deletions src/crisp/crisp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,44 +140,57 @@ export class CrispService {
}
}

async getCrispMessageOriginAnalytics() {
// Supports getCrispMessageOriginAnalytics by splitting out logic to get all session IDs
// Combining the logic into one request causes a request timeout as it takes >30 seconds
async getAllConversationSessionIds() {
const messageSentEvents = await this.eventLoggerService.getMessageSentEventLogs();
const userEmails = [...new Set(messageSentEvents.flatMap((event) => event.user.email))];

let totalEmailOrigin = 0;
let totalChatOrigin = 0;

const sessionIds: string[] = [];
for (const userEmail of userEmails) {
try {
const conversations = await CrispClient.website.listPeopleConversations(
crispWebsiteId,
userEmail,
);

for (const conversation of conversations) {
const messages = await CrispClient.website.getMessagesInConversation(
crispWebsiteId,
conversation,
);

for (const message of messages) {
if (message.from === 'user') {
if (message.origin === 'chat') totalChatOrigin++;
if (message.origin === 'email') totalEmailOrigin++;
}
}
}
sessionIds.push(...conversations);
} catch (error) {
// skip
console.log(error);
}
}
return sessionIds;
}

// Returns an analytics string containing the number/percentage of crisp messages
// sent by email vs within the chat widget
async getCrispMessageOriginAnalytics(sessionIds) {
let totalEmailOrigin = 0;
let totalChatOrigin = 0;

try {
for (const sessionId of sessionIds) {
const messages = await CrispClient.website.getMessagesInConversation(
crispWebsiteId,
sessionId,
);

for (const message of messages) {
if (message.from === 'user') {
if (message.origin === 'chat') totalChatOrigin++;
if (message.origin === 'email') totalEmailOrigin++;
}
}
}
} catch (error) {
// skip
console.log(error);
}
const totalMessages = totalEmailOrigin + totalChatOrigin;
const chatPercentage =
totalMessages === 0 ? 0 : Math.round((totalChatOrigin / totalMessages) * 100);
const emailPercentage =
totalMessages === 0 ? 0 : Math.round((totalEmailOrigin / totalMessages) * 100);

return `Crisp message origin report: ${totalChatOrigin} (${chatPercentage}%) chat origin, ${totalEmailOrigin} (${emailPercentage}%) email origin`;
return `Crisp message origin report: ${totalChatOrigin} (${chatPercentage}%) chat widget origin, ${totalEmailOrigin} (${emailPercentage}%) email origin`;
}
}

0 comments on commit 6f1f45a

Please sign in to comment.