You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Dialogflow CX官方分析数据优化流程展示方案?

直接利用Dialogflow CX官方Analytics API优化你的流程分析方案

你完全可以通过Dialogflow CX的官方Analytics API直接获取控制台分析页面的数据,不用再自己维护webhook调用和数据库记录,以下是具体实现方案:

一、准备工作

  • 在Google Cloud Console启用Dialogflow CX Analytics API
  • 确保你的服务账号拥有dialogflowcx.analytics.reader或更高权限(比如dialogflowcx.admin),并配置好GOOGLE_APPLICATION_CREDENTIALS环境变量

二、Python后端调用Analytics API获取核心分析数据

使用google-cloud-dialogflow-cx客户端库直接调用Analytics接口,获取你需要的各类统计数据:

1. 安装依赖(如果未安装)

pip install google-cloud-dialogflow-cx

2. 示例代码:获取关键分析数据

from google.cloud import dialogflowcx_v3beta1 as dialogflowcx
import os
from flask import make_response

def get_dialogflow_analytics(parent):
    try:
        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = current_app.config['GOOGLE_APPLICATION_CREDENTIALS']
        analytics_client = dialogflowcx.AnalyticsClient()

        # 1. 获取访问量最高的流程数据
        flow_analytics_request = dialogflowcx.ListFlowAnalyticsRequest(
            parent=parent,
            filter="start_time>='2024-01-01T00:00:00Z' AND end_time<='2024-01-31T23:59:59Z'"
        )
        flow_analytics = list(analytics_client.list_flow_analytics(flow_analytics_request))

        # 2. 获取未命中意图/流程数据
        intent_analytics_request = dialogflowcx.ListIntentAnalyticsRequest(
            parent=parent,
            filter="start_time>='2024-01-01T00:00:00Z' AND end_time<='2024-01-31T23:59:59Z' AND is_fallback=true"
        )
        fallback_intents = list(analytics_client.list_intent_analytics(intent_analytics_request))

        # 3. 获取高频触发的意图数据
        top_intents_request = dialogflowcx.ListIntentAnalyticsRequest(
            parent=parent,
            filter="start_time>='2024-01-01T00:00:00Z' AND end_time<='2024-01-31T23:59:59Z'",
            order_by="conversations_count desc",
            page_size=10
        )
        top_intents = list(analytics_client.list_intent_analytics(top_intents_request))

        # 4. 获取用户结束通话的位置数据
        session_end_analytics_request = dialogflowcx.ListSessionAnalyticsRequest(
            parent=parent,
            filter="start_time>='2024-01-01T00:00:00Z' AND end_time<='2024-01-31T23:59:59Z'",
            page_size=50
        )
        session_end_data = []
        for session in analytics_client.list_session_analytics(session_end_analytics_request):
            if session.session_end:
                session_end_data.append({
                    'end_page': session.session_end.page.display_name,
                    'end_flow': session.session_end.flow.display_name,
                    'count': 1
                })
        # 聚合结束位置的次数
        end_location_counts = {}
        for item in session_end_data:
            key = f"{item['end_flow']}-{item['end_page']}"
            end_location_counts[key] = end_location_counts.get(key, 0) + 1

        return make_response({
            "message": "Analytics data fetched successfully",
            "success": True,
            "data": {
                "top_flows": [{"flow_name": flow.flow.display_name, "conversations": flow.conversations_count} for flow in flow_analytics],
                "fallback_intents": [{"intent_name": intent.intent.display_name, "count": intent.conversations_count} for intent in fallback_intents],
                "top_intents": [{"intent_name": intent.intent.display_name, "count": intent.conversations_count} for intent in top_intents],
                "end_locations": [{"flow_page": k, "count": v} for k, v in end_location_counts.items()]
            },
            "status": 200
        })
    except Exception as e:
        current_app.logger.error(e)
        return make_response({"message": str(e), "success": False, "data": None, "status": 500})

三、整合流程结构与分析数据

把你之前获取的流程/页面结构数据和上述Analytics数据做关联,给每个flow和page添加对应的统计字段:

def get_combined_flow_data(parent):
    # 获取流程结构数据
    flow_struct_response = get_dialogflow_flows(parent)
    if not flow_struct_response.json['success']:
        return flow_struct_response
    
    # 获取分析数据
    analytics_response = get_dialogflow_analytics(parent)
    if not analytics_response.json['success']:
        return analytics_response
    
    flow_struct = flow_struct_response.json['data']
    analytics_data = analytics_response.json['data']

    # 给每个flow添加访问量统计
    top_flow_map = {item['flow_name']: item['conversations'] for item in analytics_data['top_flows']}
    for flow in flow_struct:
        flow['conversations_count'] = top_flow_map.get(flow['flow'], 0)
        # 给每个page添加结束次数统计
        end_loc_map = {item['flow_page'].split('-')[1]: item['count'] for item in analytics_data['end_locations'] if item['flow_page'].split('-')[0] == flow['flow']}
        for page in flow['pages']:
            page['end_count'] = end_loc_map.get(page['page_name'], 0)
    
    return make_response({
        "message": "Combined data fetched successfully",
        "success": True,
        "data": flow_struct,
        "analytics_summary": analytics_data,
        "status": 200
    })

四、前端React Flow渲染

前端拿到整合后的数据,用React Flow渲染流程节点时,把分析数据可视化展示:

  • 给访问量最高的节点添加高亮样式(比如深色背景)
  • 未命中的意图/流程节点标注红色提示
  • 节点上显示访问次数、结束次数等统计数字
  • 可以通过节点交互展示高频意图详情

方案优势

  1. 无需维护webhook和数据库,减少开发与运维成本
  2. 数据与Dialogflow CX控制台完全一致,准确性更高
  3. 支持自定义时间范围、过滤条件,灵活性更强

内容的提问来源于stack exchange,提问作者Dilip Badal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 23:52:10