@uxda/appkit 4.2.93 → 4.2.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,214 @@
1
+ /**
2
+ * 埋点初始化 - 精简版
3
+ */
4
+
5
+ import { App } from 'vue'
6
+ import { onAppShow, onAppHide, onNetworkStatusChange } from '@tarojs/taro'
7
+ import { trackingSDK, TrackingUserInfo } from './tracking-sdk'
8
+ import { installTrackingDirectives } from './directives'
9
+
10
+ // 导出用户信息接口,供外部使用
11
+ export type { TrackingUserInfo }
12
+
13
+ // 埋点初始化配置
14
+ interface TrackingInitConfig {
15
+ enabled?: boolean
16
+ debug?: boolean
17
+ apiEndpoint?: string
18
+ batchSize?: number
19
+ flushInterval?: number
20
+ userInfo?: TrackingUserInfo // 用户信息
21
+ }
22
+
23
+ /**
24
+ * 初始化埋点系统
25
+ */
26
+ export async function initTracking(config: TrackingInitConfig = {}): Promise<void> {
27
+ try {
28
+ console.log('[TrackingInit] 开始初始化埋点系统...')
29
+
30
+ // 1. 更新SDK配置
31
+ const sdkConfig = {
32
+ enabled: config.enabled,
33
+ debug: config.debug,
34
+ apiEndpoint: config.apiEndpoint,
35
+ batchSize: config.batchSize,
36
+ flushInterval: config.flushInterval,
37
+ userInfo: config.userInfo, // 传递用户信息
38
+ }
39
+
40
+ trackingSDK.updateConfig(sdkConfig)
41
+
42
+ // 2. 设置全局错误处理
43
+ setupGlobalErrorHandling()
44
+
45
+ // 3. 设置页面生命周期监听
46
+ setupPageLifecycleTracking()
47
+
48
+ // 4. 设置全局属性
49
+ setupGlobalProperties()
50
+
51
+ // 5. 设置页面历史记录
52
+ trackingSDK.clearPageHistory()
53
+
54
+ console.log('[TrackingInit] 埋点系统初始化完成')
55
+
56
+ // 发送初始化完成埋点
57
+ await trackingSDK.trackCustom('tracking_init_complete', {
58
+ logTime: Date.now(),
59
+ })
60
+ } catch (error) {
61
+ console.error('[TrackingInit] 埋点系统初始化失败:', error)
62
+ }
63
+ }
64
+
65
+ /**
66
+ * 设置全局错误处理
67
+ */
68
+ function setupGlobalErrorHandling(): void {
69
+ // 监听未捕获的错误
70
+ window.addEventListener('error', () => {
71
+ // trackingSDK.trackCustom('global_error', {
72
+ // message: event.message,
73
+ // filename: event.filename,
74
+ // lineno: event.lineno,
75
+ // colno: event.colno,
76
+ // error: event.error?.stack,
77
+ // logTime: Date.now(),
78
+ // })
79
+ })
80
+
81
+ // 监听未处理的Promise拒绝
82
+ window.addEventListener('unhandledrejection', (event) => {
83
+ // trackingSDK
84
+ // .trackCustom('unhandled_promise_rejection', {
85
+ // reason: event.reason?.toString(),
86
+ // promise: event.promise,
87
+ // logTime: Date.now(),
88
+ // })
89
+ // .catch(console.error)
90
+ })
91
+ }
92
+
93
+ /**
94
+ * 设置页面生命周期监听
95
+ */
96
+ function setupPageLifecycleTracking(): void {
97
+ // 监听应用显示
98
+ onAppShow(() => {
99
+ trackingSDK
100
+ .trackCustom('app_show', {
101
+ logTime: Date.now(),
102
+ })
103
+ .catch(console.error)
104
+ })
105
+
106
+ // 监听应用隐藏
107
+ onAppHide(() => {
108
+ trackingSDK
109
+ .trackCustom('app_hide', {
110
+ logTime: Date.now(),
111
+ })
112
+ .catch(console.error)
113
+ })
114
+
115
+ // 监听网络状态变化
116
+ onNetworkStatusChange((res) => {
117
+ trackingSDK
118
+ .trackCustom('network_status_change', {
119
+ isConnected: res.isConnected,
120
+ networkType: res.networkType,
121
+ logTime: Date.now(),
122
+ })
123
+ .catch(console.error)
124
+ })
125
+ }
126
+
127
+ /**
128
+ * 设置全局属性
129
+ */
130
+ function setupGlobalProperties(): void {
131
+ // 设置全局埋点SDK引用
132
+ if (typeof window !== 'undefined') {
133
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
134
+ ;(window as any).__trackingSDK = trackingSDK
135
+ }
136
+
137
+ // 设置全局埋点方法
138
+ if (typeof globalThis !== 'undefined') {
139
+ globalThis.track = trackingSDK.trackCustom.bind(trackingSDK)
140
+ globalThis.trackPage = trackingSDK.trackPageView.bind(trackingSDK)
141
+ globalThis.trackClick = trackingSDK.trackClick.bind(trackingSDK)
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Vue应用插件安装
147
+ */
148
+ export function installTrackingPlugin(app: App): void {
149
+ // 安装埋点指令
150
+ installTrackingDirectives(app)
151
+
152
+ // 添加全局属性
153
+ app.config.globalProperties.$track = async (event: string, data?: Record<string, unknown>) => {
154
+ await trackingSDK.trackCustom(event, data)
155
+ }
156
+
157
+ app.config.globalProperties.$trackPage = async (
158
+ pageTitle?: string,
159
+ customData?: Record<string, unknown>
160
+ ) => {
161
+ await trackingSDK.trackPageView(pageTitle)
162
+ if (customData) {
163
+ await trackingSDK.trackCustom('page_custom', customData)
164
+ }
165
+ }
166
+
167
+ app.config.globalProperties.$trackClick = async (elementId?: string, elementText?: string) => {
168
+ await trackingSDK.trackClick(elementId, elementText)
169
+ }
170
+
171
+ // 添加全局错误处理
172
+ // app.config.errorHandler = (err: any, _instance: any, info: string) => {
173
+ // trackingSDK.trackCustom('vue_error', {
174
+ // error: err?.message || 'Unknown error',
175
+ // stack: err?.stack,
176
+ // componentInfo: info,
177
+ // logTime: Date.now(),
178
+ // })
179
+ // }
180
+
181
+ console.log('[TrackingInit] Vue埋点插件安装完成')
182
+ }
183
+
184
+ /**
185
+ * 销毁埋点系统
186
+ */
187
+ export function destroyTracking(): void {
188
+ try {
189
+ // 销毁SDK
190
+ trackingSDK.destroy()
191
+
192
+ // 清理全局属性
193
+ if (typeof window !== 'undefined') {
194
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
195
+ delete (window as any).__trackingSDK
196
+ }
197
+
198
+ if (typeof globalThis !== 'undefined') {
199
+ delete globalThis.track
200
+ delete globalThis.trackPage
201
+ delete globalThis.trackClick
202
+ }
203
+
204
+ console.log('[TrackingInit] 埋点系统已销毁')
205
+ } catch (error) {
206
+ console.error('[TrackingInit] 埋点系统销毁失败:', error)
207
+ }
208
+ }
209
+
210
+ export default {
211
+ init: initTracking,
212
+ install: installTrackingPlugin,
213
+ destroy: destroyTracking,
214
+ }