gio-miniprogram-sdk 4.0.1 → 4.2.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 (76) 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 +18 -10
  20. package/src/{pluginsOut/frameworks → adapters}/gioTaobaoAdapter.ts +49 -43
  21. package/src/{pluginsOut/frameworks → adapters}/gioTaroAdapter.ts +6 -4
  22. package/src/{pluginsOut/frameworks → adapters}/gioUniAppAdapter.ts +37 -6
  23. package/src/constants/config.ts +48 -18
  24. package/src/constants/emitMsg.ts +23 -0
  25. package/src/constants/regex.ts +0 -1
  26. package/src/core/dataStore/eventContextBuilder.ts +54 -25
  27. package/src/core/dataStore/eventHooks/appEffects.ts +50 -31
  28. package/src/core/dataStore/eventHooks/index.ts +13 -6
  29. package/src/core/dataStore/eventHooks/minipPage.ts +41 -13
  30. package/src/core/dataStore/eventHooks/pageEffects.ts +54 -31
  31. package/src/core/dataStore/index.ts +287 -205
  32. package/src/core/growingIO.ts +370 -180
  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 -82
  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 -5
  43. package/src/{pluginsOut/ordinary → plugins}/gioABTest.ts +88 -44
  44. package/src/{pluginsOut/ordinary → plugins}/gioCompress.ts +2 -0
  45. package/src/{pluginsOut/ordinary → plugins}/gioEventAutoTracking.ts +45 -21
  46. package/src/{pluginsOut/ordinary → plugins}/gioImpressionTracking.ts +67 -23
  47. package/src/plugins/gioMultipleInstances.ts +187 -0
  48. package/src/types/base.ts +10 -3
  49. package/src/types/dataStore.ts +35 -16
  50. package/src/types/eventHooks.ts +8 -6
  51. package/src/types/growingIO.ts +74 -39
  52. package/src/types/platforms.ts +0 -4
  53. package/src/types/plugins.ts +1 -1
  54. package/src/types/uploader.ts +16 -10
  55. package/src/types/userStore.ts +21 -17
  56. package/src/utils/glodash.ts +10 -3
  57. package/src/utils/tools.ts +31 -6
  58. package/src/utils/verifiers.ts +25 -50
  59. package/typings/global.d.ts +5 -0
  60. package/.babelrc +0 -4
  61. package/bin/build/full.sh +0 -32
  62. package/bin/build/jd.sh +0 -14
  63. package/bin/build/ks.sh +0 -14
  64. package/bin/build/my.sh +0 -14
  65. package/bin/build/plugins.sh +0 -5
  66. package/bin/build/qq.sh +0 -14
  67. package/bin/build/quickapp.sh +0 -14
  68. package/bin/build/swan.sh +0 -14
  69. package/bin/build/taro.sh +0 -18
  70. package/bin/build/tb.sh +0 -18
  71. package/bin/build/tt.sh +0 -14
  72. package/bin/build/uniapp.sh +0 -18
  73. package/bin/build/wx.sh +0 -14
  74. package/src/core/plugins.ts +0 -152
  75. package/src/pluginsOut/ordinary/gioCustomTracking.ts +0 -71
  76. package/bin/{build/all.sh → build.sh} +3 -3
@@ -2,14 +2,19 @@
2
2
  * 名称:淘宝小程序单发插件
3
3
  * 用途:用于提供淘宝小程序云函数和云应用的请求方法。
4
4
  */
5
+ import { EVENT, EXTEND_EVENT } from '@@/types/base';
5
6
  import { GrowingIOType } from '@@/types/growingIO';
7
+ import { head, unset } from '@@/utils/glodash';
8
+ import EMIT_MSG from '@@/constants/emitMsg';
6
9
 
7
10
  let ut;
8
11
  class GioTaobaoAdapter {
12
+ public pluginVersion: string;
9
13
  constructor(public growingIO: GrowingIOType) {
14
+ this.pluginVersion = '__PLUGIN_VERSION__';
10
15
  const { utils, emitter } = this.growingIO;
11
16
  ut = utils;
12
- emitter.on('OPTION_INITIALIZED', (growingIO: GrowingIOType) => {
17
+ emitter.on(EMIT_MSG.OPTION_INITIALIZED, (growingIO: GrowingIOType) => {
13
18
  const { inPlugin, dataStore } = growingIO;
14
19
  if (inPlugin) {
15
20
  ut.getGlobal().Component = function (...args: any[]) {
@@ -17,7 +22,7 @@ class GioTaobaoAdapter {
17
22
  };
18
23
  }
19
24
  });
20
- emitter.on('minipLifecycle', ({ event }) => {
25
+ emitter.on(EMIT_MSG.MINIP_LIFECYCLE, ({ event }) => {
21
26
  const { dataStore, vdsConfig } = this.growingIO;
22
27
  const widgetName = `小部件${vdsConfig.appId}`;
23
28
  switch (event) {
@@ -29,10 +34,12 @@ class GioTaobaoAdapter {
29
34
  .map((k) => `${k}=${relationAppInfo[k]}`)
30
35
  .join('&');
31
36
  dataStore.scene = sceneInfo.sceneId ?? '';
32
- dataStore.sendVisit({
33
- path: widgetName,
34
- query,
35
- title: widgetName
37
+ dataStore.trackersExecute((trackingId: string) => {
38
+ dataStore.sendVisit(trackingId, {
39
+ path: widgetName,
40
+ query,
41
+ title: widgetName
42
+ });
36
43
  });
37
44
  };
38
45
  this.getRelationAppInfo((relationAppInfo: any) =>
@@ -41,9 +48,14 @@ class GioTaobaoAdapter {
41
48
  }
42
49
  break;
43
50
  case 'Page didMount':
44
- dataStore.sendPage({
45
- path: widgetName,
46
- title: widgetName
51
+ dataStore.trackersExecute((trackingId: string) => {
52
+ const { trackPage } = dataStore.getTrackerVds(trackingId);
53
+ if (trackPage) {
54
+ dataStore.sendPage(trackingId, {
55
+ path: widgetName,
56
+ title: widgetName
57
+ });
58
+ }
47
59
  });
48
60
  break;
49
61
  default:
@@ -69,30 +81,31 @@ class GioTaobaoAdapter {
69
81
  };
70
82
 
71
83
  // 单条发送无延迟
72
- singleInvoke = () => {
84
+ singleInvoke = (trackingId: string) => {
73
85
  const { vdsConfig, emitter, uploader } = this.growingIO;
86
+ const hoardingQueue = uploader.getHoardingQueue(trackingId);
87
+ const requestQueue = uploader.getRequestQueue(trackingId);
74
88
  // 过滤掉重试超过3次的请求(直接丢弃)
75
- uploader.hoardingQueue = [...uploader.hoardingQueue].filter(
76
- (o) => (uploader.retryIds[o.requestId] || 0) < uploader.retryLimit
77
- );
78
- uploader.requestQueue = [...uploader.requestQueue].filter(
89
+ uploader.requestQueue[trackingId] = [...requestQueue].filter(
79
90
  (o) => (uploader.retryIds[o.requestId] || 0) < uploader.retryLimit
80
91
  );
81
- if (ut.isEmpty([...uploader.hoardingQueue, ...uploader.requestQueue])) {
92
+ if (ut.isEmpty([...hoardingQueue, ...requestQueue])) {
82
93
  return;
83
94
  }
84
- let requestData =
85
- uploader.hoardingQueue.shift() ?? uploader.requestQueue.shift();
95
+ const eventData: EXTEND_EVENT =
96
+ hoardingQueue.shift() ?? requestQueue.shift();
97
+ let requestData = { ...eventData };
98
+ unset(requestData, ['requestId', 'trackingId']);
86
99
  // 开启debug模式时,打印事件日志
87
100
  if (vdsConfig.debug) {
88
101
  console.log('[GrowingIO Debug]:', JSON.stringify(requestData, null, 2));
89
102
  }
90
- emitter.emit('onSendBefore', { requestData });
91
- this.cloudFuncInvoke(requestData);
103
+ emitter.emit(EMIT_MSG.ON_SEND_BEFORE, { requestData });
104
+ this.cloudFuncInvoke(eventData, requestData);
92
105
  };
93
106
 
94
107
  // 云函数调用
95
- cloudFuncInvoke = (requestData) => {
108
+ cloudFuncInvoke = (eventData: EXTEND_EVENT, requestData: EVENT) => {
96
109
  const { vdsConfig, emitter, uploader } = this.growingIO;
97
110
  const { serverUrl, projectId, tbConfig } = vdsConfig;
98
111
  const app = ut.niceTry(() => getApp() ?? $global);
@@ -114,18 +127,18 @@ class GioTaobaoAdapter {
114
127
  uploader.requestingNum -= 1;
115
128
  // 这里的返回值只是调用了api是否成功,真正是否上报成功不知道,要去淘宝控制台看
116
129
  if (res.success) {
117
- emitter.emit('onSendAfter', { result: res });
130
+ emitter.emit(EMIT_MSG.ON_SEND_AFTER, { result: res, requestData });
118
131
  } else {
119
- uploader.requestFailFn(requestData);
132
+ uploader.requestFailFn(eventData);
120
133
  ut.consoleText(`请求失败! ${JSON.stringify(res)}`, 'error');
121
134
  }
122
- uploader.initiateRequest();
135
+ uploader.initiateRequest(eventData.trackingId);
123
136
  })
124
137
  .catch((e) => {
125
138
  uploader.requestingNum -= 1;
126
- uploader.requestFailFn(requestData);
139
+ uploader.requestFailFn(eventData);
127
140
  ut.consoleText(`请求失败! ${JSON.stringify(e)}`, 'error');
128
- uploader.initiateRequest();
141
+ uploader.initiateRequest(eventData.trackingId);
129
142
  });
130
143
  } else {
131
144
  ut.consoleText(
@@ -136,12 +149,13 @@ class GioTaobaoAdapter {
136
149
  };
137
150
 
138
151
  // 云应用调用
139
- tbCloudAppInvoke(requestData) {
152
+ tbCloudAppInvoke(eventsQueue: EXTEND_EVENT[], requestData: EVENT) {
140
153
  const { vdsConfig, emitter, uploader } = this.growingIO;
141
154
  const { serverUrl, projectId, tbConfig } = vdsConfig;
142
155
  const app = ut.niceTry(() => getApp() ?? $global);
143
156
  const cloud = tbConfig.cloud ?? app.cloud;
144
157
  if (cloud?.application?.httpRequest) {
158
+ const trackingId = head(eventsQueue).trackingId;
145
159
  uploader.requestingNum += 1;
146
160
  cloud.application
147
161
  .httpRequest({
@@ -157,30 +171,22 @@ class GioTaobaoAdapter {
157
171
  uploader.requestingNum -= 1;
158
172
  // 这里的返回值只是调用了api是否成功,真正是否上报成功不知道,要看云应用的服务端是不是真的转发成功
159
173
  if (res.success) {
160
- emitter.emit('onSendAfter', { result: res });
174
+ emitter.emit(EMIT_MSG.ON_SEND_AFTER, { result: res, requestData });
161
175
  } else {
162
- if (ut.isArray(requestData)) {
163
- requestData.forEach((o: any) => {
164
- uploader.requestFailFn(o);
165
- });
166
- } else {
167
- uploader.requestFailFn(requestData);
168
- }
176
+ eventsQueue.forEach((o: any) => {
177
+ uploader.requestFailFn(o);
178
+ });
169
179
  ut.consoleText(`请求失败! ${JSON.stringify(res)}`, 'error');
170
180
  }
171
- uploader.initiateRequest();
181
+ uploader.initiateRequest(trackingId);
172
182
  })
173
183
  .catch((e) => {
174
184
  uploader.requestingNum -= 1;
175
- if (ut.isArray(requestData)) {
176
- requestData.forEach((o: any) => {
177
- uploader.requestFailFn(o);
178
- });
179
- } else {
180
- uploader.requestFailFn(requestData);
181
- }
185
+ eventsQueue.forEach((o: any) => {
186
+ uploader.requestFailFn(o);
187
+ });
182
188
  ut.consoleText(`请求失败! ${JSON.stringify(e)}`, 'error');
183
- uploader.initiateRequest();
189
+ uploader.initiateRequest(trackingId);
184
190
  });
185
191
  } else {
186
192
  ut.consoleText(
@@ -12,6 +12,7 @@ import {
12
12
  TARO_EVENT_VUE_FUNC3_REG
13
13
  } from '@@/constants/regex';
14
14
  import { GrowingIOType } from '@@/types/growingIO';
15
+ import EMIT_MSG from '@@/constants/emitMsg';
15
16
 
16
17
  let ut;
17
18
  const HANDLER = {
@@ -25,16 +26,18 @@ const CUSTOM_HOOK_EVENTS = [
25
26
  'onTabItemTap'
26
27
  ];
27
28
  class GioTaroAdapter {
29
+ public pluginVersion: string;
28
30
  public taroVersion: string;
29
31
  private taro: any;
30
32
  private taroVue: any;
31
33
  private taroVueVersion: number;
32
34
  private exposedNames: any;
33
35
  constructor(public growingIO: GrowingIOType) {
36
+ this.pluginVersion = '__PLUGIN_VERSION__';
34
37
  const { utils, emitter, minipInstance } = this.growingIO;
35
38
  ut = utils;
36
39
  this.exposedNames = {};
37
- emitter.on('minipLifecycle', ({ event }) => {
40
+ emitter.on(EMIT_MSG.MINIP_LIFECYCLE, ({ event }) => {
38
41
  if (['Page onShow', 'Page onReady'].includes(event)) {
39
42
  if (this.taro && this.taroVersion === '3') {
40
43
  // 提前存储页面节点信息用来获取dataset
@@ -66,9 +69,8 @@ class GioTaroAdapter {
66
69
  this.taroVersion = '3';
67
70
  this.taroVue = taroVue;
68
71
  if (this.taroVue) {
69
- const mainVersion = Number.parseInt(
70
- ut.head(ut.split(this.taroVue.version, '.')),
71
- 10
72
+ const mainVersion = Number(
73
+ ut.head(ut.split(this.taroVue.version, '.'))
72
74
  );
73
75
  this.taroVueVersion = mainVersion;
74
76
  ut.consoleText(`Taro version 3, Vue version ${mainVersion}`, 'info');
@@ -2,15 +2,18 @@
2
2
  * 名称:uniapp代理插件
3
3
  * 用途:用于重写uniapp框架下的创建App/Page/Component方法实现代理Hook;并获取uniapp框架下的自定义方法真实方法名获取。
4
4
  */
5
+ import { UNIAPP_FUNC_REG } from '@@/constants/regex';
5
6
  import { GrowingIOType } from '@@/types/growingIO';
6
7
 
7
8
  const ONCE = '~';
8
9
  const CUSTOM = '^';
9
10
  let ut;
10
11
  class GioUniAppAdapter {
12
+ public pluginVersion: string;
11
13
  private uniVue: any;
12
14
  private exposedNames: any;
13
15
  constructor(public growingIO: GrowingIOType) {
16
+ this.pluginVersion = '__PLUGIN_VERSION__';
14
17
  ut = this.growingIO.utils;
15
18
  this.uniVue = this.growingIO?.vdsConfig?.uniVue;
16
19
  this.exposedNames = {};
@@ -23,11 +26,7 @@ class GioUniAppAdapter {
23
26
  this.uniVue = this.growingIO?.vdsConfig?.uniVue;
24
27
  }
25
28
  console.log('Vue version: ', this.uniVue.version);
26
- if (this.growingIO?.vdsConfig?.subpackage) return false;
27
- const mainVersion = Number.parseInt(
28
- ut.head(ut.split(this.uniVue.version, '.')),
29
- 10
30
- );
29
+ const mainVersion = Number(ut.head(ut.split(this.uniVue.version, '.')));
31
30
  switch (mainVersion) {
32
31
  case 2:
33
32
  this.uniVue2Proxy(this.uniVue);
@@ -112,6 +111,38 @@ class GioUniAppAdapter {
112
111
  dataStore: { eventHooks },
113
112
  minipInstance
114
113
  } = this.growingIO;
114
+ // 重写对象遍历逻辑,优先hook methods最后才是原生
115
+ eventHooks.objectTraverse = (target: any, fn: any) => {
116
+ if (ut.has(target, 'methods')) {
117
+ Object.getOwnPropertyNames(target.methods).forEach((k: string) => {
118
+ fn(k, target.methods);
119
+ });
120
+ }
121
+ if (ut.has(target, 'lifetimes')) {
122
+ Object.getOwnPropertyNames(target.lifetimes).forEach((k: string) => {
123
+ fn(k, target.lifetimes);
124
+ });
125
+ }
126
+ if (ut.has(target, 'pageLifetimes')) {
127
+ Object.getOwnPropertyNames(target.pageLifetimes).forEach(
128
+ (k: string) => {
129
+ fn(k, target.pageLifetimes);
130
+ }
131
+ );
132
+ }
133
+ Object.getOwnPropertyNames(target).forEach((k: string) => {
134
+ if (
135
+ !ut.niceTry(
136
+ () =>
137
+ !target?.methods[k] &&
138
+ !target?.lifetimes[k] &&
139
+ !target?.pageLifetimes[k]
140
+ )
141
+ ) {
142
+ fn(k, target);
143
+ }
144
+ });
145
+ };
115
146
  eventHooks.nativeGrowing();
116
147
  // 需要单独处理getPageTitle,防止在vue中onShow里拿的是错误的
117
148
  const originGetter = minipInstance.getPageTitle;
@@ -180,7 +211,7 @@ class GioUniAppAdapter {
180
211
  const methodKeys = ut
181
212
  .keys(this.$scope)
182
213
  .filter(
183
- (k) => /^e[0-9]+$/.test(k) && ut.isFunction(this.$scope[k])
214
+ (k) => UNIAPP_FUNC_REG.test(k) && ut.isFunction(this.$scope[k])
184
215
  );
185
216
  methodKeys.forEach((k, i) => {
186
217
  const originFunc = this.$scope[k].value;
@@ -15,7 +15,7 @@ export const DEFAULT_SETTINGS = {
15
15
  extraParams: { type: 'array', default: [] },
16
16
  // 是否开启分享跟踪
17
17
  followShare: { type: 'boolean', default: true },
18
- // 是否开启强制登录
18
+ // 是否开启强制登录;多实例时仅主实例生效(其他实例无条件服从主实例)
19
19
  forceLogin: { type: 'boolean', default: false },
20
20
  // 或略上报字段
21
21
  ignoreFields: { type: 'array', default: [] },
@@ -47,6 +47,8 @@ export const DEFAULT_SETTINGS = {
47
47
  path: undefined
48
48
  }
49
49
  },
50
+ // 是否自动采集页面访问事件
51
+ trackPage: { type: 'boolean', default: true },
50
52
  // uniapp vue实例
51
53
  uniVue: { type: ['object', 'function'], default: false },
52
54
  // 上报间隔(1秒)
@@ -55,35 +57,49 @@ export const DEFAULT_SETTINGS = {
55
57
  version: { type: 'string', default: '1.0.0' }
56
58
  };
57
59
 
58
- // 通过gdp(xxxx)允许调用的通用方法
59
- export const HANDLERS = [
60
- 'clearGeneralProps', // 移除埋点通用属性
60
+ // 不管实例直接调用的方法
61
+ export const DIRECT_HANDLERS = [
62
+ 'getPlugins', // 获取所有已加载插件
63
+ 'getDeviceId', // 获取设备id
64
+ 'updateImpression' // 手动更新曝光
65
+ ];
66
+
67
+ // 多实例的方法
68
+ export const INSTANCE_HANDLERS = [
69
+ 'clearGeneralProps', // 清除埋点通用属性
70
+ 'clearPageAttributes', // 清除页面属性
61
71
  'clearTrackTimer', // 清除所有事件计时器
62
- 'clearUserId', // 清除登录用户ID
63
- 'getDeviceId', // 获取设备ID(匿名用户ID)
72
+ 'clearUserId', // 清除用户id
73
+ 'getABTest', // 获取AB实验数据
64
74
  'getGioInfo', // 内嵌页打通信息
65
- 'getOption', // 获取当前配置项状态
66
- 'identify', // 强制登录
67
- 'init', // 初始化SDK
68
- 'registerPlugins', // 手动注册插件
75
+ 'getOption', // 获取当前SDK配置项状态
76
+ 'identify', // 强制登录设置设备id
69
77
  'removeTimer', // 移除事件计时器
78
+ 'sendPage', // 手动发page事件
79
+ 'sendVisit', // 手动发visit事件
70
80
  'setGeneralProps', // 设置埋点通用属性
71
- 'setLocation', // 手动设置位置信息(2022/4/29新增api,原getLocation废弃)
72
- 'setOption', // 动态修改配置项
81
+ 'setLocation', // 手动设置位置信息
82
+ 'setOption', // 设置配置项
73
83
  'setPageAttributes', // 设置页面属性
74
84
  'setUserAttributes', // 设置用户属性
75
- 'setUserId', // 设置登录用户ID
76
- 'track', // 埋点
85
+ 'setUserId', // 设置用户id
86
+ 'track', // 上报埋点
77
87
  'trackTimerEnd', // 停止事件计时器并上报事件
78
88
  'trackTimerPause', // 暂停事件计时器
79
89
  'trackTimerResume', // 恢复事件计时器
80
- 'trackTimerStart', // 初始化事件计时器
81
- 'updateImpression', // 手动更新曝光监听
82
- 'getABTest' // 获取ABTest数据
90
+ 'trackTimerStart' // 初始化事件计时器
91
+ ];
92
+
93
+ // 直接调用的通用方法
94
+ export const HANDLERS = [
95
+ 'init', // 初始化SDK
96
+ 'registerPlugins', // 手动注册插件
97
+ ...DIRECT_HANDLERS,
98
+ ...INSTANCE_HANDLERS
83
99
  ];
84
100
 
85
101
  // 允许通过setOption修改的配置项
86
- export const ALLOWED_MODIFY_OPTIONS = {
102
+ export const ALLOW_SET_OPTIONS = {
87
103
  autotrack: '无埋点',
88
104
  dataCollect: '数据采集',
89
105
  debug: '调试模式',
@@ -134,3 +150,17 @@ export const PLATFORMTYPES = [
134
150
  'xhs',
135
151
  'quickapp'
136
152
  ];
153
+
154
+ // 各个需要拼接的在存储中的key后缀
155
+ export const STORAGE_KEYS: any = {
156
+ gsid: '_growing_gsid_',
157
+ originalSource: '_gdp_original_source_',
158
+ userId: '_growing_userId_',
159
+ userKey: '_growing_userKey_',
160
+ gioId: '_growing_gioId_',
161
+ plugin_gsid: '_growing_plugin_gsid_',
162
+ plugin_originalSource: '_gdp_plugin_original_source_',
163
+ plugin_userId: '_growing_plugin_userId_',
164
+ plugin_userKey: '_growing_plugin_userKey_',
165
+ plugin_gioId: '_growing_plugin_gioId_'
166
+ };
@@ -0,0 +1,23 @@
1
+ export default {
2
+ GIOID_UPDATE: 'GIOID_UPDATE', // gioId变更事件
3
+ MINIP_LIFECYCLE: 'MINIP_LIFECYCLE', // 小程序生命周期
4
+ ON_COMMIT_REQUEST: 'ON_COMMIT_REQUEST', // 小程序事件提交队列
5
+ ON_COMPOSE_AFTER: 'ON_COMPOSE_AFTER', // 插件生命周期:构建事件后
6
+ ON_COMPOSE_BEFORE: 'ON_COMPOSE_BEFORE', // 插件生命周期:构建事件前
7
+ ON_ERROR: 'ON_ERROR', // 插件生命周期:插件执行错误
8
+ ON_INSTALL: 'ON_INSTALL', // 插件生命周期:插件被正确加载
9
+ ON_SDK_INITIALIZE_BEFORE: 'ON_SDK_INITIALIZE_BEFORE', // SDK开始初始化之前
10
+ ON_SEND_AFTER: 'ON_SEND_AFTER', // 插件生命周期:发送事件后
11
+ ON_SEND_BEFORE: 'ON_SEND_BEFORE', // 插件生命周期:发送事件前
12
+ OPTION_CHANGE: 'OPTION_CHANGE', // 设置变更事件
13
+ OPTION_INITIALIZED: 'OPTION_INITIALIZED', // 配置项初始化完成
14
+ SCENE_UPDATE: 'SCENE_UPDATE', // scene变更事件
15
+ SDK_INITIALIZED: 'SDK_INITIALIZED', // SDK初始化完成发送事件之前
16
+ SESSIONID_UPDATE: 'SESSIONID_UPDATE', // sessionId变更事件
17
+ SET_USERID: 'SET_USERID', // setUserId事件
18
+ SET_USERKEY: 'SET_USERKEY', // setUserKey事件
19
+ UID_UPDATE: 'UID_UPDATE', // uId(设备Id)变更事件
20
+ UN_EXECUTE_CALL: 'UN_EXECUTE_CALL', // SDK加载完成但没有初始化时的调用事件
21
+ USERID_UPDATE: 'USERID_UPDATE', // userId变更事件
22
+ USERKEY_UPDATE: 'USERKEY_UPDATE' // userKey变更事件
23
+ };
@@ -8,7 +8,6 @@ export const ABTEST_DATA_REG = /^\d+_gdp_abtd$/;
8
8
  export const ID_REG = /^\d{1,10}$/;
9
9
  export const IMP_DATA_REG = /^gioTrack(.+)/;
10
10
  export const IMP_EVENTNAME_REG = /^[a-zA-Z_][0-9a-zA-Z_]{0,100}$/;
11
- export const QUICKAPP_PARAM_REG = /^text.*/;
12
11
  export const SWAN_XID_REG = /^_[0-9a-z]+/;
13
12
  export const TARO_XID_REG = /^_n_[0-9]+$/;
14
13
  export const TARO_EVENT_PROPS_REG = /^__reactProps\$[a-z0-9]+$/i;
@@ -1,69 +1,98 @@
1
1
  import { GrowingIOType } from '@@/types/growingIO';
2
- import { getOS, getScreenHeight, getScreenWidth } from '@@/utils/tools';
2
+ import {
3
+ getDynamicAttributes,
4
+ getOS,
5
+ getScreenHeight,
6
+ getScreenWidth,
7
+ limitObject
8
+ } from '@@/utils/tools';
3
9
  import { isEmpty, unset } from '@@/utils/glodash';
4
10
 
5
11
  class EventContextBuilder {
6
12
  constructor(public growingIO: GrowingIOType) {}
7
13
 
8
14
  // 通用字段组装
9
- main = (params?: any) => {
15
+ main = (trackingId: string, params?: any, executeAttributes = true) => {
16
+ // 预置事件或者默认就是发给主实例的事件不需要传trackingId
17
+ if (!trackingId) {
18
+ trackingId = this.growingIO.trackingId;
19
+ }
10
20
  const {
11
21
  sdkVersion,
12
- vdsConfig,
13
22
  platformConfig,
14
23
  minipInstance,
15
24
  userStore,
16
- dataStore: { scene, eventHooks, locationData, lastVisitEvent }
25
+ dataStore,
26
+ dataStore: {
27
+ scene,
28
+ eventHooks: { currentPage },
29
+ lastVisitEvent,
30
+ lastPageEvent,
31
+ generalProps
32
+ }
17
33
  } = this.growingIO;
34
+ const { version, dataSourceId, appId, idMapping, ignoreFields } =
35
+ dataStore.getTrackerVds(trackingId);
36
+ const locationData = dataStore.locationData[trackingId];
18
37
  const { systemInfo = {}, network = {} } = minipInstance;
19
- const { brand, model, platform, language, version } = systemInfo;
38
+ const { brand, model, platform, language } = systemInfo;
20
39
  // 事件主要内容组装
21
40
  const context: any = {
22
41
  appChannel: `scn:${scene || 'NA'}`,
23
- appVersion: vdsConfig.version,
24
- dataSourceId: vdsConfig.dataSourceId,
42
+ appVersion: version,
43
+ dataSourceId: dataSourceId,
25
44
  deviceBrand: brand,
26
- deviceId: userStore.uid,
45
+ deviceId: userStore.getUid(),
27
46
  deviceModel: model,
28
47
  deviceType: getOS(platform, platformConfig.name),
29
- domain: vdsConfig.appId,
48
+ domain: appId,
30
49
  language: language,
31
50
  latitude: locationData?.latitude,
32
51
  longitude: locationData?.longitude,
33
52
  networkState: network?.networkType || network?.type || network?.subtype,
34
53
  operatingSystem: getOS(platform, platformConfig.name),
35
- path: eventHooks.currentPage?.path
36
- ? eventHooks.currentPage?.path
37
- : lastVisitEvent.path, // 如果页面中的path取不到,说明页面没初始化,还在app的生命周期中(appOnLoad/appOnShow),需要拿visit的path补上
54
+ path: currentPage?.path ? currentPage?.path : lastVisitEvent.path, // 如果页面中的path取不到,说明页面没初始化,还在app的生命周期中(appOnLoad/appOnShow),需要拿visit的path补上
38
55
  platform: platformConfig.platform,
39
- platformVersion: platformConfig.name + (version ? ` ${version}` : ''),
40
- query: eventHooks.currentPage?.path
41
- ? eventHooks.currentPage?.query
42
- : lastVisitEvent.query, // 如果页面中的path取不到,说明页面没初始化,还在app的生命周期中(appOnLoad/appOnShow),需要拿visit的query补上
56
+ platformVersion:
57
+ platformConfig.name +
58
+ (systemInfo.version ? ` ${systemInfo.version}` : ''),
59
+ query: currentPage?.path ? currentPage?.query : lastVisitEvent.query, // 如果页面中的path取不到,说明页面没初始化,还在app的生命周期中(appOnLoad/appOnShow),需要拿visit的query补上
43
60
  screenHeight: getScreenHeight(systemInfo),
44
61
  screenWidth: getScreenWidth(systemInfo),
45
62
  sdkVersion,
46
- sessionId: userStore.sessionId,
63
+ sessionId: userStore.getSessionId(trackingId),
47
64
  title:
48
- eventHooks.currentPage?.title ||
65
+ lastPageEvent[trackingId]?.title || // 除visit,page事件外,其他事件都用lastpage中的title以保持一致
66
+ currentPage?.title ||
49
67
  minipInstance.getPageTitle(minipInstance.getCurrentPage()),
50
68
  timestamp: Date.now(),
51
69
  timezoneOffset: new Date().getTimezoneOffset(),
52
- userId: userStore.userId
70
+ userId: userStore.getUserId(trackingId)
53
71
  };
54
- if (vdsConfig.idMapping) {
55
- context.userKey = userStore.userKey;
72
+ if (idMapping) {
73
+ context.userKey = userStore.getUserKey(trackingId);
74
+ }
75
+ // 全局属性
76
+ if (executeAttributes && !isEmpty(generalProps[trackingId])) {
77
+ context.attributes = { ...generalProps[trackingId] };
78
+ }
79
+ // 属性格式化
80
+ if (!isEmpty(context.attributes)) {
81
+ context.attributes = limitObject(
82
+ getDynamicAttributes({ ...context.attributes })
83
+ );
56
84
  }
57
- if (!isEmpty(vdsConfig.ignoreFields)) {
58
- vdsConfig.ignoreFields.forEach((o) => {
85
+ if (!isEmpty(ignoreFields)) {
86
+ ignoreFields.forEach((o) => {
59
87
  unset(context, o);
60
88
  });
61
89
  }
62
90
  // 此时可能存在数据为空的字段,在数据组装完成后由转换方法移除空值字段
63
91
  if (!isEmpty(params)) {
64
- return { ...context, ...params };
92
+ return { ...context, ...params, trackingId };
93
+ } else {
94
+ return { ...context, trackingId };
65
95
  }
66
- return context;
67
96
  };
68
97
  }
69
98