Skip to content

Commit

Permalink
feat: crisp origin report (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Oct 29, 2024
1 parent 07c8cfb commit c0e3d00
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/crisp/crisp.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { SuperAdminAuthGuard } from 'src/partner-admin/super-admin-auth.guard';
import { CrispService } from './crisp.service';

@ApiTags('Crisp')
@Controller('crisp')
export class CrispController {
constructor(private readonly crispService: CrispService) {}

@Get('/analytics-message-origin')
@UseGuards(SuperAdminAuthGuard)
async getCrispMessageOriginAnalytics() {
return this.crispService.getCrispMessageOriginAnalytics();
}
}
2 changes: 2 additions & 0 deletions src/crisp/crisp.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { EventLogEntity } from 'src/entities/event-log.entity';
import { UserEntity } from 'src/entities/user.entity';
import { EventLoggerService } from 'src/event-logger/event-logger.service';
import { CrispController } from './crisp.controller';
import { CrispService } from './crisp.service';

@Module({
imports: [TypeOrmModule.forFeature([EventLogEntity, UserEntity])],
providers: [CrispService, EventLoggerService],
controllers: [CrispController],
})
export class CrispModule {}
36 changes: 36 additions & 0 deletions src/crisp/crisp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,40 @@ export class CrispService {
throw new Error(`Delete cypress crisp profiles API call failed: ${error}`);
}
}

async getCrispMessageOriginAnalytics() {
const messageSentEvents = await this.eventLoggerService.getMessageSentEventLogs();
const userEmails = [...new Set(messageSentEvents.flatMap((event) => event.user.email))];

let totalEmailOrigin = 0;
let totalChatOrigin = 0;

for (const userEmail of userEmails) {
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++;
}
}
}
}
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`;
}
}
9 changes: 8 additions & 1 deletion src/event-logger/event-logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { EventLogEntity } from 'src/entities/event-log.entity';
import { UserEntity } from 'src/entities/user.entity';
import { Repository } from 'typeorm';
import { ICreateEventLog } from './event-logger.interface';
import { EVENT_NAME, ICreateEventLog } from './event-logger.interface';

const logger = new Logger('EventLogger');

Expand All @@ -20,6 +20,13 @@ export class EventLoggerService {
return await this.eventLoggerRepository.findOneBy({ id });
}

async getMessageSentEventLogs(): Promise<EventLogEntity[]> {
return await this.eventLoggerRepository.find({
where: { event: EVENT_NAME.CHAT_MESSAGE_SENT },
relations: { user: true },
});
}

async createEventLog({ email, userId, event, date }: ICreateEventLog) {
try {
if (!userId && !email) {
Expand Down

0 comments on commit c0e3d00

Please sign in to comment.