@@ -140,44 +140,57 @@ export class CrispService {
140
140
}
141
141
}
142
142
143
- async getCrispMessageOriginAnalytics ( ) {
143
+ // Supports getCrispMessageOriginAnalytics by splitting out logic to get all session IDs
144
+ // Combining the logic into one request causes a request timeout as it takes >30 seconds
145
+ async getAllConversationSessionIds ( ) {
144
146
const messageSentEvents = await this . eventLoggerService . getMessageSentEventLogs ( ) ;
145
147
const userEmails = [ ...new Set ( messageSentEvents . flatMap ( ( event ) => event . user . email ) ) ] ;
146
-
147
- let totalEmailOrigin = 0 ;
148
- let totalChatOrigin = 0 ;
149
-
148
+ const sessionIds : string [ ] = [ ] ;
150
149
for ( const userEmail of userEmails ) {
151
150
try {
152
151
const conversations = await CrispClient . website . listPeopleConversations (
153
152
crispWebsiteId ,
154
153
userEmail ,
155
154
) ;
156
-
157
- for ( const conversation of conversations ) {
158
- const messages = await CrispClient . website . getMessagesInConversation (
159
- crispWebsiteId ,
160
- conversation ,
161
- ) ;
162
-
163
- for ( const message of messages ) {
164
- if ( message . from === 'user' ) {
165
- if ( message . origin === 'chat' ) totalChatOrigin ++ ;
166
- if ( message . origin === 'email' ) totalEmailOrigin ++ ;
167
- }
168
- }
169
- }
155
+ sessionIds . push ( ...conversations ) ;
170
156
} catch ( error ) {
171
157
// skip
172
158
console . log ( error ) ;
173
159
}
174
160
}
161
+ return sessionIds ;
162
+ }
163
+
164
+ // Returns an analytics string containing the number/percentage of crisp messages
165
+ // sent by email vs within the chat widget
166
+ async getCrispMessageOriginAnalytics ( sessionIds ) {
167
+ let totalEmailOrigin = 0 ;
168
+ let totalChatOrigin = 0 ;
169
+
170
+ try {
171
+ for ( const sessionId of sessionIds ) {
172
+ const messages = await CrispClient . website . getMessagesInConversation (
173
+ crispWebsiteId ,
174
+ sessionId ,
175
+ ) ;
176
+
177
+ for ( const message of messages ) {
178
+ if ( message . from === 'user' ) {
179
+ if ( message . origin === 'chat' ) totalChatOrigin ++ ;
180
+ if ( message . origin === 'email' ) totalEmailOrigin ++ ;
181
+ }
182
+ }
183
+ }
184
+ } catch ( error ) {
185
+ // skip
186
+ console . log ( error ) ;
187
+ }
175
188
const totalMessages = totalEmailOrigin + totalChatOrigin ;
176
189
const chatPercentage =
177
190
totalMessages === 0 ? 0 : Math . round ( ( totalChatOrigin / totalMessages ) * 100 ) ;
178
191
const emailPercentage =
179
192
totalMessages === 0 ? 0 : Math . round ( ( totalEmailOrigin / totalMessages ) * 100 ) ;
180
193
181
- return `Crisp message origin report: ${ totalChatOrigin } (${ chatPercentage } %) chat origin, ${ totalEmailOrigin } (${ emailPercentage } %) email origin` ;
194
+ return `Crisp message origin report: ${ totalChatOrigin } (${ chatPercentage } %) chat widget origin, ${ totalEmailOrigin } (${ emailPercentage } %) email origin` ;
182
195
}
183
196
}
0 commit comments