gio-miniprogram-sdk 4.4.2 → 4.5.0

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 (100) hide show
  1. package/README.md +13 -11
  2. package/dist/alipay.js +1 -1
  3. package/dist/baidu.js +1 -1
  4. package/dist/bytedance.js +1 -1
  5. package/dist/jingdong.js +1 -1
  6. package/dist/kuaishou.js +1 -1
  7. package/dist/mpx.js +1 -0
  8. package/dist/plugins/gioABTest.js +1 -1
  9. package/dist/plugins/gioCompress.js +1 -1
  10. package/dist/plugins/gioEventAutoTracking.js +1 -1
  11. package/dist/plugins/gioImpressionTracking.js +1 -1
  12. package/dist/plugins/gioMultipleInstances.js +1 -1
  13. package/dist/plugins/gioPerformance.js +1 -1
  14. package/dist/qq.js +1 -1
  15. package/dist/quickapp.js +1 -1
  16. package/dist/taobao.js +1 -1
  17. package/dist/taro.js +1 -1
  18. package/dist/uniapp.js +1 -1
  19. package/dist/wechat.js +1 -1
  20. package/dist/xiaohongshu.js +1 -1
  21. package/package.json +13 -2
  22. package/.eslintignore +0 -11
  23. package/.eslintrc.js +0 -120
  24. package/.github/workflows/greetings.yml +0 -13
  25. package/.github/workflows/publish.yml +0 -45
  26. package/.prettierignore +0 -19
  27. package/.prettierrc.js +0 -9
  28. package/babel.config.js +0 -6
  29. package/rollup.config.js +0 -100
  30. package/rollup.plugins.config.js +0 -84
  31. package/src/adapters/gioTaobaoAdapter.ts +0 -202
  32. package/src/adapters/gioTaroAdapter.ts +0 -467
  33. package/src/adapters/gioUniAppAdapter.ts +0 -431
  34. package/src/constants/config.ts +0 -170
  35. package/src/constants/emitMsg.ts +0 -24
  36. package/src/constants/regex.ts +0 -18
  37. package/src/core/dataStore/eventContextBuilder.ts +0 -95
  38. package/src/core/dataStore/eventHooks/appEffects.ts +0 -218
  39. package/src/core/dataStore/eventHooks/index.ts +0 -468
  40. package/src/core/dataStore/eventHooks/minipPage.ts +0 -221
  41. package/src/core/dataStore/eventHooks/pageEffects.ts +0 -189
  42. package/src/core/dataStore/index.ts +0 -661
  43. package/src/core/growingIO.ts +0 -637
  44. package/src/core/pluginsInstance/base.ts +0 -157
  45. package/src/core/pluginsInstance/taro.ts +0 -15
  46. package/src/core/pluginsInstance/tb.ts +0 -15
  47. package/src/core/pluginsInstance/uniapp.ts +0 -15
  48. package/src/core/uploader.ts +0 -272
  49. package/src/core/userStore.ts +0 -267
  50. package/src/index.ts +0 -77
  51. package/src/minipInstance/base.ts +0 -352
  52. package/src/minipInstance/framework.ts +0 -24
  53. package/src/minipInstance/jd.ts +0 -11
  54. package/src/minipInstance/ks.ts +0 -12
  55. package/src/minipInstance/my.ts +0 -131
  56. package/src/minipInstance/qq.ts +0 -12
  57. package/src/minipInstance/quickapp.ts +0 -233
  58. package/src/minipInstance/swan.ts +0 -20
  59. package/src/minipInstance/tb.ts +0 -58
  60. package/src/minipInstance/tt.ts +0 -12
  61. package/src/minipInstance/wx.ts +0 -27
  62. package/src/minipInstance/xhs.ts +0 -20
  63. package/src/platformConfig/base.ts +0 -82
  64. package/src/platformConfig/framework.ts +0 -31
  65. package/src/platformConfig/jd.ts +0 -11
  66. package/src/platformConfig/ks.ts +0 -11
  67. package/src/platformConfig/my.ts +0 -16
  68. package/src/platformConfig/qq.ts +0 -13
  69. package/src/platformConfig/quickapp.ts +0 -48
  70. package/src/platformConfig/swan.ts +0 -11
  71. package/src/platformConfig/tb.ts +0 -29
  72. package/src/platformConfig/tt.ts +0 -11
  73. package/src/platformConfig/wx.ts +0 -20
  74. package/src/platformConfig/xhs.ts +0 -27
  75. package/src/plugins/gioABTest.ts +0 -348
  76. package/src/plugins/gioCompress.ts +0 -253
  77. package/src/plugins/gioEventAutoTracking.ts +0 -310
  78. package/src/plugins/gioImpressionTracking.ts +0 -386
  79. package/src/plugins/gioMultipleInstances.ts +0 -189
  80. package/src/plugins/gioPerformance/exception.ts +0 -152
  81. package/src/plugins/gioPerformance/monitor.ts +0 -284
  82. package/src/plugins/gioPerformance/network.ts +0 -197
  83. package/src/plugins/gioPerformance.ts +0 -131
  84. package/src/types/base.ts +0 -167
  85. package/src/types/dataStore.ts +0 -62
  86. package/src/types/eventHooks.ts +0 -146
  87. package/src/types/gioCustomTracking.ts +0 -9
  88. package/src/types/gioEventTracking.ts +0 -10
  89. package/src/types/gioPerformance.ts +0 -8
  90. package/src/types/growingIO.ts +0 -137
  91. package/src/types/minipInstance.ts +0 -133
  92. package/src/types/platforms.ts +0 -64
  93. package/src/types/plugins.ts +0 -24
  94. package/src/types/uploader.ts +0 -30
  95. package/src/types/userStore.ts +0 -28
  96. package/src/utils/glodash.ts +0 -430
  97. package/src/utils/tools.ts +0 -511
  98. package/src/utils/verifiers.ts +0 -52
  99. package/tsconfig.json +0 -32
  100. package/typings/global.d.ts +0 -105
@@ -1,468 +0,0 @@
1
- import { EventHooksType, MinipPageType } from '@@/types/eventHooks';
2
- import { GrowingIOType } from '@@/types/growingIO';
3
- import { has, isArray, isEmpty, isFunction, typeOf } from '@@/utils/glodash';
4
- import { consoleText, getGlobal, niceTry } from '@@/utils/tools';
5
-
6
- import AppEffects from './appEffects';
7
- import MinipPage from './minipPage';
8
- import PageEffects from './pageEffects';
9
- import EMIT_MSG from '@@/constants/emitMsg';
10
-
11
- class EventHooks implements EventHooksType {
12
- private appHooked: boolean;
13
- private pageHooked: boolean;
14
- private componentHooked: boolean;
15
- private behaviorHooked: boolean;
16
- public defAppCbs: any;
17
- public defPageCbs: any;
18
- public appHandlers: string[];
19
- public pageHandlers: string[];
20
- public shareEventTypes: string[];
21
- public actionEventTypes: string[];
22
- public originalApp: (any: any) => any;
23
- public originalPage: (any: any) => any;
24
- public originalComponent: (any: any) => any;
25
- public originalBehavior: (any: any) => any;
26
- public appEffects: any;
27
- public pageEffects: any;
28
- public actionEffects: any;
29
- public currentPage: MinipPageType;
30
-
31
- constructor(public growingIO: GrowingIOType) {
32
- const config = this.growingIO?.platformConfig;
33
- this.defAppCbs = {};
34
- this.defPageCbs = {};
35
- this.appHandlers = config.appHandlers;
36
- this.pageHandlers = config.pageHandlers;
37
- this.shareEventTypes = config.shareEventTypes;
38
- this.actionEventTypes = config.actionEventTypes;
39
- this.appEffects = new AppEffects(this.growingIO);
40
- this.pageEffects = new PageEffects(this.growingIO);
41
- this.currentPage = new MinipPage(this.growingIO);
42
- }
43
-
44
- // 判断是否为非构造函数的一个函数
45
- isNormalFc = (key: string, method: any) => {
46
- return isFunction(method) && key !== 'constructor';
47
- };
48
-
49
- // 对象的遍历
50
- objectTraverse = (target: any, fn: any) => {
51
- Object.getOwnPropertyNames(target).forEach((k: string) => fn(k, target));
52
- if (has(target, 'methods')) {
53
- Object.getOwnPropertyNames(target.methods).forEach((k: string) => {
54
- if (!target[k]) {
55
- fn(k, target.methods);
56
- }
57
- });
58
- }
59
- if (has(target, 'lifetimes')) {
60
- Object.getOwnPropertyNames(target.lifetimes).forEach((k: string) => {
61
- if (!target[k]) {
62
- fn(k, target.lifetimes);
63
- }
64
- });
65
- }
66
- if (has(target, 'pageLifetimes')) {
67
- Object.getOwnPropertyNames(target.pageLifetimes).forEach((k: string) => {
68
- if (!target[k]) {
69
- fn(k, target.pageLifetimes);
70
- }
71
- });
72
- }
73
- };
74
-
75
- // 补充生命周期函数
76
- supLifeFcs = (target: any, type: 'app' | 'page') => {
77
- if (isArray(this[`${type}Handlers`])) {
78
- this[`${type}Handlers`].forEach((k: string) => {
79
- // 除分享以外
80
- if (
81
- !isFunction(target[k]) &&
82
- !['onShareAppMessage', 'onShareTimeline'].includes(k)
83
- ) {
84
- target[k] = () => {}; // eslint-disable-line
85
- }
86
- });
87
- }
88
- };
89
-
90
- /**
91
- * 执行生命周期回调并广播事件
92
- */
93
- lifeFcEffectsFn = (instance, args, cType, eventName, result = undefined) => {
94
- // 创建参数数组副本
95
- const argsArray =
96
- typeOf(args) === 'array' ? Array.prototype.slice.call(args) : [];
97
- // 如果有结果,添加到参数列表
98
- if (!isEmpty(result)) {
99
- argsArray.push(result);
100
- }
101
-
102
- // 执行effects
103
- this[`def${cType}Cbs`][eventName].apply(instance, argsArray);
104
-
105
- // 广播生命周期
106
- this.growingIO.emitter.emit(EMIT_MSG.MINIP_LIFECYCLE, {
107
- event: `${cType} ${eventName}End`,
108
- timestamp: Date.now(),
109
- params: { instance, arguments: Array.from(argsArray) }
110
- });
111
-
112
- return result;
113
- };
114
-
115
- /**
116
- * 生命周期方法effects
117
- */
118
- lifeFcEffects = (eventName: string, method: any, cType: 'App' | 'Page') => {
119
- const self = this;
120
-
121
- return function (...args) {
122
- let result;
123
-
124
- // 分享事件单独处理
125
- if (self.shareEventTypes.includes(eventName)) {
126
- // 先执行分享的生命周期拿到分享数据
127
- const originResult = method.apply(this, args) ?? {};
128
-
129
- // sdk跟踪分享事件时才处理,否则直接返回原结果
130
- if (!self.growingIO.vdsConfig.followShare) {
131
- return originResult;
132
- }
133
- const isPromiseResult = typeOf(originResult) === 'promise';
134
- const hasPromiseProperty = typeOf(originResult.promise) === 'promise';
135
- // 支付宝的promise会被提前处理过,通过以下两个字段判断是否需要自定义promise
136
- const isSpecialPromise =
137
- has(originResult, '_result') && has(originResult, '_state');
138
-
139
- // 处理异步分享结果
140
- if (isPromiseResult || hasPromiseProperty || isSpecialPromise) {
141
- // 创建处理Promise结果的通用函数
142
- const handlePromiseResult = (promiseResult = {}) => {
143
- try {
144
- // 合并原始结果和Promise结果
145
- const mergedResult = {
146
- ...originResult,
147
- ...promiseResult
148
- };
149
-
150
- // 更新分享内容
151
- const updatedResult =
152
- self.currentPage.updateShareResult(mergedResult);
153
-
154
- // 执行回调
155
- self.lifeFcEffectsFn(this, args, cType, eventName, updatedResult);
156
- } catch (error) {
157
- consoleText(error, 'error');
158
- }
159
- };
160
-
161
- // 处理整个返回值是Promise的情况
162
- if (isPromiseResult) {
163
- // 如果三秒内不resolve结果,分享会使用原默认参数
164
- Promise.race([
165
- originResult,
166
- new Promise((resolve) => {
167
- setTimeout(() => {
168
- resolve({});
169
- }, 3000);
170
- })
171
- ])
172
- .then(handlePromiseResult)
173
- .catch((error) => consoleText(error, 'error'));
174
- }
175
-
176
- // 处理返回值中包含promise属性的情况
177
- if (hasPromiseProperty) {
178
- // 如果三秒内不resolve结果,分享会使用原默认参数
179
- Promise.race([
180
- originResult.promise,
181
- new Promise((resolve) => {
182
- setTimeout(() => {
183
- resolve({});
184
- }, 3000);
185
- })
186
- ])
187
- .then(handlePromiseResult)
188
- .catch((error) => consoleText(error, 'error'));
189
- }
190
-
191
- // 处理支付宝特有的promise
192
- if (isSpecialPromise) {
193
- self.growingIO.minipInstance?.handleSharePromise(
194
- originResult,
195
- handlePromiseResult
196
- );
197
- }
198
-
199
- // 直接返回原分享结果继续执行原逻辑
200
- result = self.currentPage.updateShareResult(originResult, false);
201
- } else {
202
- // 没有异步逻辑,直接更新分享内容执行回调
203
- result = self.currentPage.updateShareResult(originResult);
204
- self.lifeFcEffectsFn(this, args, cType, eventName, result);
205
- }
206
- } else {
207
- // 其他生命周期事件:先执行回调,再执行原方法
208
- self.lifeFcEffectsFn(this, args, cType, eventName);
209
- result = method.apply(this, args);
210
- }
211
-
212
- return result;
213
- };
214
- };
215
-
216
- /*
217
- * 自定义方法effects
218
- */
219
- customFcEffects = (eventName: string, method: any) => {
220
- const self = this;
221
- return function (...args) {
222
- let result;
223
- if (
224
- self.growingIO.vdsConfig.autotrack &&
225
- self.growingIO.plugins.gioEventAutoTracking
226
- ) {
227
- try {
228
- let event: any = args[0] || {};
229
- // 有的框架的点击事件传参时需要在最后一个参数加上原生event才能触发SDK的事件
230
- if (
231
- !(has(event, 'type') && (event?.currentTarget || event?.target))
232
- ) {
233
- event = args[args.length - 1];
234
- }
235
- if (
236
- (event?.currentTarget || event?.target) &&
237
- self.actionEventTypes.includes(event.type)
238
- ) {
239
- // 无埋点事件插件未挂载时进行挂载
240
- if (!isFunction(self.actionEffects)) {
241
- self.actionEffects =
242
- self.growingIO.plugins?.gioEventAutoTracking?.main ||
243
- function () {}; //eslint-disable-line
244
- }
245
- self?.actionEffects(event, eventName);
246
- }
247
- } catch (error) {
248
- consoleText(error, 'error');
249
- }
250
- }
251
- // 执行原逻辑
252
- result = method.apply(this, args);
253
- return result;
254
- };
255
- };
256
-
257
- // App中方法代理
258
- appApplyProxy = (eventName: string, method: any) => {
259
- if (this.appHandlers.includes(eventName)) {
260
- return this.lifeFcEffects(eventName, method, 'App');
261
- } else {
262
- return this.customFcEffects(eventName, method);
263
- }
264
- };
265
-
266
- // Page中方法代理
267
- pageApplyProxy = (eventName: string, method: any) => {
268
- if (this.pageHandlers.includes(eventName)) {
269
- return this.lifeFcEffects(eventName, method, 'Page');
270
- } else {
271
- return this.customFcEffects(eventName, method);
272
- }
273
- };
274
-
275
- // App重写遍历
276
- appOverriding = (options: any) => {
277
- this.setAppEffectCbs();
278
- const fn = (k: string, target: any) => {
279
- if (this.isNormalFc(k, target[k])) {
280
- target[k] = this.appApplyProxy(k, target[k]);
281
- }
282
- };
283
- // 补充未定义的生命周期,防止不触发vst和cls事件(vst依赖onShow、cls依赖onHide)
284
- this.supLifeFcs(options, 'app');
285
- // 遍历方法,进行重写
286
- this.objectTraverse(options, fn);
287
- return options;
288
- };
289
-
290
- // Page重写遍历
291
- pageOverriding = (options: any) => {
292
- this.setPageEffectCbs();
293
- const fn = (k: string, target: any) => {
294
- if (this.isNormalFc(k, target[k])) {
295
- target[k] = this.pageApplyProxy(k, target[k]);
296
- }
297
- };
298
- // 补充未定义的生命周期,防止不触发page事件(page依赖onShow)
299
- this.supLifeFcs(options, 'page');
300
- // 遍历方法,进行重写
301
- this.objectTraverse(options, fn);
302
- return options;
303
- };
304
-
305
- // Component/Behavior重写(重写除构造函数之外的所有方法)
306
- componentOverriding = (options: any) => {
307
- this.setPageEffectCbs();
308
- const fn = (k: string, target: any) => {
309
- if (this.isNormalFc(k, target[k])) {
310
- target[k] = this.pageApplyProxy(k, target[k]);
311
- }
312
- };
313
- if (!options.methods) {
314
- options.methods = {};
315
- }
316
- // 补充生命周期方法
317
- this.supLifeFcs(
318
- this.growingIO.inPlugin ? options : options.methods,
319
- 'page'
320
- );
321
- // 遍历重写方法
322
- this.objectTraverse(
323
- this.growingIO.inPlugin ? options : options.methods,
324
- fn
325
- );
326
- return options;
327
- };
328
-
329
- // 挂载App生命周期的effect方法
330
- setAppEffectCbs = () => {
331
- const self = this;
332
- this.appHandlers.forEach((handlerName: string) => {
333
- this.defAppCbs[handlerName] = function (...args) {
334
- self.appEffects.main(handlerName, args);
335
- };
336
- });
337
- };
338
-
339
- // 挂载Page/Component生命周期的effect方法
340
- setPageEffectCbs = () => {
341
- const self = this;
342
- this.pageHandlers.forEach((handlerName: string) => {
343
- this.defPageCbs[handlerName] = function (...args) {
344
- self.pageEffects.main(this, handlerName, args);
345
- };
346
- });
347
- };
348
-
349
- growingApp = (app: any) =>
350
- isFunction(this.originalApp)
351
- ? this.originalApp(this.appOverriding(app))
352
- : this.appOverriding(app);
353
-
354
- growingPage = (page: any) =>
355
- isFunction(this.originalPage)
356
- ? this.originalPage(this.pageOverriding(page))
357
- : this.pageOverriding(page);
358
-
359
- growingComponent = (component: any) =>
360
- isFunction(this.originalComponent)
361
- ? this.originalComponent(this.componentOverriding(component))
362
- : this.componentOverriding(component);
363
-
364
- growingBehavior = (behavior: any) =>
365
- isFunction(this.originalBehavior)
366
- ? this.originalBehavior(this.componentOverriding(behavior))
367
- : this.componentOverriding(behavior);
368
-
369
- nativeGrowing = (
370
- designated: string[] = ['App', 'Page', 'Component', 'Behavior']
371
- ) => {
372
- const self = this;
373
- const { platformConfig } = this.growingIO;
374
- const platformHooks = platformConfig.hooks;
375
- if (designated.includes('App')) {
376
- try {
377
- // 重写App
378
- if (platformHooks.App && !this.appHooked) {
379
- App = function (...args) {
380
- return self.originalApp(self.appOverriding(args[0]));
381
- };
382
- this.appHooked = true;
383
- }
384
- } catch (error) {
385
- // 在插件中可能会不存在App
386
- }
387
- }
388
- if (designated.includes('Page')) {
389
- try {
390
- // 重写Page
391
- if (platformHooks.Page && !this.pageHooked) {
392
- Page = function (...args) {
393
- return self.originalPage(self.pageOverriding(args[0]));
394
- };
395
- this.pageHooked = true;
396
- }
397
- } catch (error) {
398
- // 在插件中可能会不存在Page
399
- }
400
- }
401
- if (designated.includes('Component')) {
402
- try {
403
- // 重写Component
404
- if (platformHooks.Component && !this.componentHooked) {
405
- Component = function (...args) {
406
- return self.originalComponent(self.componentOverriding(args[0]));
407
- };
408
- this.componentHooked = true;
409
- }
410
- } catch (error) {
411
- //
412
- }
413
- }
414
- if (designated.includes('Behavior')) {
415
- try {
416
- // 重写Behavior
417
- if (platformHooks.Behavior && !this.behaviorHooked) {
418
- Behavior = function (...args) {
419
- return self.originalBehavior(self.componentOverriding(args[0]));
420
- };
421
- this.behaviorHooked = true;
422
- }
423
- } catch (error) {
424
- // 部分小程序不存在Behavior
425
- }
426
- }
427
- };
428
-
429
- // 初始化原始值
430
- initOriginalValue = () => {
431
- this.setAppEffectCbs();
432
- this.setPageEffectCbs();
433
- this.originalApp = niceTry(() => App || global.App);
434
- this.originalPage = niceTry(() => Page || global.Page);
435
- this.originalComponent = niceTry(() => Component || global.Component);
436
- this.originalBehavior = niceTry(() => Behavior || global.Behavior);
437
- };
438
-
439
- // 初始化事件钩子
440
- initEventHooks = () => {
441
- const self = this;
442
- const { platformConfig, gioPlatform } = this.growingIO;
443
- this.initOriginalValue();
444
- if (platformConfig.canHook) {
445
- this.nativeGrowing();
446
- getGlobal().GioApp = niceTry(() => App ?? this.originalApp);
447
- getGlobal().GioPage = niceTry(() => Page ?? this.originalPage);
448
- getGlobal().GioComponent = niceTry(
449
- () => Component ?? this.originalComponent
450
- );
451
- getGlobal().GioBehavior = niceTry(
452
- () => Behavior ?? this.originalBehavior
453
- );
454
- } else if (gioPlatform === 'quickapp') {
455
- getGlobal().GioApp = function (...args) {
456
- return self.appOverriding(args[0]);
457
- };
458
- getGlobal().GioPage = function (...args) {
459
- return self.pageOverriding(args[0]);
460
- };
461
- getGlobal().GioComponent = function (...args) {
462
- return self.pageOverriding(args[0]);
463
- };
464
- }
465
- };
466
- }
467
-
468
- export default EventHooks;
@@ -1,221 +0,0 @@
1
- import { compact, has, isEmpty, keys, last, unset } from '@@/utils/glodash';
2
- import { GrowingIOType } from '@@/types/growingIO';
3
- import { MinipPageType, PageShareResult } from '@@/types/eventHooks';
4
- import {
5
- qsStringify,
6
- normalPath,
7
- splitPath,
8
- qsParse,
9
- niceTry,
10
- getDynamicAttributes,
11
- limitObject,
12
- getPureParams
13
- } from '@@/utils/tools';
14
- import { EVENT } from '@@/types/base';
15
-
16
- class MinipPage implements MinipPageType {
17
- // 根据页面存储的页面参数
18
- public queryOptions: any;
19
- // 通过调用wx.setNavigationBarTitle设置的title
20
- public configuredTitle: any;
21
- // 页面级属性
22
- public pageProps: any;
23
- // 上一个执行的小程序生命周期
24
- public lastLifecycle: string;
25
- // 当前执行的小程序生命周期
26
- public currentLifecycle: string;
27
- // 当前生命周期是否在页面显示前
28
- public lifeBeforeShow = true;
29
-
30
- constructor(public growingIO: GrowingIOType) {
31
- this.queryOptions = {};
32
- this.configuredTitle = {};
33
- this.pageProps = {};
34
- }
35
-
36
- /**
37
- * 获取当前页面路径
38
- * 优先级:页面堆栈路径 > 启动参数路径 > 最近 page 事件路径 > 最近 visit 事件路径
39
- * 插件环境下无页面时返回插件 appId
40
- */
41
- getPagePath = () => {
42
- const { minipInstance, inPlugin, dataStore, trackingId } = this.growingIO;
43
- const path = minipInstance.getCurrentPath();
44
- if (inPlugin) {
45
- const vds = dataStore.getTrackerVds(trackingId);
46
- // 插件中没有页面时直接将插件appId作为页面信息
47
- return path || `/插件${vds.appId}`;
48
- } else {
49
- return (
50
- path ||
51
- dataStore.eventHooks.appEffects.enterParams?.path ||
52
- // 作为页面组件时component created生命周期中的埋点会取不到页面堆栈,所以拿最近的一个page事件中的页面信息做兜底
53
- dataStore.lastPageEvent[trackingId]?.path ||
54
- // 没有最近的一个page事件说明在app初始化的生命周期中,有些框架可能会有异步处理逻辑导致事件触发比visit晚,则使用visit事件中的页面信息兜底
55
- dataStore.lastVisitEvent[trackingId]?.path
56
- );
57
- }
58
- };
59
-
60
- /**
61
- * 获取当前页面参数(query)
62
- * 优先级:页面堆栈参数 > 生命周期参数 > 启动参数 > 最近 page 事件参数 > 最近 visit 事件参数
63
- */
64
- getPageQuery = () => {
65
- const { minipInstance, dataStore, trackingId } = this.growingIO;
66
- const stackPage = minipInstance.getCurrentPage();
67
- const stackPath = minipInstance.getCurrentPath();
68
- const { enterParams = {} } = dataStore.eventHooks.appEffects;
69
- if (stackPath) {
70
- const pageParams = stackPage.options ?? stackPage.$taroParams;
71
- // 堆栈中有页面信息,则使用堆栈中的页面参数
72
- if (!isEmpty(pageParams)) {
73
- // 有页面解析的 option 直接取
74
- return qsStringify(getPureParams(pageParams));
75
- } else if (!isEmpty(this.queryOptions[stackPath])) {
76
- // 没有 option 从 hook 获取的生命周期参数中取
77
- return qsStringify(getPureParams(this.queryOptions[stackPath]));
78
- } else if (stackPath === enterParams.path) {
79
- // 某些小程序虽然有页面堆栈,但是页面还没加载完,兜底取启动参数
80
- return qsStringify(
81
- getPureParams(
82
- enterParams?.query,
83
- enterParams?.referrerInfo?.extraData
84
- )
85
- );
86
- }
87
- } else if (enterParams.path) {
88
- // 堆栈中没有页面信息,但启动参数中有,说明是小程序刚启动还没加载页面,直接取启动参数
89
- return qsStringify(
90
- getPureParams(enterParams?.query, enterParams?.referrerInfo?.extraData)
91
- );
92
- } else {
93
- // 作为页面组件时 component created 生命周期中的埋点会取不到页面堆栈,所以拿最近的一个 page 事件中的页面信息做兜底
94
- return dataStore.lastPageEvent[trackingId]?.path
95
- ? dataStore.lastPageEvent[trackingId]?.query
96
- : // 没有最近的一个 page 事件说明在 app 初始化的生命周期中,有些框架可能会有异步处理逻辑导致事件触发比 visit 晚,则使用 visit 事件中的页面信息兜底
97
- dataStore.lastVisitEvent[trackingId]?.query;
98
- }
99
- };
100
-
101
- /**
102
- * 获取页面标题
103
- * 优先级:setNavigationBarTitle > data.gioPageTitle > config.js 中设置的值 > tabBar 配置
104
- * 插件环境下无页面时返回插件 appId
105
- * @param trackingId 可选,追踪 id
106
- */
107
- getPageTitle = (trackingId?: string) => {
108
- const { minipInstance, inPlugin, dataStore } = this.growingIO;
109
- const path = minipInstance.getCurrentPath();
110
- const title =
111
- this.configuredTitle[path] ||
112
- minipInstance.getPageTitle(minipInstance.getCurrentPage());
113
- if (inPlugin) {
114
- const vds = dataStore.getTrackerVds(
115
- trackingId || this.growingIO.trackingId
116
- );
117
- // 插件中没有页面时直接将插件appId作为页面信息
118
- return (
119
- title || `/插件${vds.appId}${path ? '_' + last(path.split('/')) : ''}`
120
- );
121
- } else {
122
- return title;
123
- }
124
- };
125
-
126
- /**
127
- * 获取页面来源(referrer)
128
- * 优先级:最近 page 事件路径 > 启动参数 referrerInfo.appId > scene 场景值
129
- * @param trackingId 追踪 id
130
- */
131
- getReferralPage = (trackingId: string) => {
132
- const {
133
- minipInstance,
134
- dataStore: { lastPageEvent, scene, eventHooks }
135
- } = this.growingIO;
136
- return (
137
- lastPageEvent[trackingId]?.path ||
138
- eventHooks.appEffects.enterParams?.referrerInfo?.appId ||
139
- (scene ? `scn:${minipInstance.scnPrefix}${scene}` : null)
140
- );
141
- };
142
-
143
- /**
144
- * 处理分享参数,生成分享用的 path 和 query
145
- * 优先级:result.query > result.path > 当前页面参数(去除 utm_ 开头参数)
146
- * @param result PageShareResult 客户自定义的参数
147
- * @returns [path, queryString]
148
- */
149
- buildShareQuery(result: PageShareResult): [string, string] {
150
- // 自定义 path 的地址参数截取(onShareAppMessage 中的页面参数是拼在地址中的)
151
- const customSplit = splitPath(result.path ?? '');
152
-
153
- let path = result.path || this.getPagePath();
154
- let parsedQuery: any = {};
155
- let resultHasPath = has(result, 'path');
156
- let resultHasQuery = has(result, 'query');
157
- // 如果定义了 query 字段则优先取 query 参数
158
- if (resultHasQuery) {
159
- parsedQuery = qsParse(result.query ?? '') || {};
160
- } else if (resultHasPath) {
161
- // 没有定义 query 字段则尝试取 path 中的参数
162
- parsedQuery = qsParse(last(customSplit)) || {};
163
- } else {
164
- // 只有在没有自定义 path 和 query 的情况下,才使用页面堆栈中的默认参数并删除 utm 参数
165
- const stackPage = this.growingIO.minipInstance.getCurrentPage();
166
- // 这里不要直接用对象赋值,下面的 unset 会修改原对象,导致触发一次分享以后当前页面事件中的 utm 参数就会被删除掉
167
- parsedQuery = {
168
- ...(stackPage.options || this.queryOptions[stackPage.route] || {})
169
- };
170
-
171
- // 删除 utm_ 参数
172
- compact(keys(parsedQuery)).forEach((key) => {
173
- if (key.toLowerCase().startsWith('utm_')) {
174
- unset(parsedQuery, key);
175
- }
176
- });
177
- }
178
-
179
- return [path, qsStringify(parsedQuery)];
180
- }
181
-
182
- /**
183
- * 更新分享结果,补全参数
184
- * @param result PageShareResult 分享参数对象
185
- * @param fillParams 是否补全参数,默认 true
186
- * @returns 更新后的 result
187
- */
188
- updateShareResult = (result: PageShareResult, fillParams = true) => {
189
- const [path, queryString] = this.buildShareQuery(result);
190
- if (fillParams || has(result, 'path')) {
191
- result.path = normalPath(path + (queryString ? `?${queryString}` : ''));
192
- }
193
- if (fillParams || has(result, 'query')) {
194
- result.query = queryString;
195
- }
196
- return result;
197
- };
198
-
199
- /**
200
- * 给事件合并页面属性
201
- * @param trackingId 追踪 id
202
- * @param event 事件对象
203
- * @returns 合并后的属性对象
204
- */
205
- eventSetPageProps = (trackingId: string, event: EVENT) => {
206
- const pageProps = niceTry(
207
- () => this.pageProps[trackingId][this.getPagePath()]
208
- );
209
- if (!isEmpty(pageProps)) {
210
- return limitObject(
211
- getDynamicAttributes({
212
- ...event.attributes,
213
- ...pageProps
214
- })
215
- );
216
- }
217
- return event.attributes;
218
- };
219
- }
220
-
221
- export default MinipPage;