analytics.py
8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from fastapi import APIRouter, Body, Depends, Query
from sqlalchemy.orm import Session
from typing import Optional
from datetime import datetime
from app.schema.analytics import (
ConversationLogListResponse,
ConversationLogItem,
AnalyticsEventRequest,
AnalyticsEventResponse,
AnalyticsStatisticsResponse,
PageViewRequest,
PageViewResponse,
ScanEventRequest,
ScanEventResponse,
GroupJoinEventRequest,
GroupJoinEventResponse,
)
from app.service.analytics_service import AnalyticsService
from app.infrastructure.database import get_db
router = APIRouter()
EVENTS_USAGE = """
用于前端或 H5 批量上报通用行为埋点。
使用方式:
1. 在页面访问、按钮点击、表单提交、关键业务动作发生后调用。
2. `event_type` 写事件类型,建议使用稳定英文枚举,例如 `page_view`、`button_click`、`form_submit`。
3. `event_data` 放业务字段,例如页面路径、按钮名称、业务 ID、来源渠道。
4. 多条事件可以一次性放入 `events` 数组批量上报。
最小示例:
```json
{
"events": [
{
"event_type": "button_click",
"event_data": {
"page": "official_home",
"button": "open_qrcode"
}
}
]
}
```
"""
PAGE_VIEW_USAGE = """
用于记录页面访问埋点。
使用方式:
1. H5 首页、官网岗位详情页、后台关键页面打开后调用一次。
2. `page_type` 填页面编码,例如 `official_home`、`official_job_tab`。
3. 如果有登录用户或会话,传 `user_id`、`session_id`;匿名访问可以为空。
4. `device_type` 建议传 `pc`、`mobile` 或 `tablet`。
最小示例:
```json
{
"page_type": "official_home",
"session_id": "sess_001",
"device_type": "mobile"
}
```
"""
SCAN_EVENT_USAGE = """
用于记录客户扫码渠道活码的埋点。
使用方式:
1. 客户扫码渠道活码后调用。
2. 优先传 `qrcode_id`;如果当前链路只有渠道编码,也可以传 `channel_code`。
3. 可同时传 `position_code`、`employee_id`、`customer_user_id`,用于后续按岗位、承接员工和客户分析扫码来源。
最小示例:
```json
{
"qrcode_id": 1,
"channel_code": "position_good",
"customer_user_id": "customer_001",
"position_code": "position_good",
"employee_id": "emp_001"
}
```
"""
GROUP_JOIN_USAGE = """
用于记录客户成功入群后的转化埋点。
使用方式:
1. 客户扫码并成功加入企微群后调用。
2. `group_id` 填客户加入的群 ID。
3. `customer_user_id` 填客户用户 ID。
4. `employee_count`、`agent_count` 用于统计该群中的员工和 Agent 配置规模。
5. `channel_code` 用于回溯来源渠道。
最小示例:
```json
{
"group_id": "group_001",
"customer_user_id": "customer_001",
"employee_count": 2,
"agent_count": 1,
"channel_code": "position_good"
}
```
"""
@router.get(
"/api/v1/logs/conversations",
response_model=ConversationLogListResponse,
summary="查询会话日志",
description="查询 Agent 会话日志,用于后台排查用户问题、意图识别、响应内容和错误情况。该接口是日志查询,不是前端埋点上报。",
)
def list_conversation_logs(
page: int = Query(1, ge=1, description="页码,默认 1。"),
page_size: int = Query(20, ge=1, le=100, description="每页数量,默认 20,最大 100。"),
user_id: Optional[str] = Query(None, description="按用户 ID 筛选会话日志。"),
agent_id: Optional[int] = Query(None, description="按 Agent ID 筛选会话日志。"),
intent_type: Optional[str] = Query(None, description="按意图类型筛选。"),
time_start: Optional[datetime] = Query(None, description="开始时间,ISO datetime。"),
time_end: Optional[datetime] = Query(None, description="结束时间,ISO datetime。"),
has_error: Optional[bool] = Query(None, description="是否只查询异常会话。"),
db: Session = Depends(get_db),
):
svc = AnalyticsService(db)
total, items = svc.list_conversation_logs(
page=page, page_size=page_size,
user_id=user_id, agent_id=agent_id,
intent_type=intent_type,
time_start=time_start, time_end=time_end,
has_error=has_error,
)
return ConversationLogListResponse(
total=total,
list=[ConversationLogItem.model_validate(item) for item in items],
)
@router.post(
"/api/v1/analytics/events",
response_model=AnalyticsEventResponse,
summary="批量上报通用埋点事件",
description=EVENTS_USAGE,
)
def upload_analytics_events(
req: AnalyticsEventRequest = Body(
...,
openapi_examples={
"button_click": {
"summary": "按钮点击埋点",
"value": {
"events": [
{
"event_type": "button_click",
"event_data": {
"page": "official_home",
"button": "open_qrcode",
},
}
]
},
}
},
),
db: Session = Depends(get_db),
):
svc = AnalyticsService(db)
events_data = [ev.model_dump() for ev in req.events]
return svc.upload_events(events_data)
@router.get(
"/api/v1/analytics/statistics",
response_model=AnalyticsStatisticsResponse,
summary="查询埋点统计数据",
description="查询埋点和会话统计概览,包括 Agent 调用量、会话数、意图分布、Agent 调用排行和趋势数据。",
)
def get_analytics_statistics(
date_range: str = Query("last_7_days", description="统计时间范围,例如 last_7_days。"),
group_by: Optional[str] = Query(None, description="可选分组维度。"),
db: Session = Depends(get_db),
):
svc = AnalyticsService(db)
return svc.get_statistics(date_range=date_range, group_by=group_by)
@router.post(
"/api/v1/agent-group/analytics/page-view",
response_model=PageViewResponse,
summary="上报页面访问埋点",
description=PAGE_VIEW_USAGE,
)
def record_page_view(
req: PageViewRequest = Body(
...,
openapi_examples={
"official_home": {
"summary": "官网首页访问",
"value": {
"page_type": "official_home",
"session_id": "sess_001",
"device_type": "mobile",
},
}
},
),
db: Session = Depends(get_db),
):
svc = AnalyticsService(db)
return svc.record_page_view(
page_type=req.page_type,
user_id=req.user_id,
session_id=req.session_id,
device_type=req.device_type,
)
@router.post(
"/api/v1/agent-group/analytics/scan-event",
response_model=ScanEventResponse,
summary="上报扫码埋点",
description=SCAN_EVENT_USAGE,
)
def record_scan_event(
req: ScanEventRequest = Body(
...,
openapi_examples={
"qrcode_scan": {
"summary": "客户扫码渠道活码",
"value": {
"qrcode_id": 1,
"channel_code": "position_good",
"customer_user_id": "customer_001",
"position_code": "position_good",
"employee_id": "emp_001",
},
}
},
),
db: Session = Depends(get_db),
):
svc = AnalyticsService(db)
return svc.record_scan_event(
qrcode_id=req.qrcode_id,
channel_code=req.channel_code,
customer_user_id=req.customer_user_id,
position_code=req.position_code,
employee_id=req.employee_id,
)
@router.post(
"/api/v1/agent-group/analytics/group-join-success",
response_model=GroupJoinEventResponse,
summary="上报入群成功埋点",
description=GROUP_JOIN_USAGE,
)
def record_group_join_event(
req: GroupJoinEventRequest = Body(
...,
openapi_examples={
"group_join": {
"summary": "客户入群成功",
"value": {
"group_id": "group_001",
"customer_user_id": "customer_001",
"employee_count": 2,
"agent_count": 1,
"channel_code": "position_good",
},
}
},
),
db: Session = Depends(get_db),
):
svc = AnalyticsService(db)
return svc.record_group_join_event(
group_id=req.group_id,
customer_user_id=req.customer_user_id,
employee_count=req.employee_count,
agent_count=req.agent_count,
channel_code=req.channel_code,
)