gio-miniprogram-sdk 4.0.0 → 4.1.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 (78) 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 -0
  11. package/dist/qq.js +1 -1
  12. package/dist/quickapp.js +1 -1
  13. package/dist/taobao.js +1 -1
  14. package/dist/taro.js +1 -1
  15. package/dist/uniapp.js +1 -1
  16. package/dist/wechat.js +1 -1
  17. package/package.json +39 -42
  18. package/rollup.config.js +11 -6
  19. package/rollup.plugins.config.js +13 -10
  20. package/src/{pluginsOut/frameworks → adapters}/gioTaobaoAdapter.ts +47 -44
  21. package/src/adapters/gioTaroAdapter.ts +459 -0
  22. package/src/{pluginsOut/frameworks → adapters}/gioUniAppAdapter.ts +35 -6
  23. package/src/constants/config.ts +49 -20
  24. package/src/constants/emitMsg.ts +23 -0
  25. package/src/constants/regex.ts +14 -0
  26. package/src/core/dataStore/eventContextBuilder.ts +28 -17
  27. package/src/core/dataStore/eventHooks/appEffects.ts +50 -31
  28. package/src/core/dataStore/eventHooks/index.ts +7 -5
  29. package/src/core/dataStore/eventHooks/minipPage.ts +2 -11
  30. package/src/core/dataStore/eventHooks/pageEffects.ts +61 -27
  31. package/src/core/dataStore/index.ts +266 -181
  32. package/src/core/growingIO.ts +312 -179
  33. package/src/core/pluginsInstance/base.ts +157 -0
  34. package/src/core/pluginsInstance/taro.ts +15 -0
  35. package/src/core/pluginsInstance/tb.ts +15 -0
  36. package/src/core/pluginsInstance/uniapp.ts +15 -0
  37. package/src/core/uploader.ts +131 -87
  38. package/src/core/userStore.ts +160 -121
  39. package/src/index.ts +37 -28
  40. package/src/minipInstance/base.ts +2 -1
  41. package/src/minipInstance/quickapp.ts +5 -19
  42. package/src/platformConfig/base.ts +0 -4
  43. package/src/{pluginsOut/ordinary → plugins}/gioABTest.ts +74 -37
  44. package/src/{pluginsOut/ordinary → plugins}/gioEventAutoTracking.ts +25 -18
  45. package/src/{pluginsOut/ordinary → plugins}/gioImpressionTracking.ts +109 -41
  46. package/src/plugins/gioMultipleInstances.ts +176 -0
  47. package/src/types/base.ts +12 -3
  48. package/src/types/dataStore.ts +31 -16
  49. package/src/types/eventHooks.ts +8 -9
  50. package/src/types/growingIO.ts +30 -14
  51. package/src/types/platforms.ts +0 -4
  52. package/src/types/plugins.ts +1 -1
  53. package/src/types/uploader.ts +16 -12
  54. package/src/types/userStore.ts +21 -17
  55. package/src/utils/glodash.ts +10 -3
  56. package/src/utils/tools.ts +27 -6
  57. package/src/utils/verifiers.ts +25 -50
  58. package/typings/global.d.ts +5 -0
  59. package/.babelrc +0 -4
  60. package/bin/build/full.sh +0 -32
  61. package/bin/build/jd.sh +0 -14
  62. package/bin/build/ks.sh +0 -14
  63. package/bin/build/my.sh +0 -14
  64. package/bin/build/plugins.sh +0 -5
  65. package/bin/build/qq.sh +0 -14
  66. package/bin/build/quickapp.sh +0 -14
  67. package/bin/build/swan.sh +0 -14
  68. package/bin/build/taro.sh +0 -18
  69. package/bin/build/tb.sh +0 -18
  70. package/bin/build/tt.sh +0 -14
  71. package/bin/build/uniapp.sh +0 -18
  72. package/bin/build/wx.sh +0 -14
  73. package/src/core/plugins.ts +0 -152
  74. package/src/pluginsOut/README.md +0 -50
  75. package/src/pluginsOut/frameworks/gioTaroAdapter.ts +0 -358
  76. package/src/pluginsOut/ordinary/gioCustomTracking.ts +0 -71
  77. package/bin/{build/all.sh → build.sh} +3 -3
  78. /package/src/{pluginsOut/ordinary → plugins}/gioCompress.ts +0 -0
@@ -1,358 +0,0 @@
1
- /**
2
- * 名称:Taro代理插件
3
- * 用途:用于重写Taro框架下的创建App/Page/Component方法实现代理Hook;并获取Taro框架下的自定义方法真实方法名获取。
4
- * 注意:测试taro3react时,微信要用react18测试,支付宝要用react17测试。
5
- */
6
- import { GrowingIOType } from '@@/types/growingIO';
7
-
8
- let ut;
9
- class GioTaroAdapter {
10
- private taro: any;
11
- private vue: any;
12
- private exposedNames: any;
13
- private taro3Listeners: any;
14
- constructor(public growingIO: GrowingIOType) {
15
- const { utils, vdsConfig, emitter, minipInstance, gioPlatform } =
16
- this.growingIO;
17
- ut = utils;
18
- this.taro = vdsConfig?.taro;
19
- this.vue = vdsConfig?.taroVue;
20
- this.exposedNames = {};
21
- this.taro3Listeners = {};
22
- emitter.on('minipLifecycle', ({ event }) => {
23
- if (event === 'Page onReadyEnd') {
24
- if (this.taro && ut.isTaro3(this.taro)) {
25
- // 提前存储页面节点信息用来获取dataset
26
- this.growingIO.plugins?.gioTaroAdapter?.saveFullPage(document.body);
27
- // taro3开发时要等onready才能取到节点,所以补充做一次曝光监听
28
- if (!['swan', 'tt'].includes(gioPlatform)) {
29
- setTimeout(
30
- () =>
31
- minipInstance.initImpression(minipInstance.getCurrentPage()),
32
- 0
33
- );
34
- }
35
- }
36
- }
37
- });
38
- }
39
-
40
- // 主入口,版本判断
41
- main = () => {
42
- // 插件挂载时可能还没取到
43
- if (!this.taro) {
44
- this.taro = this.growingIO?.vdsConfig?.taro;
45
- }
46
- if (!this.vue) {
47
- this.vue = this.growingIO?.vdsConfig?.taroVue;
48
- }
49
- const { Current, createComponent } = this.taro;
50
- if (!Current && createComponent) {
51
- this.proxyTaro2(this.taro);
52
- } else if (Current && !createComponent) {
53
- this.proxyTaro3(this.taro, this.vue);
54
- } else {
55
- ut.consoleText('未获取到Taro实例或不支持的Taro版本,请检查!', 'error');
56
- }
57
- };
58
-
59
- // 代理taro2
60
- proxyTaro2 = (taro: any) => {
61
- const self = this;
62
- const originApp = taro.createApp;
63
- const originComponent = taro.createComponent;
64
- if (originApp) {
65
- taro.createApp = function (AppClass) {
66
- const AppRef = originApp(AppClass);
67
- self.growingIO.dataStore.eventHooks.appOverriding(AppRef);
68
- return AppRef;
69
- };
70
- }
71
- if (originComponent) {
72
- taro.createComponent = function (ComponentClass, isPage) {
73
- const ComponentRef = originComponent(ComponentClass, isPage);
74
- const targetType = isPage ? 'page' : 'component';
75
- // wx/qq的生命周期函数和自定义函数都在ComponentRef.methods中
76
- // my/tt/jd的生命周期函数和自定义函数都在ComponentRef中
77
- // swan的生命周期函数在ComponentRef.methods中,自定义函数在ComponentRef中
78
- switch (self.growingIO.gioPlatform) {
79
- case 'wx':
80
- case 'qq':
81
- self.growingIO.dataStore.eventHooks[`${targetType}Overriding`](
82
- ComponentRef.methods
83
- );
84
- break;
85
- case 'my':
86
- case 'tt':
87
- case 'jd':
88
- self.growingIO.dataStore.eventHooks[`${targetType}Overriding`](
89
- ComponentRef
90
- );
91
- break;
92
- case 'swan':
93
- self.growingIO.dataStore.eventHooks[`${targetType}Overriding`](
94
- ComponentRef
95
- );
96
- self.growingIO.dataStore.eventHooks[`${targetType}Overriding`](
97
- ComponentRef.methods
98
- );
99
- break;
100
- default:
101
- break;
102
- }
103
- return ComponentRef;
104
- };
105
- }
106
- };
107
-
108
- // 代理taro3
109
- proxyTaro3 = (taro: any, vue?: any) => {
110
- const { eventHooks } = this.growingIO.dataStore;
111
- if (vue) {
112
- console.log('Vue version: ', vue.version);
113
- const mainVersion = Number.parseInt(
114
- ut.head(ut.split(vue.version, '.')),
115
- 10
116
- );
117
- eventHooks.nativeGrowing();
118
- if (mainVersion === 2) {
119
- this.taro3Vue2Proxy(vue);
120
- } else if (mainVersion === 3) {
121
- this.taro3Vue3Proxy(vue);
122
- } else {
123
- ut.consoleText('不支持的Vue版本,请使用 Vue2 或 Vue3!', 'error');
124
- }
125
- } else {
126
- eventHooks.nativeGrowing(['App']);
127
- this.taro3ReactProxy(taro);
128
- }
129
- };
130
-
131
- // taro3使用react的代理
132
- taro3ReactProxy = (taroRuntime: any) => {
133
- // @ts-ignore
134
- const taroNode = document.__proto__.__proto__;
135
- const self = this;
136
- const { eventHooks } = this.growingIO.dataStore;
137
- // 重写页面生命周期
138
- const originCall = taroRuntime?.hooks?.call;
139
- if (originCall) {
140
- self.defineProperty(taroRuntime.hooks, 'call', function (...args) {
141
- const argsArray = Array.from(args);
142
- let result = originCall.apply(this, argsArray);
143
- if (argsArray[0] === 'modifyPageObject') {
144
- const pageObject = argsArray[1];
145
- ut.keys(pageObject).forEach((k) => {
146
- if (eventHooks.pageHandlers.includes(k)) {
147
- pageObject[k] = eventHooks.lifeFcEffects(
148
- k,
149
- ut.isFunction(pageObject[k]) ? pageObject[k] : () => {},
150
- 'Page'
151
- );
152
- }
153
- });
154
- }
155
- return result;
156
- });
157
- } else {
158
- ut.consoleText('Taro3实例获取失败,请检查初始化逻辑!', 'warn');
159
- eventHooks.nativeGrowing(['Page']);
160
- }
161
-
162
- // 重写页面自定义方法
163
- const proxyAddEventListener = (addEventListener) => {
164
- return function (...args) {
165
- const originHandler = args[1];
166
- const handler = self.growingIO.dataStore.eventHooks.customFcEffects(
167
- originHandler.name,
168
- originHandler
169
- );
170
- self.taro3Listeners[originHandler.name] = handler;
171
- return addEventListener.apply(this, [args[0], handler, args[2]]);
172
- };
173
- };
174
- const proxyRemoveEventListener = (removeEventListener) => {
175
- return function (...args) {
176
- return removeEventListener.apply(this, [
177
- args[0],
178
- self.taro3Listeners[args[1].name]
179
- ]);
180
- };
181
- };
182
- self.defineProperty(
183
- taroNode,
184
- 'addEventListener',
185
- proxyAddEventListener(taroNode.addEventListener)
186
- );
187
- self.defineProperty(
188
- taroNode,
189
- 'removeEventListener',
190
- proxyRemoveEventListener(taroNode.removeEventListener)
191
- );
192
- };
193
-
194
- // taro3使用vue2的代理
195
- taro3Vue2Proxy = (vue: any) => {
196
- const self = this;
197
- vue.mixin({
198
- beforeMount: function () {
199
- // 对传统methods写法的普通事件hook
200
- const methods = this.$options.methods;
201
- ut.keys(methods).forEach((k: string) => {
202
- self.defineProperty(this, k, self.proxyMethods(k, this[k]));
203
- });
204
- // 对setup中定义的事件hook
205
- const setupState = this._setupState;
206
- if (!ut.isEmpty(setupState)) {
207
- ut.keys(setupState).forEach((k: string) => {
208
- self.defineProperty(
209
- setupState,
210
- k,
211
- self.proxyMethods(k, setupState[k])
212
- );
213
- });
214
- }
215
- }
216
- });
217
- };
218
-
219
- // taro3使用vue3的代理
220
- taro3Vue3Proxy = (vue: any) => {
221
- const { eventHooks } = this.growingIO.dataStore;
222
- const self = this;
223
- vue.mixin({
224
- beforeMount: function () {
225
- // hook逻辑
226
- const transferMethods = (o) =>
227
- ut.keys(o).forEach((k: string) => {
228
- self.defineProperty(this, k, self.proxyMethods(k, this[k]));
229
- });
230
- // 对传统methods写法的普通事件hook
231
- const methods = this.$options.methods;
232
- if (methods) {
233
- transferMethods(methods);
234
- }
235
- // 对setup中定义的事件hook
236
- const setupState = this?._?.setupState;
237
- if (setupState) {
238
- transferMethods(setupState);
239
- }
240
- // 使用emit监听组件点击事件的处理
241
- const tEmit = this._.emit;
242
- this._.emit = function (...args) {
243
- (() =>
244
- eventHooks.customFcEffects(args[0], () => {}).apply(this, args))();
245
- return tEmit.apply(this, args);
246
- };
247
- },
248
- mounted() {
249
- // this._.vnode.el要在mounted中才能获取到
250
- // 对<script setup>写法的事件hook,编译时会匿名,需要配合expose
251
- const exposed = this?._?.exposed ?? {};
252
- self.exposedNames = {};
253
- if (!ut.isEmpty(exposed)) {
254
- const nExposed = {};
255
- // 保存exposed供<script setup>写法的事件查找真实的事件名
256
- ut.keys(exposed).forEach((k) => {
257
- if (ut.isFunction(exposed[k])) {
258
- self.exposedNames[exposed[k].name] = k;
259
- nExposed[exposed[k].name] = exposed[k];
260
- }
261
- });
262
- // 只能取到编译后的名称,需要从exposedNames中映射
263
- self.traverseHook(this._.vnode.el, 0);
264
- }
265
- }
266
- });
267
- };
268
-
269
- // 重写方法
270
- proxyMethods = (name: string, method: any) => {
271
- const { eventHooks } = this.growingIO.dataStore;
272
- if (ut.isFunction(method) && !eventHooks.pageHandlers.includes(name)) {
273
- return function (...args) {
274
- return eventHooks.customFcEffects(name, method).apply(this, args);
275
- };
276
- } else {
277
- return method;
278
- }
279
- };
280
-
281
- // 重新定义属性
282
- defineProperty = (target, key, value) => {
283
- Object.defineProperty(target, key, {
284
- writable: true,
285
- enumerable: true,
286
- configurable: true,
287
- value
288
- });
289
- };
290
-
291
- // 遍历hook节点方法
292
- traverseHook = (node: any, i: number) => {
293
- this.veiAutoEventHook(node, i);
294
- if (node.childNodes && ut.isArray(node.childNodes)) {
295
- node.childNodes.forEach((n, i) => {
296
- this.traverseHook(n, i);
297
- });
298
- }
299
- };
300
-
301
- // vue3中查找并重写节点vei对象中的自定义事件
302
- veiAutoEventHook = (node: any, i: number) => {
303
- const { eventHooks } = this.growingIO.dataStore;
304
- if (node._vei) {
305
- ut.keys(node._vei).forEach((k) => {
306
- if (eventHooks.actionEventTypes.includes(k)) {
307
- const func = node._vei[k].value;
308
- let funcName;
309
- if (
310
- (ut.startsWith(`${func}`, 'function ($event)') ||
311
- ut.startsWith(`${func}`, 'function($event)')) &&
312
- /return \$setup.[\w]+\(/.test(`${func}`)
313
- ) {
314
- // 进入该判断说明客户对方法进行了传参,导致框架编译为匿名函数,此时需要我们处理一下拿到真实的方法名
315
- const s = `${func}`.match(/return \$setup.[\w]+\(/)[0];
316
- if (s) {
317
- funcName = s.replace('return $setup.', '').replace('(', '');
318
- }
319
- }
320
- // 没有获取到方法的情况下,通过exposedNames中存的值尝试匹配,不一定准,因为对象是无序的
321
- if (!funcName) {
322
- funcName = `${ut.keys(this.exposedNames)[i]}#${
323
- Object.values(this.exposedNames)[i]
324
- }`;
325
- }
326
- this.defineProperty(
327
- node._vei[k],
328
- 'value',
329
- this.proxyMethods(
330
- this.exposedNames[func.name] || func.name || funcName,
331
- func
332
- )
333
- );
334
- }
335
- });
336
- }
337
- };
338
-
339
- // taro3中存储当前页面完整的数据以获取曝光事件中的dataset
340
- // https://taro-docs.jd.com/taro/docs/react-overall#dataset
341
- saveFullPage = (body: any) => {
342
- const pageTraverse = (tEle: any) => {
343
- tEle.forEach((cEle: any) => {
344
- if (cEle?.ctx?.route) {
345
- if (!this.growingIO.taro3VMs) {
346
- this.growingIO.taro3VMs = {};
347
- }
348
- this.growingIO.taro3VMs[cEle.ctx.route] = cEle;
349
- } else if (ut.isArray(cEle.childNodes)) {
350
- pageTraverse(cEle.childNodes);
351
- }
352
- });
353
- };
354
- pageTraverse(body.childNodes);
355
- };
356
- }
357
-
358
- export default { name: 'gioTaroAdapter', method: GioTaroAdapter };
@@ -1,71 +0,0 @@
1
- /**
2
- * 名称:自定义埋点插件
3
- * 用途:用于提供自定义埋点的事件构建方法。
4
- */
5
- import { GioCustomTrackingType } from '@@/types/gioCustomTracking';
6
- import { GrowingIOType } from '@@/types/growingIO';
7
-
8
- let ut;
9
- class GioCustomTracking implements GioCustomTrackingType {
10
- constructor(public growingIO: GrowingIOType) {
11
- ut = this.growingIO.utils;
12
- }
13
-
14
- // 获取动态属性的值
15
- getDynamicAttributes = (properties: any) => {
16
- if (!ut.isNil(properties)) {
17
- ut.keys(properties).forEach((k: string) => {
18
- if (ut.isFunction(properties[k])) {
19
- properties[k] = properties[k]();
20
- } else if (!ut.isArray(properties[k])) {
21
- properties[k] = ut.toString(properties[k]);
22
- }
23
- });
24
- }
25
- return properties;
26
- };
27
-
28
- // 构建自定义埋点事件
29
- buildCustomEvent = (name: string, properties: { [key: string]: string }) => {
30
- ut.eventNameValidate(name, () => {
31
- const {
32
- dataStore: { eventContextBuilder, eventInterceptor }
33
- } = this.growingIO;
34
- const event = {
35
- eventType: 'CUSTOM',
36
- eventName: name,
37
- attributes: ut.limitObject(
38
- this.getDynamicAttributes(
39
- ut.isObject(properties) && !ut.isEmpty(properties)
40
- ? properties
41
- : undefined
42
- )
43
- ),
44
- ...eventContextBuilder()
45
- };
46
- eventInterceptor(event);
47
- });
48
- };
49
-
50
- // 构建用户变量事件
51
- buildUserAttributesEvent = (attributes: { [key: string]: string }) => {
52
- const {
53
- dataStore: { eventContextBuilder, eventInterceptor, lastVisitEvent }
54
- } = this.growingIO;
55
- const event = {
56
- eventType: 'LOGIN_USER_ATTRIBUTES',
57
- attributes: ut.limitObject(attributes),
58
- ...eventContextBuilder()
59
- };
60
- // 该方法很可能被放在在app的onShow中的wx.login异步回调中,
61
- // 此时页面的path和query可能还没取到,就拿visit事件的path和query补上
62
- if (!event.path) {
63
- event.path = lastVisitEvent.path;
64
- event.query = lastVisitEvent.query;
65
- event.title = lastVisitEvent.title;
66
- }
67
- eventInterceptor(event);
68
- };
69
- }
70
-
71
- export default { name: 'gioCustomTracking', method: GioCustomTracking };
@@ -1,9 +1,6 @@
1
1
  # 清空产物
2
2
  rm -r dist/*
3
3
 
4
- # 插件
5
- npm run build:plugins
6
-
7
4
  # 微信小程序
8
5
  npm run build:wx
9
6
 
@@ -36,3 +33,6 @@ npm run build:uniapp
36
33
 
37
34
  # taro2/3 react/vue2/vue3
38
35
  npm run build:taro
36
+
37
+ # 插件
38
+ npm run build:plugins