gio-miniprogram-sdk 4.4.2 → 4.4.3

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.
Files changed (98) hide show
  1. package/dist/alipay.js +1 -1
  2. package/dist/baidu.js +1 -1
  3. package/dist/bytedance.js +1 -1
  4. package/dist/jingdong.js +1 -1
  5. package/dist/kuaishou.js +1 -1
  6. package/dist/plugins/gioABTest.js +1 -1
  7. package/dist/plugins/gioCompress.js +1 -1
  8. package/dist/plugins/gioEventAutoTracking.js +1 -1
  9. package/dist/plugins/gioImpressionTracking.js +1 -1
  10. package/dist/plugins/gioMultipleInstances.js +1 -1
  11. package/dist/plugins/gioPerformance.js +1 -1
  12. package/dist/qq.js +1 -1
  13. package/dist/quickapp.js +1 -1
  14. package/dist/taobao.js +1 -1
  15. package/dist/taro.js +1 -1
  16. package/dist/uniapp.js +1 -1
  17. package/dist/wechat.js +1 -1
  18. package/dist/xiaohongshu.js +1 -1
  19. package/package.json +7 -1
  20. package/.eslintignore +0 -11
  21. package/.eslintrc.js +0 -120
  22. package/.github/workflows/greetings.yml +0 -13
  23. package/.github/workflows/publish.yml +0 -45
  24. package/.prettierignore +0 -19
  25. package/.prettierrc.js +0 -9
  26. package/babel.config.js +0 -6
  27. package/rollup.config.js +0 -100
  28. package/rollup.plugins.config.js +0 -84
  29. package/src/adapters/gioTaobaoAdapter.ts +0 -202
  30. package/src/adapters/gioTaroAdapter.ts +0 -467
  31. package/src/adapters/gioUniAppAdapter.ts +0 -431
  32. package/src/constants/config.ts +0 -170
  33. package/src/constants/emitMsg.ts +0 -24
  34. package/src/constants/regex.ts +0 -18
  35. package/src/core/dataStore/eventContextBuilder.ts +0 -95
  36. package/src/core/dataStore/eventHooks/appEffects.ts +0 -218
  37. package/src/core/dataStore/eventHooks/index.ts +0 -468
  38. package/src/core/dataStore/eventHooks/minipPage.ts +0 -221
  39. package/src/core/dataStore/eventHooks/pageEffects.ts +0 -189
  40. package/src/core/dataStore/index.ts +0 -661
  41. package/src/core/growingIO.ts +0 -637
  42. package/src/core/pluginsInstance/base.ts +0 -157
  43. package/src/core/pluginsInstance/taro.ts +0 -15
  44. package/src/core/pluginsInstance/tb.ts +0 -15
  45. package/src/core/pluginsInstance/uniapp.ts +0 -15
  46. package/src/core/uploader.ts +0 -272
  47. package/src/core/userStore.ts +0 -267
  48. package/src/index.ts +0 -77
  49. package/src/minipInstance/base.ts +0 -352
  50. package/src/minipInstance/framework.ts +0 -24
  51. package/src/minipInstance/jd.ts +0 -11
  52. package/src/minipInstance/ks.ts +0 -12
  53. package/src/minipInstance/my.ts +0 -131
  54. package/src/minipInstance/qq.ts +0 -12
  55. package/src/minipInstance/quickapp.ts +0 -233
  56. package/src/minipInstance/swan.ts +0 -20
  57. package/src/minipInstance/tb.ts +0 -58
  58. package/src/minipInstance/tt.ts +0 -12
  59. package/src/minipInstance/wx.ts +0 -27
  60. package/src/minipInstance/xhs.ts +0 -20
  61. package/src/platformConfig/base.ts +0 -82
  62. package/src/platformConfig/framework.ts +0 -31
  63. package/src/platformConfig/jd.ts +0 -11
  64. package/src/platformConfig/ks.ts +0 -11
  65. package/src/platformConfig/my.ts +0 -16
  66. package/src/platformConfig/qq.ts +0 -13
  67. package/src/platformConfig/quickapp.ts +0 -48
  68. package/src/platformConfig/swan.ts +0 -11
  69. package/src/platformConfig/tb.ts +0 -29
  70. package/src/platformConfig/tt.ts +0 -11
  71. package/src/platformConfig/wx.ts +0 -20
  72. package/src/platformConfig/xhs.ts +0 -27
  73. package/src/plugins/gioABTest.ts +0 -348
  74. package/src/plugins/gioCompress.ts +0 -253
  75. package/src/plugins/gioEventAutoTracking.ts +0 -310
  76. package/src/plugins/gioImpressionTracking.ts +0 -386
  77. package/src/plugins/gioMultipleInstances.ts +0 -189
  78. package/src/plugins/gioPerformance/exception.ts +0 -152
  79. package/src/plugins/gioPerformance/monitor.ts +0 -284
  80. package/src/plugins/gioPerformance/network.ts +0 -197
  81. package/src/plugins/gioPerformance.ts +0 -131
  82. package/src/types/base.ts +0 -167
  83. package/src/types/dataStore.ts +0 -62
  84. package/src/types/eventHooks.ts +0 -146
  85. package/src/types/gioCustomTracking.ts +0 -9
  86. package/src/types/gioEventTracking.ts +0 -10
  87. package/src/types/gioPerformance.ts +0 -8
  88. package/src/types/growingIO.ts +0 -137
  89. package/src/types/minipInstance.ts +0 -133
  90. package/src/types/platforms.ts +0 -64
  91. package/src/types/plugins.ts +0 -24
  92. package/src/types/uploader.ts +0 -30
  93. package/src/types/userStore.ts +0 -28
  94. package/src/utils/glodash.ts +0 -430
  95. package/src/utils/tools.ts +0 -511
  96. package/src/utils/verifiers.ts +0 -52
  97. package/tsconfig.json +0 -32
  98. package/typings/global.d.ts +0 -105
@@ -1,637 +0,0 @@
1
- import {
2
- forEach,
3
- isArray,
4
- isEmpty,
5
- isFunction,
6
- isNil,
7
- isObject,
8
- keys,
9
- toString,
10
- unset
11
- } from '@@/utils/glodash';
12
- import {
13
- callError,
14
- consoleText,
15
- eventNameValidate,
16
- getDynamicAttributes,
17
- getGlobal,
18
- getPlainPlatform,
19
- guid,
20
- limitObject,
21
- niceCallback,
22
- qsParse
23
- } from '@@/utils/tools';
24
- import { ALLOW_SET_OPTIONS, DEFAULT_SETTINGS } from '@@/constants/config';
25
- import { DataStoreType, OriginOptions } from '@@/types/dataStore';
26
- import { FRAMEWORKS, PLATFORMTYPES } from '@@/types/platforms';
27
- import { GrowingIOType } from '@@/types/growingIO';
28
- import { MinipInstanceType } from '@@/types/minipInstance';
29
- import { PluginsType } from '@@/types/plugins';
30
- import { UploaderType } from '@@/types/uploader';
31
- import { UserStoreType } from '@@/types/userStore';
32
- import { verifyId } from '@@/utils/verifiers';
33
- import * as glodash from '@@/utils/glodash';
34
- import * as tools from '@@/utils/tools';
35
- import DataStore from './dataStore';
36
- import mitt from 'mitt';
37
- import platformConfig from '__GIO_PLATFORM_CONFIG__';
38
- import PlatformInstance from '__GIO_PLATFORM_INSTANCE__';
39
- import Plugins from '__GIO_PLUGIN_INSTANCE__';
40
- import qs from 'querystringify';
41
- import Uploader from './uploader';
42
- import UserStore from '@@/core/userStore';
43
- import EMIT_MSG from '@@/constants/emitMsg';
44
-
45
- // SDK全局参数 版本号、小程序平台、框架名称、运行环境 均由打包工具替换写入
46
- const sdkVersion: any = '__SDK_VERSION__';
47
- const gioPlatform: any = '__GIO_PLATFORM__';
48
- const gioFramework: any = '__GIO_FRAMEWORK__';
49
-
50
- class GrowingIO implements GrowingIOType {
51
- public utils: any;
52
- public emitter: any;
53
- public gioPlatform: PLATFORMTYPES;
54
- public gioFramework: FRAMEWORKS;
55
- public sdkVersion: string;
56
- public inPlugin: boolean;
57
- public subKey: string;
58
- public platformConfig: any;
59
- public gioSDKInitialized: boolean;
60
- public vdsConfig: OriginOptions;
61
- public minipInstance: MinipInstanceType;
62
- public userStore: UserStoreType;
63
- public dataStore: DataStoreType;
64
- public plugins: PluginsType;
65
- public uploader: UploaderType;
66
- // 当前主SDK的trackingId,用于区分事件归属。
67
- public trackingId: string;
68
- public subInstance: any;
69
- private _identified: boolean;
70
- constructor() {
71
- this.utils = { ...glodash, ...tools, qs };
72
- this.emitter = mitt();
73
- // 小程序平台
74
- this.gioPlatform =
75
- gioPlatform === 'framework' ? getPlainPlatform() : gioPlatform;
76
- // 框架
77
- this.gioFramework = gioFramework;
78
- // sdk版本号
79
- this.sdkVersion = sdkVersion;
80
- // 是否在小程序插件中
81
- try {
82
- // 没有getApp方法或没有App方法的认为在插件中
83
- if (
84
- this.gioPlatform === 'quickapp' ||
85
- (isFunction(getApp) && isFunction(App))
86
- ) {
87
- this.inPlugin = false;
88
- } else {
89
- this.inPlugin = true;
90
- }
91
- } catch (error) {
92
- this.inPlugin = true;
93
- }
94
- if (this.inPlugin) {
95
- consoleText('未检测到小程序实例,自动切换为插件模式!', 'info');
96
- }
97
- // 小程序实例配置
98
- this.platformConfig = platformConfig;
99
- // 小程序实例
100
- this.minipInstance = new PlatformInstance(this);
101
- // 数据中心实例
102
- this.dataStore = new DataStore(this);
103
- // 用户实例
104
- this.userStore = new UserStore(this);
105
- // 初始化请求逻辑
106
- this.uploader = new Uploader(this);
107
- // 插件管理实例
108
- this.plugins = new Plugins(this);
109
- // 加载内置插件
110
- this.plugins.innerPluginInit();
111
- }
112
-
113
- // SDK初始化方法
114
- init = (options: any) => {
115
- // 初始化实例之前
116
- this.emitter.emit(EMIT_MSG.ON_SDK_INITIALIZE_BEFORE, { options });
117
- try {
118
- consoleText('Gio小程序SDK 初始化中...', 'info');
119
- if (
120
- options.trackingId &&
121
- this.dataStore.initializedTrackingIds.includes(options.trackingId)
122
- ) {
123
- throw new Error(`已存在实例 ${options.trackingId},请勿重复初始化!`);
124
- }
125
- if (this.trackingId && !this.plugins.gioMultipleInstances) {
126
- throw new Error('您正在尝试初始化另一个实例,请集成多实例插件后重试!');
127
- }
128
- // 初始化实例配置
129
- const vdsConfig = this.dataStore.initTrackerOptions(options);
130
- // 广播基础配置初始化完成
131
- this.emitter?.emit(EMIT_MSG.OPTION_INITIALIZED, vdsConfig);
132
- // 标记SDK已初始化完成
133
- this.gioSDKInitialized = true;
134
- // 初始化实例hooker
135
- this.dataStore.initTrackerHooker(vdsConfig);
136
- // 手动初始化用户信息
137
- if (this.gioPlatform !== 'quickapp') {
138
- this.userStore.initUserInfo(options.trackingId);
139
- }
140
- // 广播SDK初始化完成
141
- this.emitter.emit(EMIT_MSG.SDK_INITIALIZED, vdsConfig);
142
- // 初始化完成后把实例注入到小程序全局
143
- if (!this.trackingId || this.trackingId === options.trackingId) {
144
- // ?直接挂载对象,存在问题:Page.data 触发深拷贝 存在循环引用会导致死循环
145
- getGlobal().__gio__ = () => this;
146
- }
147
- consoleText('Gio小程序SDK 初始化完成!', 'success');
148
- // 在插件中手动触发visit
149
- if (this.inPlugin && this.minipInstance.platform !== 'tbp') {
150
- this.dataStore.sendVisit(vdsConfig.trackingId);
151
- }
152
- if (this.vdsConfig.forceLogin) {
153
- consoleText(
154
- 'forceLogin已开启,请调用 identify 方法设置 openId 以继续上报!',
155
- 'info'
156
- );
157
- }
158
- return true;
159
- } catch (error) {
160
- consoleText(error, 'error');
161
- return false;
162
- }
163
- };
164
-
165
- // 需要校验实例的方法执行
166
- handlerDistribute = (trackingId: string, handler: string, args: any) => {
167
- const { initializedTrackingIds, getTrackerVds } = this.dataStore;
168
- const trackerVds = getTrackerVds(trackingId);
169
- if (trackerVds && initializedTrackingIds.includes(trackingId)) {
170
- return this[handler](trackingId, ...args);
171
- } else {
172
- consoleText(`不存在实例:${trackingId},请检查!`, 'warn');
173
- }
174
- };
175
-
176
- // 手动注册插件
177
- registerPlugins = (plugins: any) => {
178
- this.plugins.pluginItems = [...this.plugins.pluginItems, ...plugins];
179
- this.plugins.installAll(plugins);
180
- };
181
-
182
- // 获取所有已注册插件
183
- getPlugins = () => this.plugins.pluginItems;
184
-
185
- // 获取设备ID(匿名用户ID)
186
- getDeviceId = () => this.userStore.getUid();
187
-
188
- // 运行中获取配置
189
- getOption = (trackingId: string, k?: string) =>
190
- this.dataStore.getOption(trackingId, k);
191
-
192
- // 运行中修改配置
193
- setOption = (trackingId: string, k: string, v: any) => {
194
- if (keys(ALLOW_SET_OPTIONS).includes(k)) {
195
- if (typeof v === DEFAULT_SETTINGS[k]?.type) {
196
- this.dataStore.setOption(trackingId, k, v);
197
- consoleText(`已修改${ALLOW_SET_OPTIONS[k]}: ${v}`, 'info');
198
- return true;
199
- } else {
200
- consoleText(`参数格式不正确:${v},请检查后重试!`, 'error');
201
- return false;
202
- }
203
- } else {
204
- consoleText(`不存在可修改的配置项:${k},请检查后重试!`, 'error');
205
- return false;
206
- }
207
- };
208
-
209
- // 设置页面属性
210
- setPageAttributes = (trackingId: string, properties: any) => {
211
- if (isObject(properties) && !isEmpty(properties)) {
212
- const { currentPage } = this.dataStore.eventHooks;
213
- const path = currentPage.getPagePath();
214
- if (isEmpty(currentPage.pageProps[trackingId])) {
215
- currentPage.pageProps[trackingId] = {};
216
- }
217
- if (isEmpty(currentPage.pageProps[trackingId][path])) {
218
- currentPage.pageProps[trackingId][path] = {};
219
- }
220
- // 结果合并,动态属性不作处理
221
- if (!isEmpty(properties)) {
222
- forEach(properties, (v, k) => {
223
- if (
224
- !isFunction(currentPage.pageProps[trackingId][path][k]) ||
225
- isFunction(v)
226
- ) {
227
- currentPage.pageProps[trackingId][path][k] = v;
228
- }
229
- });
230
- }
231
- return true;
232
- } else {
233
- callError('setPageAttributes');
234
- return false;
235
- }
236
- };
237
-
238
- // 清空已设置的页面属性
239
- clearPageAttributes = (
240
- trackingId: string,
241
- properties: string[] | undefined
242
- ) => {
243
- try {
244
- if (
245
- !isEmpty(this.dataStore.eventHooks.currentPage.pageProps[trackingId])
246
- ) {
247
- const { currentPage } = this.dataStore.eventHooks;
248
- const path = currentPage.getPagePath();
249
- if (isArray(properties) && !isEmpty(properties)) {
250
- properties.forEach((propName: string) => {
251
- unset(currentPage.pageProps[trackingId][path], propName);
252
- });
253
- } else {
254
- currentPage.pageProps[trackingId][path] = {};
255
- }
256
- }
257
- } catch (error) {
258
- callError('clearPageAttributes');
259
- return false;
260
- }
261
- };
262
-
263
- // 手动发page
264
- sendPage = (trackingId: string, props?: any) => {
265
- const { trackPage } = this.dataStore.getTrackerVds(trackingId);
266
- if (!trackPage) {
267
- this.dataStore.sendPage(trackingId, props);
268
- } else {
269
- consoleText(
270
- '仅在关闭trackPage时允许调用sendPage,请确认后修改初始化配置项!',
271
- 'error'
272
- );
273
- }
274
- };
275
-
276
- // 设置设备ID,一般为openId
277
- identify = (trackingId: string, assignmentId: string | number) => {
278
- if (trackingId !== this.trackingId) {
279
- callError('identify', !1, '子实例不允许调用identify');
280
- return;
281
- }
282
- if (this.vdsConfig.forceLogin) {
283
- if (!verifyId(assignmentId)) {
284
- callError('identify');
285
- return;
286
- }
287
- // 截取长度
288
- const asId = toString(assignmentId).slice(0, 1000);
289
- // 在之后的请求中使用assignmentId作为uid(deviceId)使用
290
- this.userStore.setUid(asId);
291
- // 修改forceLogin配置项
292
- this.dataStore.setOption(this.trackingId, 'forceLogin', false);
293
- // 标记当前已被identify过
294
- this._identified = true;
295
- // 为已积压的请求使用assignmentId全部赋值deviceId
296
- this.dataStore.trackersExecute((tid: string) => {
297
- const hq = this.uploader.getHoardingQueue(tid);
298
- hq.forEach((o, i) => (hq[i].deviceId = asId));
299
- // 发送积压队列中的请求
300
- this.uploader.initiateRequest(tid, true);
301
- });
302
- } else if (this._identified) {
303
- callError('identify', !1, '请勿重复调用');
304
- } else {
305
- callError('identify', !1, 'forceLogin未开启');
306
- }
307
- };
308
-
309
- // 发送用户属性
310
- setUserAttributes = (trackingId: string, userAttributes: any) => {
311
- if (isObject(userAttributes) && !isEmpty(userAttributes)) {
312
- const { eventContextBuilder, eventInterceptor, lastVisitEvent } =
313
- this.dataStore;
314
- const event = {
315
- eventType: 'LOGIN_USER_ATTRIBUTES',
316
- ...eventContextBuilder(trackingId)
317
- };
318
- event.attributes = limitObject({
319
- ...(event.attributes ?? {}),
320
- ...getDynamicAttributes(userAttributes)
321
- });
322
- // 该方法很可能被放在在app的onShow中的wx.login异步回调中,
323
- // 此时页面的path和query可能还没取到,就拿visit事件的path和query补上
324
- if (!event.path) {
325
- event.path = lastVisitEvent[trackingId]?.path;
326
- event.query = lastVisitEvent[trackingId]?.query;
327
- event.title = lastVisitEvent[trackingId]?.title;
328
- }
329
- eventInterceptor(event);
330
- } else {
331
- callError('setUserAttributes');
332
- }
333
- };
334
-
335
- // 设置登录用户Id
336
- setUserId = (
337
- trackingId: string,
338
- userId: string | number,
339
- userKey?: string
340
- ) => {
341
- if (verifyId(userId)) {
342
- // 切换userId要重设session补发visit
343
- const prevId = this.userStore.getGioId(trackingId);
344
- userId = toString(userId).slice(0, 1000);
345
- userKey = toString(userKey).slice(0, 1000);
346
- this.userStore.setUserId(trackingId, userId);
347
- const processedUserKey =
348
- !isNil(userKey) && toString(userKey).length > 0
349
- ? toString(userKey).slice(0, 1000)
350
- : '';
351
- this.userStore.setUserKey(trackingId, processedUserKey);
352
- const { idMapping } = this.dataStore.getTrackerVds(trackingId);
353
- if (!idMapping && processedUserKey) {
354
- consoleText('您设置了 userKey ,请初始化开启 idMapping!', 'warn');
355
- }
356
- // 切换userId时重置session
357
- if (prevId && prevId !== userId) {
358
- this.userStore.setSessionId(trackingId);
359
- }
360
- } else {
361
- callError('setUserId');
362
- }
363
- };
364
-
365
- // 清除登录用户Id
366
- clearUserId = (trackingId: string) => {
367
- this.userStore.setUserId(trackingId, undefined);
368
- this.userStore.setUserKey(trackingId, undefined);
369
- };
370
-
371
- // 设置全局通用属性(即每个事件都会带上的属性值)
372
- setGeneralProps = (trackingId: string, properties: any) => {
373
- if (isObject(properties) && !isEmpty(properties)) {
374
- if (isEmpty(this.dataStore.generalProps[trackingId])) {
375
- this.dataStore.generalProps[trackingId] = {};
376
- }
377
- forEach(properties, (v, k) => {
378
- if (
379
- !isFunction(this.dataStore.generalProps[trackingId][k]) ||
380
- isFunction(v)
381
- ) {
382
- this.dataStore.generalProps[trackingId][k] = v;
383
- }
384
- });
385
- return true;
386
- } else {
387
- callError('setGeneralProps');
388
- return false;
389
- }
390
- };
391
-
392
- // 清空已设置的全局通用属性
393
- clearGeneralProps = (
394
- trackingId: string,
395
- properties: string[] | undefined
396
- ) => {
397
- try {
398
- if (!isEmpty(this.dataStore.generalProps[trackingId])) {
399
- if (isArray(properties) && !isEmpty(properties)) {
400
- properties.forEach((propName: string) => {
401
- unset(this.dataStore.generalProps[trackingId], propName);
402
- });
403
- } else {
404
- this.dataStore.generalProps[trackingId] = {};
405
- }
406
- }
407
- } catch (error) {
408
- callError('clearGeneralProps');
409
- return false;
410
- }
411
- };
412
-
413
- // 自定义埋点事件
414
- track = (
415
- trackingId: string,
416
- eventName: string,
417
- properties: { [key: string]: string | string[] }
418
- ) => {
419
- eventNameValidate(eventName, () => {
420
- const { eventContextBuilder, eventInterceptor } = this.dataStore;
421
- const event = {
422
- eventType: 'CUSTOM',
423
- eventName,
424
- ...eventContextBuilder(trackingId)
425
- };
426
- event.attributes = limitObject({
427
- ...(event.attributes ?? {}),
428
- ...getDynamicAttributes({
429
- ...(isObject(properties) && !isEmpty(properties) ? properties : {})
430
- })
431
- });
432
- // 埋点事件要保留'&&sendTo'字段用于多实例复制发送
433
- if (
434
- this.plugins.gioMultipleInstances &&
435
- properties &&
436
- properties['&&sendTo']
437
- ) {
438
- event['&&sendTo'] = properties['&&sendTo'];
439
- }
440
- eventInterceptor(event);
441
- });
442
- };
443
-
444
- // 初始化事件计时器
445
- trackTimerStart = (trackingId: string, eventName: string) => {
446
- const { dataCollect } = this.dataStore.getTrackerVds(trackingId);
447
- if (dataCollect) {
448
- return eventNameValidate(eventName, () => {
449
- const timerId = guid();
450
- if (!this.dataStore.trackTimers[trackingId]) {
451
- this.dataStore.trackTimers[trackingId] = {};
452
- }
453
- this.dataStore.trackTimers[trackingId][timerId] = {
454
- eventName,
455
- leng: 0,
456
- start: +Date.now()
457
- };
458
- return timerId;
459
- });
460
- } else {
461
- consoleText('指定实例未开启数据采集,请检查!', 'error');
462
- }
463
- return false;
464
- };
465
-
466
- // 暂停事件计时器
467
- trackTimerPause = (trackingId: string, timerId: string) => {
468
- const timers = this.dataStore.trackTimers[trackingId];
469
- if (timerId && timers && timers[timerId]) {
470
- const timer = timers[timerId];
471
- if (timer.start) {
472
- timer.leng = timer.leng + (+Date.now() - timer.start);
473
- }
474
- timer.start = 0;
475
- return true;
476
- }
477
- return false;
478
- };
479
-
480
- // 恢复事件计时器
481
- trackTimerResume = (trackingId: string, timerId: string) => {
482
- const timers = this.dataStore.trackTimers[trackingId];
483
- if (timerId && timers && timers[timerId]) {
484
- const timer = timers[timerId];
485
- if (timer.start === 0) {
486
- timer.start = +Date.now();
487
- }
488
- return true;
489
- }
490
- return false;
491
- };
492
-
493
- // 停止事件计时器并上报事件
494
- trackTimerEnd = (trackingId: string, timerId: string, properties: any) => {
495
- const { dataCollect } = this.dataStore.getTrackerVds(trackingId);
496
- const timers = this.dataStore.trackTimers[trackingId];
497
- if (timerId && timers && timers[timerId]) {
498
- if (dataCollect) {
499
- const maxEnd = 60 * 60 * 24 * 1000;
500
- const timer = timers[timerId];
501
- if (timer.start !== 0) {
502
- const shortCut = +Date.now() - timer.start;
503
- timer.leng = shortCut > 0 ? timer.leng + shortCut : 0;
504
- }
505
- const { eventContextBuilder, eventInterceptor } = this.dataStore;
506
- const event = {
507
- eventType: 'CUSTOM',
508
- eventName: timer.eventName,
509
- ...eventContextBuilder(trackingId)
510
- };
511
- event.attributes = limitObject({
512
- ...(event.attributes ?? {}),
513
- ...getDynamicAttributes({
514
- ...(isObject(properties) && !isEmpty(properties) ? properties : {}),
515
- event_duration: timer.leng > maxEnd ? 0 : timer.leng / 1000
516
- })
517
- });
518
- // 埋点事件要保留'&&sendTo'字段用于多实例复制发送
519
- if (
520
- this.plugins.gioMultipleInstances &&
521
- properties &&
522
- properties['&&sendTo']
523
- ) {
524
- event['&&sendTo'] = properties['&&sendTo'];
525
- }
526
- eventInterceptor(event);
527
- return true;
528
- } else {
529
- consoleText('指定实例未开启数据采集,计时器已移除,请检查!', 'error');
530
- return false;
531
- }
532
- this.removeTimer(trackingId, timerId);
533
- } else {
534
- consoleText('未查询到指定计时器,请检查!', 'error');
535
- return false;
536
- }
537
- };
538
-
539
- // 移除事件计时器
540
- removeTimer = (trackingId: string, timerId: string) => {
541
- const timers = this.dataStore.trackTimers[trackingId];
542
- if (timerId && timers && timers[timerId]) {
543
- delete timers[timerId];
544
- return true;
545
- } else {
546
- consoleText('未查询到指定计时器,请检查!', 'error');
547
- return false;
548
- }
549
- };
550
-
551
- // 清除所有事件计时器
552
- clearTrackTimer = (trackingId: string) => {
553
- this.dataStore.trackTimers[trackingId] = {};
554
- };
555
-
556
- // 手动获取位置信息补发visit上报
557
- setLocation = (trackingId: string, latitude: number, longitude: number) => {
558
- if (isEmpty(this.dataStore.locationData[trackingId])) {
559
- this.dataStore.locationData[trackingId] = {};
560
- }
561
- const ld = this.dataStore.locationData[trackingId];
562
- const verifyLT = (o: number) => o >= -180 && o <= 180;
563
- if (
564
- verifyLT(latitude) &&
565
- verifyLT(longitude) &&
566
- (ld.latitude !== latitude || ld.longitude !== longitude)
567
- ) {
568
- this.dataStore.locationData[trackingId] = { latitude, longitude };
569
- }
570
- };
571
-
572
- // 手动更新曝光监听(该方法会在曝光插件加载时被重写)
573
- updateImpression = () => {
574
- consoleText(
575
- 'updateImpression 错误! 请集成半自动埋点浏览插件后重试!',
576
- 'error'
577
- );
578
- };
579
-
580
- // 获取A/B实验数据
581
- getABTest = (trackingId: string, layerId: string | number, callback: any) => {
582
- consoleText('获取ABTest数据错误! 请集成ABTest插件后重试!', 'error');
583
- niceCallback(callback, {});
584
- };
585
-
586
- // 获取打通信息
587
- getGioInfo = (trackingId: string) => {
588
- const { getUid, getUserId, getUserKey, getSessionId } = this.userStore;
589
- const {
590
- projectId,
591
- appId,
592
- dataSourceId,
593
- dataCollect,
594
- idMapping,
595
- extraParams,
596
- ignoreFields
597
- } = this.dataStore.getTrackerVds(trackingId);
598
- const infos: any = {
599
- gioappid: appId,
600
- giocs1: getUserId(trackingId) || '',
601
- gioplatform: this.platformConfig.platform,
602
- gioprojectid: projectId,
603
- gios: getSessionId(trackingId),
604
- giou: getUid()
605
- };
606
- infos.giodatacollect = dataCollect;
607
- infos.giodatasourceid = dataSourceId;
608
- if (idMapping) {
609
- infos.giouserkey = getUserKey(trackingId);
610
- }
611
- if (!isEmpty(extraParams)) {
612
- extraParams.forEach((o) => {
613
- if (!ignoreFields.includes(o)) {
614
- infos[`gio${o}`] = (this.dataStore.lastVisitEvent[trackingId] ?? {})[
615
- o
616
- ];
617
- }
618
- });
619
- }
620
- // 只有主实例有可能会带圈选的字段
621
- if (trackingId === this.trackingId) {
622
- // 小程序处于圈选状态时,把圈选上报地址带给web
623
- const { circleServerUrl, circleOpen } =
624
- this.plugins?.gioEventAutoTracking ?? {};
625
- if (circleServerUrl && circleOpen) {
626
- // infos.giocircleserverurl = encodeURIComponent(circleServerUrl);
627
- const circleParam: any = qsParse(circleServerUrl.split('?')[1]);
628
- if (circleParam.roomId) {
629
- infos.giocircleroomid = circleParam.roomId;
630
- }
631
- }
632
- }
633
- return qs.stringify(infos);
634
- };
635
- }
636
-
637
- export default GrowingIO;