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,386 +0,0 @@
1
- /**
2
- * 名称:半自动浏览埋点插件
3
- * 用途:用于提供半自动浏览埋点事件的自动注册、参数检验构建和创建事件方法。
4
- */
5
- import { IMP_DATA_REG } from '@@/constants/regex';
6
- import { GrowingIOType } from '@@/types/growingIO';
7
- import {
8
- compact,
9
- isArray,
10
- isEmpty,
11
- isEqualArray,
12
- isObject,
13
- isString,
14
- toString
15
- } from '@@/utils/glodash';
16
- import {
17
- eventNameValidate,
18
- hashCode,
19
- limitObject,
20
- niceTry
21
- } from '@@/utils/tools';
22
- import EMIT_MSG from '@@/constants/emitMsg';
23
-
24
- let ut;
25
- class GioImpressionTracking {
26
- public pluginVersion: string;
27
- public observerIds: any;
28
- public rects: any;
29
- public pageImpObserver: any;
30
- public componentImpObserver: any;
31
- public pageQueryTarget: any;
32
- public componentQueryTarget: any;
33
- public sentImps: any;
34
- constructor(public growingIO: GrowingIOType) {
35
- this.pluginVersion = '__PLUGIN_VERSION__';
36
- ut = this.growingIO.utils;
37
- this.observerIds = {};
38
- this.rects = {};
39
- this.sentImps = {};
40
- const { emitter, minipInstance } = this.growingIO;
41
- this.growingIO.updateImpression = (collect: any) => {
42
- if (!collect) {
43
- collect = minipInstance.getCurrentPage();
44
- }
45
- minipInstance.initImpression(collect);
46
- };
47
- emitter.on(EMIT_MSG.MINIP_LIFECYCLE, ({ event, params }) => {
48
- const { minipInstance } = this.growingIO;
49
- if (event === 'Page onShowEnd') {
50
- const page = minipInstance.getCurrentPage();
51
- this.traverseListen(page);
52
- }
53
- if (['Page onHide', 'Page onUnload'].includes(event)) {
54
- // 页面隐藏时销毁曝光监听
55
- const route = params.page.route;
56
- const observers = Object.values(this.observerIds[route] ?? {});
57
- observers.forEach((o: any) => o.disconnect());
58
- }
59
- });
60
- }
61
-
62
- // 遍历页面中的自定义组件进行监听
63
- traverseListen = (target: any) => {
64
- const { minipInstance } = this.growingIO;
65
- if (ut.isFunction(target?.selectAllComponents)) {
66
- const selects = target?.selectAllComponents(
67
- '.growing_collect_imp_component'
68
- );
69
- if (!ut.isEmpty(selects)) {
70
- selects.forEach((s) => {
71
- this.traverseListen(s);
72
- });
73
- }
74
- }
75
- minipInstance.initImpression(target);
76
- };
77
-
78
- // 判断是否为相同的监听节点
79
- // 这个方法中的hashCode和isEqualArray要直接引入使用,否则可能会在jd/my/tb小程序中出现找不到的情况(运行时机问题)
80
- isEqualRects = (rs: any[], route: string, nodeId: string) => {
81
- const controlGroup = ((this.rects[route] ?? {})[nodeId] ?? []).map((o) =>
82
- hashCode(JSON.stringify(o))
83
- );
84
- const comparisonGroup = (rs ?? []).map((o) => hashCode(JSON.stringify(o)));
85
- return isEqualArray(controlGroup, comparisonGroup);
86
- };
87
-
88
- // 获取nodeId
89
- getNodeId = (collectTarget: any) => {
90
- const { gioPlatform } = this.growingIO;
91
- switch (gioPlatform) {
92
- case 'wx':
93
- case 'qq':
94
- case 'ks':
95
- case 'jd':
96
- return collectTarget.__wxExparserNodeId__; // wx page/component
97
- case 'my':
98
- case 'tb':
99
- return collectTarget.$page
100
- ? `${collectTarget.$page.$id}-${collectTarget.$id}` //my component
101
- : collectTarget.$id; // my page
102
- case 'swan':
103
- return collectTarget.route
104
- ? toString(hashCode(collectTarget.route)) // swan page
105
- : collectTarget.nodeId; // swan component
106
- case 'tt':
107
- return collectTarget.route
108
- ? toString(hashCode(collectTarget.route)) // swan page
109
- : collectTarget.__nodeId__; // swan component
110
- case 'xhs':
111
- return collectTarget.route
112
- ? collectTarget.pageId // xhs page
113
- : collectTarget.componentId; // xhs component
114
- default:
115
- break;
116
- }
117
- };
118
-
119
- // 获取到可监听节点后校验节点信息
120
- rectObserve = (rect, collectTarget, optionKey) => {
121
- const { gioPlatform } = this.growingIO;
122
- if (
123
- (gioPlatform === 'tt' && collectTarget.__nodeId__) ||
124
- (gioPlatform === 'ks' && collectTarget.is) ||
125
- (gioPlatform === 'xhs' && collectTarget.componentId)
126
- ) {
127
- // 字节/快手/小红书小程序页面监听时可以拿到自定义组件一次性完成监听,所以组件的节点实例不再处理
128
- return false;
129
- }
130
- const nodeId = this.getNodeId(collectTarget);
131
- const page = collectTarget.route
132
- ? collectTarget
133
- : this.growingIO.minipInstance.getCurrentPage();
134
- // 监听的目标页面和上一次的不一致,或者监听的组件或页面的监听已经被注销时,可以继续生成新的监听
135
- let observerNeedNew = false;
136
- if (page.route) {
137
- const isEqualRects = this.isEqualRects(rect, page.route, nodeId);
138
- const disconnect = ut.get(
139
- this.observerIds,
140
- `${page.route}.${nodeId}._disconnected`
141
- );
142
- observerNeedNew = !isEqualRects || disconnect !== false;
143
- }
144
- if (
145
- ut.isArray(rect) &&
146
- !ut.isEmpty(rect) &&
147
- !ut.isNil(rect[0]) &&
148
- observerNeedNew
149
- ) {
150
- if (ut.isEmpty(this.rects[page.route])) {
151
- this.rects[page.route] = {};
152
- }
153
- this.rects[page.route][nodeId] = rect;
154
- this.createObserver(collectTarget, optionKey);
155
- }
156
- };
157
-
158
- // swan、my中optionKey为'selectAll',其他为'observeAll',不含快应用
159
- main = (collectTarget: any, optionKey: 'observeAll' | 'selectAll') => {
160
- const { gioPlatform } = this.growingIO;
161
- // 页面中存在曝光标记的节点才监听
162
- // 有route字段的是页面,没有的是组件内部
163
- const query = collectTarget.route
164
- ? this.growingIO.minipInstance.minip?.createSelectorQuery()
165
- : this.growingIO.minipInstance.minip
166
- ?.createSelectorQuery()
167
- .in(collectTarget);
168
- if (['ks', 'qq'].includes(gioPlatform)) {
169
- query
170
- .selectAll('.growing_collect_imp')
171
- .boundingClientRect(([rect = []]) => {
172
- rect = ut.isArray(rect) ? rect : [rect];
173
- if (!isEmpty(rect)) {
174
- this.rectObserve(rect, collectTarget, optionKey);
175
- }
176
- });
177
- query.exec();
178
- } else {
179
- query.selectAll('.growing_collect_imp').boundingClientRect();
180
- query.exec(([rect]) => {
181
- if (!isEmpty(rect)) {
182
- this.rectObserve(rect, collectTarget, optionKey);
183
- }
184
- });
185
- }
186
- };
187
-
188
- // 创建监听
189
- createObserver = (
190
- collectTarget: any,
191
- optionKey: 'observeAll' | 'selectAll'
192
- ) => {
193
- const { gioPlatform, minipInstance, vdsConfig } = this.growingIO;
194
- // 初始化监听对象
195
- // 数值要大于0,只能无限趋近于0,等于0的时候会导致没出现就会发曝光
196
- const threshold = vdsConfig.impressionScale || 0.0000000000001;
197
- const observerOption = { thresholds: [threshold], [optionKey]: true };
198
- if (
199
- ['tb', 'my'].includes(gioPlatform) &&
200
- ut.compareVersion(my.SDKVersion, '2.7.0') >= -1
201
- ) {
202
- observerOption.dataset = true;
203
- }
204
- const nodeId = this.getNodeId(collectTarget);
205
- let page = collectTarget;
206
- // uniapp是组件和页面一体,所以要分两次尝试监听;原生是组件和页面互相独立,按类型监听
207
- // 自定义组件是不能直接在页面上被拿到,所以直接监听页面不会监听到自定义组件
208
- if (
209
- collectTarget.isComponent ||
210
- !collectTarget.route ||
211
- collectTarget.mpType === 'component'
212
- ) {
213
- page = minipInstance.getCurrentPage();
214
- }
215
- if (!this.observerIds[page.route]) {
216
- this.observerIds[page.route] = {};
217
- }
218
- // 存在组件id一致的监听时要先移除监听,防止重复监听重复触发
219
- const originObserver = this.observerIds[page.route][nodeId];
220
- if (originObserver && !originObserver._disconnected) {
221
- originObserver.disconnect();
222
- }
223
- // 创建一个新的监听
224
- let observer = ['swan', 'jd', 'xhs'].includes(gioPlatform)
225
- ? minipInstance.minip?.createIntersectionObserver(
226
- collectTarget,
227
- observerOption
228
- )
229
- : collectTarget.createIntersectionObserver(observerOption);
230
- observer = observer.relativeToViewport();
231
- // 不管原先是否创建过,直接覆盖(observerId会更新)
232
- this.observerIds[page.route][nodeId] = observer;
233
- // 添加监听回调
234
- this.targetObserve(observer, page);
235
- };
236
-
237
- // 给目标节点添加监听回调
238
- targetObserve = (observerTarget: any, collectPage: any) => {
239
- const { taro, impressionScale } = this.growingIO.vdsConfig;
240
- // 数值要大于0,只能无限趋近于0,等于0的时候会导致没出现就会发曝光
241
- const threshold = impressionScale || 0.0000000000001;
242
- // 监听节点变化
243
- observerTarget.observe('.growing_collect_imp', (result: any) => {
244
- if (!ut.isEmpty(result) && result.intersectionRatio >= threshold) {
245
- let dataset: any = result.dataset;
246
- if (taro && ut.isTaro3(taro)) {
247
- dataset = this.getTaro3Dataset(collectPage.route, result.id);
248
- }
249
- const dataProperties = this.getImpressionProperties(dataset);
250
- // 曝光类型判断,单次曝光的需要有id和gio-imp-type字段
251
- const sentId = result.id;
252
- if (sentId) {
253
- if (dataset.gioImpType === 'once' && ut.has(this.sentImps, sentId)) {
254
- return;
255
- } else {
256
- this.sentImps[sentId] = dataProperties;
257
- }
258
- }
259
- if (dataProperties.eventName) {
260
- let sendTargets = [];
261
- if (dataset.gioImpSendto) {
262
- // 先尝试处理成数组
263
- sendTargets = compact(
264
- isArray(dataset.gioImpSendto)
265
- ? dataset.gioImpSendto
266
- : niceTry(() => JSON.parse(dataset.gioImpSendto)) || []
267
- );
268
- // 在尝试处理字符串
269
- if (isEmpty(sendTargets)) {
270
- niceTry(() =>
271
- dataset.gioImpSendto.split(',').forEach((s: string) => {
272
- s = isString(s) && s.trim().replace('[', '').replace(']', '');
273
- if (s) {
274
- sendTargets.push(s);
275
- }
276
- })
277
- );
278
- }
279
- }
280
- this.buildImpEvent(dataProperties, sendTargets);
281
- } else {
282
- ut.consoleText(
283
- '曝光事件格式不正确,事件名只能包含数字、字母和下划线,且不以数字开头!',
284
- 'warn'
285
- );
286
- }
287
- }
288
- });
289
- };
290
-
291
- // 曝光参数获取
292
- getImpressionProperties = (dataSet: any) => {
293
- let data: any = {
294
- eventName: undefined,
295
- properties: {}
296
- };
297
- if (!dataSet?.gioImpTrack) {
298
- return data;
299
- } else {
300
- data.eventName = dataSet.gioImpTrack;
301
- }
302
- if (ut.has(dataSet, 'gioImpAttrs')) {
303
- // imp写法二
304
- data.properties = ut.niceTry(() =>
305
- ut.isObject(dataSet.gioImpAttrs)
306
- ? dataSet.gioImpAttrs
307
- : JSON.parse(dataSet.gioImpAttrs)
308
- );
309
- } else {
310
- // imp写法一
311
- for (const key in dataSet) {
312
- let normKey;
313
- const matchArr = key.match(IMP_DATA_REG);
314
- if (matchArr) {
315
- normKey = ut.lowerFirst(matchArr[1]);
316
- if (normKey !== 'track') {
317
- data.properties[normKey] = dataSet[key];
318
- }
319
- }
320
- }
321
- }
322
- // 对参数对象进行限制
323
- data.properties = ut.limitObject(data.properties);
324
- // 校验eventName
325
- const validate = eventNameValidate(data.eventName, () => {
326
- return data;
327
- });
328
- if (!validate) {
329
- data.eventName = null;
330
- data = {};
331
- }
332
- return data;
333
- };
334
-
335
- // 获取Taro3节点上的dataset
336
- getTaro3Dataset = (route: string, targetId: string) => {
337
- const page = this.growingIO?.taro3VMs[route];
338
- if (!page) {
339
- return false;
340
- }
341
- let impDataset = {};
342
- const searchTraverse = (n: any) => {
343
- n.some((o: any) => {
344
- if (
345
- (o.uid === targetId || o.sid === targetId) &&
346
- o.props?.class.indexOf('growing_collect_imp') > -1 &&
347
- !ut.isEmpty(o.dataset)
348
- ) {
349
- impDataset = o.dataset;
350
- return true;
351
- } else if (Array.isArray(o.childNodes)) {
352
- return searchTraverse(o.childNodes);
353
- } else {
354
- return false;
355
- }
356
- });
357
- };
358
- searchTraverse(page.childNodes);
359
- return impDataset;
360
- };
361
-
362
- // 创建半自动曝光事件
363
- buildImpEvent = (dataProperties: any, sendTargets: string[]) => {
364
- const { eventName, properties } = dataProperties;
365
- const {
366
- trackingId,
367
- dataStore: { eventContextBuilder, eventConverter },
368
- plugins
369
- } = this.growingIO;
370
- const event = {
371
- eventType: 'CUSTOM',
372
- eventName,
373
- ...eventContextBuilder(trackingId)
374
- };
375
- event.attributes = limitObject({
376
- ...(event.attributes ?? {}),
377
- ...(isObject(properties) && !isEmpty(properties) ? properties : {})
378
- });
379
- if (plugins.gioMultipleInstances && !isEmpty(sendTargets)) {
380
- event['&&sendTo'] = sendTargets;
381
- }
382
- eventConverter(event);
383
- };
384
- }
385
-
386
- export default { name: 'gioImpressionTracking', method: GioImpressionTracking };
@@ -1,189 +0,0 @@
1
- /**
2
- * 名称:多实例插件
3
- * 用途:用于提供多实例以向指定项目和数据源发数的插件。
4
- */
5
-
6
- import { EXTEND_EVENT } from '@@/types/base';
7
- import { GrowingIOType } from '@@/types/growingIO';
8
- import { isString, unset } from '@@/utils/glodash';
9
- import { getGlobal, niceTry } from '@@/utils/tools';
10
- import { OriginOptions } from '@@/types/dataStore';
11
- import EMIT_MSG from '@@/constants/emitMsg';
12
-
13
- export class GioMultipleInstances {
14
- public pluginVersion: string;
15
- public subTrackingIds: string[];
16
- constructor(public growingIO: GrowingIOType) {
17
- this.pluginVersion = '__PLUGIN_VERSION__';
18
- this.subTrackingIds = [];
19
- this.growingIO.emitter.on(EMIT_MSG.ON_SDK_INITIALIZE_BEFORE, () => {
20
- // 自动重写部分代码以实现多实例功能
21
- if (!this.growingIO.trackingId) {
22
- this.rewriteDataStore();
23
- }
24
- // 挂载子实例集合对象
25
- if (!this.growingIO.subInstance) {
26
- this.growingIO.subInstance = {};
27
- }
28
- });
29
- this.growingIO.emitter.on(EMIT_MSG.OPTION_INITIALIZED, ({ trackingId }) => {
30
- // 是子实例
31
- if (
32
- this.growingIO.trackingId &&
33
- this.growingIO.trackingId !== trackingId
34
- ) {
35
- // 把所有已初始化过的子实例trackingId保存下来方便预定义事件和无埋点事件广播时调用
36
- this.subTrackingIds.push(trackingId);
37
- }
38
- });
39
- }
40
-
41
- // 判断当前trackingId实例类型,为主实例还是子实例
42
- getTrackerType = (trackingId: string, growingIO: GrowingIOType): number => {
43
- return !growingIO.trackingId || growingIO.trackingId === trackingId ? 1 : 0;
44
- };
45
-
46
- // 重写dataStore中的部分内容
47
- rewriteDataStore = () => {
48
- // 获取采集实例
49
- this.growingIO.dataStore.getTrackerVds = (trackingId: string) => {
50
- if (this.growingIO.trackingId === trackingId) {
51
- const vds = { ...this.growingIO.vdsConfig };
52
- unset(vds, 'subInstance');
53
- if (!this.growingIO.plugins.gioTaobaoAdapter) {
54
- unset(vds, 'tbConfig');
55
- }
56
- return vds;
57
- } else {
58
- return this.growingIO.subInstance[trackingId]
59
- ? { ...this.growingIO.subInstance[trackingId] }
60
- : undefined;
61
- }
62
- };
63
-
64
- // 初始化实例配置的方法
65
- this.growingIO.dataStore.initTrackerOptions = (options: OriginOptions) => {
66
- const trackerOptions = this.growingIO.dataStore.initOptions(options);
67
- const trackerType = this.getTrackerType(
68
- options.trackingId,
69
- this.growingIO
70
- );
71
- trackerOptions.trackingId = options.trackingId;
72
- if (trackerType) {
73
- // 当前初始化主实例时给主实例赋值trackingId
74
- this.growingIO.trackingId = options.trackingId;
75
- // 主实例的配置直接挂在growingIO上
76
- this.growingIO.vdsConfig = trackerOptions;
77
- // 把主实例挂到小程序全局对象上
78
- getGlobal().vdsConfig = trackerOptions;
79
- } else {
80
- // 子实例无效的配置项删除
81
- unset(trackerOptions, [
82
- 'autotrack',
83
- 'debug',
84
- 'forceLogin',
85
- 'impressionScale',
86
- 'keepAlive',
87
- 'originalSource',
88
- 'performance',
89
- 'pluginMode',
90
- 'sessionExpires',
91
- 'taro',
92
- 'taroVue',
93
- 'tbConfig',
94
- 'uniVue'
95
- ]);
96
- // 子实例的配置挂在subInstance下,以trackingId作为key值存储
97
- this.growingIO.subInstance[options.trackingId] = trackerOptions;
98
- // 把子实例挂到小程序全局对象上
99
- if (!getGlobal().vdsConfig?.subInstance) {
100
- getGlobal().vdsConfig.subInstance = {};
101
- }
102
- getGlobal().vdsConfig.subInstance[options.trackingId] = trackerOptions;
103
- }
104
- return trackerOptions;
105
- };
106
-
107
- // 初始化实例的hooker
108
- this.growingIO.dataStore.initTrackerHooker = (options: OriginOptions) => {
109
- const trackerType = this.getTrackerType(
110
- options.trackingId,
111
- this.growingIO
112
- );
113
- if (trackerType) {
114
- const { platformConfig, inPlugin, minipInstance, plugins, dataStore } =
115
- this.growingIO;
116
- const { uniVue, taro } = options;
117
- // 如果是使用全量版本时,当前小程序不是框架的,要允许原生的hook
118
- if (!(uniVue || taro)) {
119
- platformConfig.canHook = true;
120
- }
121
- // 初始化核心钩子(重写全局)
122
- if (!inPlugin || ['tbp', 'jdp'].includes(minipInstance.platform)) {
123
- dataStore.eventHooks.initEventHooks();
124
- }
125
- /** 第三方框架重写 */
126
- // uniapp
127
- if (uniVue) {
128
- plugins?.gioUniAppAdapter?.main();
129
- }
130
- // taro
131
- if (taro) {
132
- plugins?.gioTaroAdapter?.main();
133
- }
134
- }
135
- };
136
-
137
- // 多实例时处理复制发送
138
- const originFunction = this.growingIO.dataStore.eventConverter;
139
- const self = this;
140
- this.growingIO.dataStore.eventConverter = function (...args) {
141
- const event: EXTEND_EVENT = args[0];
142
- // 埋点事件使用合并后的发送目标发送
143
- if (event.eventType === 'CUSTOM') {
144
- const sendTo = [event.trackingId];
145
- niceTry(() => {
146
- event['&&sendTo'].forEach((s) => {
147
- // 合法且不重复的实例
148
- if (
149
- isString(s) &&
150
- self.growingIO.dataStore.getTrackerVds(s) &&
151
- !sendTo.includes(s)
152
- ) {
153
- sendTo.push(s);
154
- }
155
- });
156
- });
157
- unset(event, '&&sendTo');
158
- sendTo.forEach((trackingId: string) => {
159
- const { eventContextBuilder } = self.growingIO.dataStore;
160
- const newEvent = {
161
- ...event,
162
- ...eventContextBuilder(
163
- trackingId,
164
- undefined,
165
- event.trackingId !== trackingId
166
- )
167
- };
168
- newEvent.attributes = {
169
- ...newEvent.attributes,
170
- ...event.attributes
171
- };
172
- originFunction.call(this, newEvent);
173
- });
174
- } else {
175
- // 其他事件直接发给指定调用实例
176
- originFunction.call(this, event);
177
- }
178
- };
179
- };
180
-
181
- onSendBefore = ({ eventsQueue, requestData, trackingId }: any) => {
182
- // 不是主实例的事件由多实例插件来提交请求
183
- if (trackingId !== this.growingIO.trackingId) {
184
- this.growingIO.uploader.sendEvent(eventsQueue, requestData);
185
- }
186
- };
187
- }
188
-
189
- export default { name: 'gioMultipleInstances', method: GioMultipleInstances };