index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /**
  2. * SDK 初始化参数
  3. */
  4. interface SdkConfig {
  5. /**
  6. * 数据源ID,必填
  7. */
  8. user_action_set_id: number;
  9. /**
  10. * 加密key,必填
  11. */
  12. secret_key: string;
  13. /**
  14. * 微信小程序/小游戏APPID,wx开头,必填
  15. */
  16. appid: string;
  17. /**
  18. * 微信 openid,openid 和 unionid 至少填一个, 可以调用 setOpenId 设置
  19. */
  20. openid?: string;
  21. /**
  22. * 微信 unionid,openid 和 unionid 至少填一个, 可以调用 setUnionId 设置
  23. */
  24. unionid?: string;
  25. /**
  26. * 自定义用户ID,选填
  27. */
  28. user_unique_id?: string;
  29. /**
  30. * 是否自动采集事件
  31. */
  32. auto_track?: boolean;
  33. /**
  34. * 是否自动采集属性
  35. */
  36. auto_attr?: boolean;
  37. /**
  38. * 上报回调(包括成功、失败、重试的各种场景)
  39. */
  40. on_report_complete?: Function | undefined;
  41. /**
  42. * 明确上报失败的回调(sdk内部会直接丢弃的行为,无法再重试上报成功的部分)
  43. */
  44. on_report_fail?: Function | undefined;
  45. }
  46. /**
  47. * 小程序/小游戏信息
  48. */
  49. interface GameInfo {
  50. /**
  51. * 广告点击id、曝光id
  52. */
  53. ad_trace_id?: string;
  54. /**
  55. * 页面路径,小游戏没有
  56. */
  57. page_url?: string;
  58. /**
  59. * 页面名称,小游戏没有
  60. */
  61. page_title?: string;
  62. /**
  63. * 组件名称
  64. */
  65. component_name?: string;
  66. /**
  67. * 场景来源
  68. */
  69. source_scene?: number;
  70. /**
  71. * 渠道号
  72. */
  73. pkg_channel_id?: string;
  74. /**
  75. * 框架信息,小游戏没有
  76. */
  77. framework?: string;
  78. /**
  79. * 渠道来源
  80. */
  81. channel: string;
  82. /**
  83. * 启动参数
  84. */
  85. launch_options: string;
  86. }
  87. /**
  88. * 设备信息
  89. */
  90. interface DeviceInfo {
  91. /**
  92. * 设备品牌
  93. */
  94. device_brand: string;
  95. /**
  96. * 设备型号
  97. */
  98. device_model: string;
  99. /**
  100. * 微信App版本
  101. */
  102. wx_version: string;
  103. /**
  104. * 微信基础库版本
  105. */
  106. wx_lib_version: string;
  107. /**
  108. * 微信客户端平台
  109. */
  110. wx_platform: string;
  111. /**
  112. * 操作系统
  113. */
  114. os: string;
  115. /**
  116. * 操作系统版本
  117. */
  118. os_version: string;
  119. /**
  120. * 设备性能等级
  121. */
  122. benchmark_level: number;
  123. /**
  124. * 屏幕高度
  125. */
  126. screen_height: number;
  127. /**
  128. * 屏幕宽度
  129. */
  130. screen_width: number;
  131. }
  132. interface TrackBaseInfo extends SdkConfig, DeviceInfo {
  133. local_id: string;
  134. sdk_name: string;
  135. sdk_version: string;
  136. inner_param: {
  137. app_version: string;
  138. app_env_version: string;
  139. };
  140. }
  141. /**
  142. * 行为信息
  143. */
  144. interface ActionInfo {
  145. action_type: string;
  146. action_param?: Record<string | number | symbol, unknown>;
  147. is_sdk_auto_track: boolean;
  148. session_id: string;
  149. log_id: number;
  150. action_id: string;
  151. action_time: number;
  152. revised_action_time: number;
  153. is_retry: boolean;
  154. retry_count: number;
  155. ad_trace_id?: string;
  156. page_url?: string;
  157. page_title?: string;
  158. component_name?: string;
  159. source_scene?: number;
  160. pkg_channel_id?: string;
  161. /**
  162. * 网络类型
  163. */
  164. network_type?: string;
  165. inner_param?: Record<string | number | symbol, unknown>;
  166. channel?: string;
  167. }
  168. interface LogParams {
  169. log_type: string;
  170. code?: number;
  171. message?: string;
  172. err_stack?: string;
  173. user_action_set_id?: number;
  174. appid?: string;
  175. local_id?: string;
  176. session_id?: string;
  177. sdk_name?: string;
  178. sdk_version?: string;
  179. page_url?: string;
  180. framework?: string;
  181. device_brand?: string;
  182. device_model?: string;
  183. wx_version?: string;
  184. wx_lib_version?: string;
  185. wx_platform?: string;
  186. os?: string;
  187. os_version?: string;
  188. }
  189. interface TrackResponse {
  190. code: number;
  191. message: string;
  192. actionId?: string;
  193. }
  194. interface QueueProps {
  195. /**
  196. * 数据源ID,用于本地存储队列key
  197. */
  198. userActionSetId: number;
  199. /**
  200. * 队列长度
  201. */
  202. maxLength: number;
  203. }
  204. interface QueueManagerProps extends QueueProps {
  205. /**
  206. * OG事件列表
  207. */
  208. ogEvents: string[];
  209. }
  210. interface IEvent extends ActionInfo {
  211. inner_status?: string;
  212. }
  213. /**
  214. * 队列原子操作
  215. */
  216. declare class Queue {
  217. protected maxLength: any;
  218. protected userActionSetId: any;
  219. protected lostActionMaps: Record<string, number>;
  220. private stack;
  221. private localStorageKey;
  222. private timeStamp;
  223. constructor({ userActionSetId, maxLength }: QueueProps);
  224. getItems(): IEvent[];
  225. getStorage(): IEvent[];
  226. /**
  227. * 上报丢失条数到日志系统
  228. */
  229. reportLostNum(): void;
  230. /**
  231. * 获取丢失条数map
  232. * @returns 丢失条数map
  233. */
  234. getLostMaps(): Record<string, number>;
  235. protected init(): void;
  236. protected addItem(eventData: IEvent): void;
  237. protected removeItems(actionIds: string[]): void;
  238. protected updateForReportFail(actionIds: string[]): void;
  239. protected updateForReporting(actionIds: string[]): void;
  240. protected updateAllStack(stack: IEvent[]): void;
  241. protected updateToStorage(): void;
  242. /**
  243. * 更新丢失条数
  244. * @param sessionId 当前被丢弃行为的会话id
  245. */
  246. protected updateLostAction(sessionId: string): void;
  247. /**
  248. * 设置时间戳
  249. */
  250. protected setTimeStamp(): void;
  251. }
  252. declare class QueueManage extends Queue {
  253. protected ogEvents: string[];
  254. constructor({ userActionSetId, maxLength, ogEvents }: QueueManagerProps);
  255. /**
  256. * 获取可上报的队列
  257. * @param reportMaxLength 需要上报的队列最大长度
  258. */
  259. getReportableActions(reportMaxLength?: number): IEvent[];
  260. /**
  261. * 添加行为到队列
  262. * @param eventData 单条行为数据
  263. */
  264. addAction(eventData: IEvent): TrackResponse;
  265. /**
  266. * 根据acition_id删除行为
  267. * @param actionIds 行为id列表
  268. */
  269. removeActions(actionIds: string[]): void;
  270. /**
  271. * 上报失败时更新队列状态
  272. * @param actionIds
  273. */
  274. updateActionsForReportFail(actionIds: string[]): void;
  275. /**
  276. * 上报时更新队列状态为reporting
  277. * @param actionIds 需要更新的行为id数组
  278. */
  279. updateActionsForReporting(actionIds: string[]): void;
  280. /**
  281. * 获取可上报队列长度
  282. * @returns 可上报队列长度
  283. */
  284. getReportableActionsLength(): number;
  285. /**
  286. * 按照优先级从大到小给队列排序
  287. * @param currEvent 当前事件
  288. * @param stack 当前队列
  289. */
  290. sortQueue(currEvent: IEvent, stack: IEvent[]): IEvent[];
  291. /**
  292. * 计算优先级
  293. * @param currTime 当前时间
  294. * @param event 行为事件
  295. * @returns 权重值
  296. */
  297. private caculateWeight;
  298. /**
  299. *
  300. * @param currTime 当前时间
  301. * @param actionTime 队列行为时间
  302. * @returns 权重值
  303. */
  304. private formatWeight;
  305. }
  306. interface ConfigManager {
  307. getRealTimeActionList: () => Array<string>;
  308. getRequestTimeout: () => number;
  309. }
  310. interface ActionReporterProps {
  311. getBaseInfo: () => TrackBaseInfo;
  312. reportLog: (param: LogParams) => void;
  313. queueManager: QueueManage;
  314. configManager?: ConfigManager;
  315. onReportComplete?: Function | undefined;
  316. onReportFail?: Function | undefined;
  317. }
  318. /**
  319. * 行为上报类
  320. */
  321. declare class ActionReporter {
  322. private static currentRequestCount;
  323. private static requestConcurrency;
  324. static setRequestConcurrency(n: number): void;
  325. private getBaseInfo;
  326. private reportLog;
  327. private queueManager;
  328. private configManager;
  329. private onReportComplete;
  330. private onReportFail;
  331. private cgiBatchSize;
  332. private reportThreshold;
  333. private reportDelay;
  334. private triggerExecuteSend;
  335. private inspectDelay;
  336. private inspectTimer;
  337. private isNeedContinueSend;
  338. constructor(props: ActionReporterProps);
  339. /**
  340. * 批量上报行为,track的时候触发
  341. */
  342. batchSend(): void;
  343. /**
  344. * 全量上报, flush的时候触发
  345. */
  346. flushSend(): void;
  347. /**
  348. * 执行指定数量的行为上报逻辑,超过接口batch上限则并行发送请求
  349. */
  350. private executeSend;
  351. /**
  352. * 组装上报用的行为数据
  353. */
  354. private generateActionReportParams;
  355. /**
  356. * 上报接口请求成功逻辑处理
  357. * @param resData
  358. * @param actionIdList
  359. */
  360. private dealSuccessData;
  361. /**
  362. * 上报接口请求失败逻辑处理
  363. * @param errData
  364. * @param actionIdList
  365. */
  366. private dealFailData;
  367. /**
  368. * wx.request文档:https://developers.weixin.qq.com/minigame/dev/api/network/request/wx.request.html
  369. * @param param.data:接口请求参数、param.actionIdList:行为id列表
  370. * @returns
  371. */
  372. private report;
  373. /**
  374. * 启动上报定时器
  375. */
  376. private startInspectTimer;
  377. /**
  378. * 上报回调数据
  379. */
  380. private doTrackCallbackFn;
  381. }
  382. declare abstract class SdkExtCommon {
  383. protected special_method_symbol: symbol;
  384. /**
  385. * 付费
  386. */
  387. onPurchase(value: number): TrackResponse;
  388. /**
  389. * 进入前台
  390. */
  391. onEnterForeground(): TrackResponse;
  392. /**
  393. * 进入后台
  394. */
  395. onEnterBackground(): TrackResponse;
  396. /**
  397. * 启动
  398. */
  399. onAppStart(): TrackResponse;
  400. /**
  401. * 退出
  402. */
  403. onAppQuit(): TrackResponse;
  404. /**
  405. * 收藏
  406. */
  407. onAddToWishlist(): TrackResponse;
  408. protected wrapTrack(action_type: ActionInfo["action_type"], action_param?: ActionInfo["action_param"]): TrackResponse;
  409. abstract track(action_type: ActionInfo["action_type"], action_param?: ActionInfo["action_param"]): TrackResponse;
  410. }
  411. declare abstract class sdkExt extends SdkExtCommon {
  412. /**
  413. * 注册
  414. */
  415. onRegister(): TrackResponse;
  416. /**
  417. * 创角
  418. */
  419. onCreateRole(name?: string): TrackResponse;
  420. /**
  421. * 完成新手教程
  422. */
  423. onTutorialFinish(): TrackResponse;
  424. }
  425. declare const initializedInstanceSymbol: unique symbol;
  426. declare class SDK extends sdkExt {
  427. private static [initializedInstanceSymbol];
  428. static setRequestConcurrency(n: number): void;
  429. static setDebug(debug: boolean): void;
  430. protected env: string;
  431. protected sdk_version: string;
  432. protected sdk_name: string;
  433. protected config?: SdkConfig;
  434. protected deviceInfo: Partial<DeviceInfo>;
  435. protected gameInfo: Partial<GameInfo>;
  436. protected session_id: string;
  437. protected log_id: number;
  438. protected queueManage?: QueueManage;
  439. protected actionReporter?: ActionReporter;
  440. protected openid?: string;
  441. protected unionid?: string;
  442. protected user_unique_id?: string;
  443. protected inited: boolean;
  444. protected initErrMsg: string;
  445. protected onReportComplete?: Function | undefined;
  446. protected onReportFail?: Function | undefined;
  447. constructor(sdkConfig: SdkConfig);
  448. /**
  449. * 返回初始化状态
  450. * @returns
  451. */
  452. getInitResult(): {
  453. inited: boolean;
  454. initErrMsg: string;
  455. };
  456. /**
  457. * 数据上报方法
  458. * 0: 成功
  459. * 100: 未完成初始化或重复初始化导致初始化失败,请先完成初始化
  460. * 101: action_type 或 action_param 参数错误
  461. * 102: action_param 参数过大,不能超过 xxxx 字符
  462. * 103: 缓存队列已满,主动丢弃行为
  463. *
  464. */
  465. track(action_type: ActionInfo['action_type'], action_param?: ActionInfo['action_param']): TrackResponse;
  466. /**
  467. * 立即上报数据
  468. */
  469. flush(): void;
  470. /**
  471. * 设置 openId
  472. * 0: 成功
  473. * 101: openid 格式错误
  474. */
  475. setOpenId(openid: string): {
  476. code: number;
  477. message: string;
  478. };
  479. /**
  480. * 设置 unionid
  481. * 0: 成功
  482. * 101: unionid 格式错误
  483. */
  484. setUnionId(unionid: string): {
  485. code: number;
  486. message: string;
  487. };
  488. /**
  489. * 设置 user_unique_id
  490. * @param user_unique_id
  491. */
  492. setUserUniqueId(user_unique_id: string): {
  493. code: number;
  494. message: string;
  495. };
  496. protected doReportOnEnterBackground(): void;
  497. /**
  498. * 获取数据上报时所需要的 info 参数
  499. */
  500. private getTrackBaseInfo;
  501. /**
  502. * 生成上报队列需要的行为数据
  503. * @param actionParams 行为参数
  504. * @param is_sdk_auto_track 是否自动采集
  505. * @returns
  506. */
  507. private createAction;
  508. /**
  509. * 给指定行为的inner_param字段追加字段
  510. * @param action 待追加字段的行为
  511. * @param key 待添加到inner_param字段的key
  512. * @param value key对应的value
  513. */
  514. private addActionInnerParam;
  515. /**
  516. * 不同行为携带的渠道来源判断逻辑
  517. * @param actionType
  518. * @returns
  519. */
  520. private getChannelByActionType;
  521. /**
  522. * 上报日志
  523. * @param params 日志参数
  524. */
  525. private reportLog;
  526. private useAutoTrack;
  527. private getAutoProxyRemoteConfig;
  528. /**
  529. * 设置openid时,如果值合法缓存到Storage
  530. */
  531. private saveValidOpenidToStorage;
  532. }
  533. export { SDK };