aiAssistantTrack.js 1.59 KB
import { storageApi } from '@/utils/cache'

const EVENT = {
  BALL_CLICK: 'app_ai_ball_click',
  THINKING_TRIGGER: 'app_ai_thinking_trigger',
  QUESTION_SELECT: 'app_ai_question_select',
  ANSWER_RENDER: 'app_ai_answer_render',
  ERROR: 'app_ai_error'
}

function getUserId() {
  const { uuid } = storageApi.getUserInfo() || {}
  return uuid || ''
}

function getSessionId() {
  return storageApi.getSessionId() || ''
}

function getPageContext() {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  return currentPage?.route || '/'
}

function track(eventCode, businessParam = {}) {
  if (!uni.$track) return
  uni.$track.add({
    eventCode,
    businessParam: {
      user_id: getUserId(),
      session_id: getSessionId(),
      ...businessParam
    }
  })
}

export function trackAiBallClick() {
  track(EVENT.BALL_CLICK, {
    timestamp: Date.now(),
    page_context: getPageContext()
  })
}

export function trackAiThinkingTrigger(audioDuration) {
  track(EVENT.THINKING_TRIGGER, {
    audio_duration: audioDuration
  })
}

export function trackAiQuestionSelect(intentId, intentText) {
  track(EVENT.QUESTION_SELECT, {
    intent_id: intentId,
    intent_text: intentText
  })
}

export function trackAiAnswerRender(intentId, renderTime) {
  track(EVENT.ANSWER_RENDER, {
    intent_id: intentId,
    render_time: renderTime
  })
}

export function trackAiError(errorType) {
  track(EVENT.ERROR, {
    error_type: errorType,
    timestamp: Date.now()
  })
}

export const AI_ERROR_TYPE = {
  TIMEOUT: 'timeout',
  NETWORK: 'network_disconnect',
  NO_ANSWER: 'no_answer',
  OTHER: 'other'
}