1+ // Virtual Agent Conversation Analytics
2+ // Analyzes VA conversation logs to identify the most common topics
3+
4+ var daysBack = 7 ; // Analyze conversations from the past 7 days
5+
6+ // Calculate date range
7+ var startDate = new GlideDateTime ( ) ;
8+ startDate . addDaysLocalTime ( - daysBack ) ;
9+
10+ gs . info ( '=== Virtual Agent Conversation Analytics ===' ) ;
11+ gs . info ( 'Analyzing conversations from: ' + startDate . getDisplayValue ( ) ) ;
12+
13+ // Get conversation logs
14+ var convGr = new GlideRecord ( 'sys_cs_conversation' ) ;
15+ convGr . addQuery ( 'sys_created_on' , '>=' , startDate ) ;
16+ convGr . query ( ) ;
17+
18+ var totalConversations = convGr . getRowCount ( ) ;
19+ gs . info ( 'Total Conversations: ' + totalConversations ) ;
20+
21+ // Auto-detect topic field (handles schema variations)
22+ var topicField = convGr . isValidField ( 'topic' ) ? 'topic' :
23+ ( convGr . isValidField ( 'selected_topic' ) ? 'selected_topic' : null ) ;
24+
25+ if ( ! topicField ) {
26+ gs . warn ( 'No topic field found on sys_cs_conversation table' ) ;
27+ } else {
28+ // Track topic counts
29+ var topicCounts = { } ;
30+
31+ while ( convGr . next ( ) ) {
32+ var topicId = convGr . getValue ( topicField ) ;
33+
34+ if ( topicId ) {
35+ var topicGr = new GlideRecord ( 'sys_cs_topic' ) ;
36+ if ( topicGr . get ( topicId ) ) {
37+ var topicName = topicGr . getValue ( 'name' ) ;
38+
39+ if ( ! topicCounts [ topicName ] ) {
40+ topicCounts [ topicName ] = 0 ;
41+ }
42+ topicCounts [ topicName ] ++ ;
43+ }
44+ }
45+ }
46+
47+ // Sort and display most common topics
48+ gs . info ( '\n=== Most Common Topics ===' ) ;
49+ var sortedTopics = [ ] ;
50+ for ( var topic in topicCounts ) {
51+ sortedTopics . push ( { name : topic , count : topicCounts [ topic ] } ) ;
52+ }
53+ sortedTopics . sort ( function ( a , b ) { return b . count - a . count ; } ) ;
54+
55+ if ( sortedTopics . length === 0 ) {
56+ gs . info ( 'No topics found in the selected time range' ) ;
57+ } else {
58+ for ( var i = 0 ; i < Math . min ( 10 , sortedTopics . length ) ; i ++ ) {
59+ gs . info ( ( i + 1 ) + '. ' + sortedTopics [ i ] . name + ': ' + sortedTopics [ i ] . count + ' conversations' ) ;
60+ }
61+ }
62+ }
63+
64+ gs . info ( '\n=== Analysis Complete ===' ) ;
0 commit comments