gio-miniprogram-sdk 4.4.1 → 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.
- package/dist/alipay.js +1 -1
- package/dist/baidu.js +1 -1
- package/dist/bytedance.js +1 -1
- package/dist/jingdong.js +1 -1
- package/dist/kuaishou.js +1 -1
- package/dist/plugins/gioABTest.js +1 -1
- package/dist/plugins/gioCompress.js +1 -1
- package/dist/plugins/gioEventAutoTracking.js +1 -1
- package/dist/plugins/gioImpressionTracking.js +1 -1
- package/dist/plugins/gioMultipleInstances.js +1 -1
- package/dist/plugins/gioPerformance.js +1 -1
- package/dist/qq.js +1 -1
- package/dist/quickapp.js +1 -1
- package/dist/taobao.js +1 -1
- package/dist/taro.js +1 -1
- package/dist/uniapp.js +1 -1
- package/dist/wechat.js +1 -1
- package/dist/xiaohongshu.js +1 -1
- package/package.json +7 -1
- package/.eslintignore +0 -11
- package/.eslintrc.js +0 -120
- package/.github/workflows/greetings.yml +0 -13
- package/.github/workflows/publish.yml +0 -45
- package/.prettierignore +0 -19
- package/.prettierrc.js +0 -9
- package/babel.config.js +0 -6
- package/rollup.config.js +0 -100
- package/rollup.plugins.config.js +0 -84
- package/src/adapters/gioTaobaoAdapter.ts +0 -202
- package/src/adapters/gioTaroAdapter.ts +0 -467
- package/src/adapters/gioUniAppAdapter.ts +0 -431
- package/src/constants/config.ts +0 -170
- package/src/constants/emitMsg.ts +0 -24
- package/src/constants/regex.ts +0 -18
- package/src/core/dataStore/eventContextBuilder.ts +0 -95
- package/src/core/dataStore/eventHooks/appEffects.ts +0 -218
- package/src/core/dataStore/eventHooks/index.ts +0 -468
- package/src/core/dataStore/eventHooks/minipPage.ts +0 -234
- package/src/core/dataStore/eventHooks/pageEffects.ts +0 -189
- package/src/core/dataStore/index.ts +0 -661
- package/src/core/growingIO.ts +0 -637
- package/src/core/pluginsInstance/base.ts +0 -157
- package/src/core/pluginsInstance/taro.ts +0 -15
- package/src/core/pluginsInstance/tb.ts +0 -15
- package/src/core/pluginsInstance/uniapp.ts +0 -15
- package/src/core/uploader.ts +0 -272
- package/src/core/userStore.ts +0 -267
- package/src/index.ts +0 -77
- package/src/minipInstance/base.ts +0 -352
- package/src/minipInstance/framework.ts +0 -24
- package/src/minipInstance/jd.ts +0 -11
- package/src/minipInstance/ks.ts +0 -12
- package/src/minipInstance/my.ts +0 -131
- package/src/minipInstance/qq.ts +0 -12
- package/src/minipInstance/quickapp.ts +0 -233
- package/src/minipInstance/swan.ts +0 -20
- package/src/minipInstance/tb.ts +0 -58
- package/src/minipInstance/tt.ts +0 -12
- package/src/minipInstance/wx.ts +0 -27
- package/src/minipInstance/xhs.ts +0 -20
- package/src/platformConfig/base.ts +0 -82
- package/src/platformConfig/framework.ts +0 -31
- package/src/platformConfig/jd.ts +0 -11
- package/src/platformConfig/ks.ts +0 -11
- package/src/platformConfig/my.ts +0 -16
- package/src/platformConfig/qq.ts +0 -13
- package/src/platformConfig/quickapp.ts +0 -48
- package/src/platformConfig/swan.ts +0 -11
- package/src/platformConfig/tb.ts +0 -29
- package/src/platformConfig/tt.ts +0 -11
- package/src/platformConfig/wx.ts +0 -20
- package/src/platformConfig/xhs.ts +0 -27
- package/src/plugins/gioABTest.ts +0 -348
- package/src/plugins/gioCompress.ts +0 -253
- package/src/plugins/gioEventAutoTracking.ts +0 -288
- package/src/plugins/gioImpressionTracking.ts +0 -386
- package/src/plugins/gioMultipleInstances.ts +0 -189
- package/src/plugins/gioPerformance/exception.ts +0 -152
- package/src/plugins/gioPerformance/monitor.ts +0 -284
- package/src/plugins/gioPerformance/network.ts +0 -197
- package/src/plugins/gioPerformance.ts +0 -131
- package/src/types/base.ts +0 -167
- package/src/types/dataStore.ts +0 -62
- package/src/types/eventHooks.ts +0 -147
- package/src/types/gioCustomTracking.ts +0 -9
- package/src/types/gioEventTracking.ts +0 -10
- package/src/types/gioPerformance.ts +0 -8
- package/src/types/growingIO.ts +0 -137
- package/src/types/minipInstance.ts +0 -133
- package/src/types/platforms.ts +0 -64
- package/src/types/plugins.ts +0 -24
- package/src/types/uploader.ts +0 -30
- package/src/types/userStore.ts +0 -28
- package/src/utils/glodash.ts +0 -430
- package/src/utils/tools.ts +0 -472
- package/src/utils/verifiers.ts +0 -52
- package/tsconfig.json +0 -32
- package/typings/global.d.ts +0 -105
|
@@ -1,661 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ALLOW_SET_OPTIONS,
|
|
3
|
-
DEFAULT_SETTINGS,
|
|
4
|
-
EXTRA_INFO_PARAMS,
|
|
5
|
-
IGNORE_PARAMS,
|
|
6
|
-
STORAGE_KEYS
|
|
7
|
-
} from '@@/constants/config';
|
|
8
|
-
import {
|
|
9
|
-
forEach,
|
|
10
|
-
has,
|
|
11
|
-
head,
|
|
12
|
-
isArray,
|
|
13
|
-
isBoolean,
|
|
14
|
-
isEmpty,
|
|
15
|
-
isFunction,
|
|
16
|
-
isString,
|
|
17
|
-
keys,
|
|
18
|
-
typeOf,
|
|
19
|
-
unset
|
|
20
|
-
} from '@@/utils/glodash';
|
|
21
|
-
import {
|
|
22
|
-
batchSetStorageSync,
|
|
23
|
-
callError,
|
|
24
|
-
consoleText,
|
|
25
|
-
getGlobal,
|
|
26
|
-
guid,
|
|
27
|
-
limitObject,
|
|
28
|
-
niceTry
|
|
29
|
-
} from '@@/utils/tools';
|
|
30
|
-
import {
|
|
31
|
-
DataStoreType,
|
|
32
|
-
OriginOptions,
|
|
33
|
-
StorageKeyType
|
|
34
|
-
} from '@@/types/dataStore';
|
|
35
|
-
import { EXTEND_EVENT } from '@@/types/base';
|
|
36
|
-
import { EventHooksType } from '@@/types/eventHooks';
|
|
37
|
-
import { GrowingIOType } from '@@/types/growingIO';
|
|
38
|
-
import EMIT_MSG from '@@/constants/emitMsg';
|
|
39
|
-
import EventContextBuilder from './eventContextBuilder';
|
|
40
|
-
import EventHooks from './eventHooks';
|
|
41
|
-
|
|
42
|
-
class DataStore implements DataStoreType {
|
|
43
|
-
readonly ALLOW_SETTING_KEYS = keys(DEFAULT_SETTINGS);
|
|
44
|
-
public _gsid: any;
|
|
45
|
-
public _stid: any; // 用于判断每发送10个事件,往本地存储里同步一次gsid,减少存储的读写操作。
|
|
46
|
-
public eventHooks: EventHooksType;
|
|
47
|
-
// 来源场景值
|
|
48
|
-
public _scene: number | string;
|
|
49
|
-
// 上次来源场景值
|
|
50
|
-
public lastScene: number | string;
|
|
51
|
-
// 是否是分享弹窗导致的跳出页面
|
|
52
|
-
public shareOut: boolean;
|
|
53
|
-
// 上次小程序关闭时间
|
|
54
|
-
public lastCloseTime: number;
|
|
55
|
-
// 事件通用属性构建方法
|
|
56
|
-
public eventContextBuilder: (trackingId: string, params?: any) => any;
|
|
57
|
-
// 上一个访问事件,保存最近的一次visit事件,存储一些设备系统信息,避免多次重新获取
|
|
58
|
-
public lastVisitEvent: any;
|
|
59
|
-
// 上一个页面事件,便于下一个Page事件获取数据
|
|
60
|
-
public lastPageEvent: any;
|
|
61
|
-
// 由客户设置的位置信息
|
|
62
|
-
public locationData: any;
|
|
63
|
-
// 埋点事件通用属性
|
|
64
|
-
public generalProps: any;
|
|
65
|
-
// 事件时长计时器
|
|
66
|
-
public trackTimers: any;
|
|
67
|
-
// 被拦截的事件
|
|
68
|
-
public interceptEvents: any;
|
|
69
|
-
// 记录已初始化的trackingId,以帮助判断是否再更新sessionId时是否需要发送事件
|
|
70
|
-
public initializedTrackingIds: string[];
|
|
71
|
-
|
|
72
|
-
constructor(public growingIO: GrowingIOType) {
|
|
73
|
-
this.initializedTrackingIds = [];
|
|
74
|
-
this.eventContextBuilder = new EventContextBuilder(this.growingIO).main;
|
|
75
|
-
this.eventHooks = new EventHooks(this.growingIO);
|
|
76
|
-
this.shareOut = false;
|
|
77
|
-
this._gsid = {};
|
|
78
|
-
this._stid = {};
|
|
79
|
-
this.lastVisitEvent = {};
|
|
80
|
-
this.lastPageEvent = {};
|
|
81
|
-
this.locationData = {};
|
|
82
|
-
this.generalProps = {};
|
|
83
|
-
this.trackTimers = {};
|
|
84
|
-
this.interceptEvents = [];
|
|
85
|
-
const { emitter, minipInstance } = this.growingIO;
|
|
86
|
-
// sdk初始化完成记录trackingId
|
|
87
|
-
emitter.on(EMIT_MSG.SDK_INITIALIZED, ({ trackingId: initTrackingId }) => {
|
|
88
|
-
this.initializedTrackingIds.push(initTrackingId);
|
|
89
|
-
this.initStorageInfo(initTrackingId);
|
|
90
|
-
});
|
|
91
|
-
// visit、page事件监听
|
|
92
|
-
emitter.on(
|
|
93
|
-
EMIT_MSG.ON_COMMIT_REQUEST,
|
|
94
|
-
({ eventData: event, trackingId }) => {
|
|
95
|
-
if (event.eventType === 'VISIT') {
|
|
96
|
-
this.lastVisitEvent[trackingId] = event;
|
|
97
|
-
}
|
|
98
|
-
if (event.eventType === 'PAGE') {
|
|
99
|
-
this.lastPageEvent[trackingId] = event;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
);
|
|
103
|
-
// 小程序切后台把所有还在计时中的计时器记一次时长
|
|
104
|
-
if (isFunction(minipInstance.minip?.onAppHide)) {
|
|
105
|
-
minipInstance.minip.onAppHide(() => {
|
|
106
|
-
this.trackersExecute((trackingId: string) => {
|
|
107
|
-
const timers = this.trackTimers[trackingId];
|
|
108
|
-
forEach(timers, (timer: any) => {
|
|
109
|
-
if (timer.start) {
|
|
110
|
-
timer.leng = timer.leng + (+Date.now() - timer.start);
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
// 小程序切前台把所有还在计时中的计时器重置开始时间
|
|
117
|
-
if (isFunction(minipInstance.minip?.onAppShow)) {
|
|
118
|
-
minipInstance.minip.onAppShow(() => {
|
|
119
|
-
this.trackersExecute((trackingId: string) => {
|
|
120
|
-
const timers = this.trackTimers[trackingId];
|
|
121
|
-
forEach(timers, (timer: any) => {
|
|
122
|
-
if (timer.start) {
|
|
123
|
-
timer.start = +Date.now();
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
// 关闭数据采集时要移除所有的计时器
|
|
130
|
-
emitter.on(EMIT_MSG.OPTION_CHANGE, ({ optionName, optionValue }) => {
|
|
131
|
-
if (optionName === 'dataCollect' && optionValue === false) {
|
|
132
|
-
this.trackersExecute((trackingId: string) => {
|
|
133
|
-
this.growingIO.clearTrackTimer(trackingId);
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
// session更新要补发当前页面的visit和page
|
|
138
|
-
this.growingIO.emitter.on(
|
|
139
|
-
EMIT_MSG.SESSIONID_UPDATE,
|
|
140
|
-
({ oldSessionId, trackingId }) => {
|
|
141
|
-
if (oldSessionId) {
|
|
142
|
-
// 补发visit
|
|
143
|
-
const { getPagePath, getPageQuery: getPageQuery } =
|
|
144
|
-
this.eventHooks.currentPage;
|
|
145
|
-
const path = getPagePath();
|
|
146
|
-
const query = getPageQuery();
|
|
147
|
-
if (!path) {
|
|
148
|
-
// 个别(说的就是你:淘宝)小程序场景值不一样后台拉起时可能args中会没有path和query,直接取上一个最后发送的page中的值
|
|
149
|
-
const lastPage = this.lastPageEvent[trackingId];
|
|
150
|
-
this.sendVisit(trackingId, {
|
|
151
|
-
path: lastPage.path,
|
|
152
|
-
query: lastPage.query
|
|
153
|
-
});
|
|
154
|
-
} else {
|
|
155
|
-
this.sendVisit(trackingId, { path, query });
|
|
156
|
-
}
|
|
157
|
-
// 页面显示以后触发切session要补发page,离开页面到页面加载显示完成前的切session不补发,由onshow生命周期触发
|
|
158
|
-
if (!this.eventHooks.currentPage.lifeBeforeShow) {
|
|
159
|
-
this.sendPage(trackingId);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// 获取存储key
|
|
167
|
-
getStorageKey = (trackingId: string, name: StorageKeyType) => {
|
|
168
|
-
const { inPlugin } = this.growingIO;
|
|
169
|
-
return `${trackingId === this.growingIO.trackingId ? '' : trackingId}${
|
|
170
|
-
STORAGE_KEYS[inPlugin ? 'plugin_' + name : name]
|
|
171
|
-
}`;
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
// @ts-ignore
|
|
175
|
-
getGsid = (trackingId: string) => {
|
|
176
|
-
let gsid = Number(this._gsid[trackingId]);
|
|
177
|
-
if (Number.isNaN(gsid) || gsid >= 1e9 || gsid < 1) {
|
|
178
|
-
this.setGsid(trackingId, 1);
|
|
179
|
-
return 1;
|
|
180
|
-
} else {
|
|
181
|
-
return gsid;
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
// @ts-ignore
|
|
186
|
-
setGsid = (trackingId: string, value: any) => {
|
|
187
|
-
let gsid = Number(value);
|
|
188
|
-
if (Number.isNaN(gsid) || gsid >= 1e9 || gsid < 1) {
|
|
189
|
-
gsid = 1;
|
|
190
|
-
}
|
|
191
|
-
this._gsid[trackingId] = gsid;
|
|
192
|
-
// _gsid每自增10次(即每发送10个事件)时,往本地存储里同步一次gsid
|
|
193
|
-
if (this._gsid[trackingId] - this._stid[trackingId] >= 10) {
|
|
194
|
-
this.saveStorageInfo();
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
get scene() {
|
|
199
|
-
return this._scene;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
set scene(v: any) {
|
|
203
|
-
const prevScene = this._scene;
|
|
204
|
-
this._scene = v;
|
|
205
|
-
if (prevScene !== this._scene) {
|
|
206
|
-
this.growingIO.emitter.emit(EMIT_MSG.SCENE_UPDATE, {
|
|
207
|
-
newScene: v,
|
|
208
|
-
oldScene: prevScene
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// 初始化gsid
|
|
214
|
-
initStorageInfo = (trackingId: string) => {
|
|
215
|
-
const { minipInstance } = this.growingIO;
|
|
216
|
-
const oriGsid = minipInstance.getStorageSync(
|
|
217
|
-
this.getStorageKey(trackingId, 'gsid')
|
|
218
|
-
);
|
|
219
|
-
let gsid = Number(oriGsid);
|
|
220
|
-
if (Number.isNaN(gsid) || gsid >= 1e9 || gsid < 1) {
|
|
221
|
-
gsid = 1;
|
|
222
|
-
}
|
|
223
|
-
this._gsid[trackingId] = gsid;
|
|
224
|
-
this._stid[trackingId] = gsid;
|
|
225
|
-
// 存储中的gsid不合法时要往存储写一次
|
|
226
|
-
if (gsid !== oriGsid) {
|
|
227
|
-
minipInstance.setStorageSync(
|
|
228
|
-
this.getStorageKey(trackingId, 'gsid'),
|
|
229
|
-
gsid
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
// 在存储中同步gsid信息
|
|
235
|
-
saveStorageInfo = () => {
|
|
236
|
-
const { minipInstance } = this.growingIO;
|
|
237
|
-
this._stid = { ...this._gsid };
|
|
238
|
-
batchSetStorageSync(
|
|
239
|
-
minipInstance,
|
|
240
|
-
this.initializedTrackingIds.map((trackingId: string) => ({
|
|
241
|
-
key: this.getStorageKey(trackingId, 'gsid'),
|
|
242
|
-
value: this._gsid[trackingId]
|
|
243
|
-
}))
|
|
244
|
-
);
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
// 保存初始来源信息
|
|
248
|
-
setOriginalSource = (trackingId: string, origins: any) => {
|
|
249
|
-
const { minipInstance } = this.growingIO;
|
|
250
|
-
minipInstance.setStorageSync(
|
|
251
|
-
this.getStorageKey(trackingId, 'originalSource'),
|
|
252
|
-
JSON.stringify(origins)
|
|
253
|
-
);
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
// 获取初始来源信息
|
|
257
|
-
getOriginalSource = (trackingId: string) => {
|
|
258
|
-
const { minipInstance } = this.growingIO;
|
|
259
|
-
return (
|
|
260
|
-
niceTry(() =>
|
|
261
|
-
JSON.parse(
|
|
262
|
-
minipInstance.getStorageSync(
|
|
263
|
-
this.getStorageKey(trackingId, 'originalSource')
|
|
264
|
-
)
|
|
265
|
-
)
|
|
266
|
-
) ?? {}
|
|
267
|
-
);
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
// 获取采集实例配置内容 //? 多实例插件会重写该方法
|
|
271
|
-
// eslint-disable-next-line
|
|
272
|
-
getTrackerVds = (trackingId: string): any =>
|
|
273
|
-
this.growingIO.trackingId === trackingId
|
|
274
|
-
? { ...this.growingIO.vdsConfig }
|
|
275
|
-
: undefined;
|
|
276
|
-
|
|
277
|
-
// 初始化实例的配置项 // ?多实例插件会重写该方法
|
|
278
|
-
initTrackerOptions = (options: any) => {
|
|
279
|
-
const trackerOptions = this.initOptions(options);
|
|
280
|
-
trackerOptions.trackingId = options.trackingId;
|
|
281
|
-
this.growingIO.trackingId = options.trackingId;
|
|
282
|
-
this.growingIO.vdsConfig = trackerOptions;
|
|
283
|
-
return trackerOptions;
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
// 初始化实例的hooker // ?多实例插件会重写该方法
|
|
287
|
-
initTrackerHooker = (vdsConfig: OriginOptions) => {
|
|
288
|
-
const { platformConfig, inPlugin, minipInstance, plugins } = this.growingIO;
|
|
289
|
-
const { uniVue, taro } = vdsConfig;
|
|
290
|
-
// 如果是使用全量版本时,当前小程序不是框架的,要允许原生的hook
|
|
291
|
-
if (!(uniVue || taro || minipInstance.platform === 'quickapp')) {
|
|
292
|
-
platformConfig.canHook = true;
|
|
293
|
-
}
|
|
294
|
-
// 初始化核心钩子(重写全局)
|
|
295
|
-
if (!inPlugin || ['tbp', 'jdp'].includes(minipInstance.platform)) {
|
|
296
|
-
this.eventHooks.initEventHooks();
|
|
297
|
-
} else {
|
|
298
|
-
this.eventHooks.initOriginalValue();
|
|
299
|
-
}
|
|
300
|
-
/** 第三方框架重写 */
|
|
301
|
-
// uniapp
|
|
302
|
-
if (uniVue) {
|
|
303
|
-
plugins?.gioUniAppAdapter?.main();
|
|
304
|
-
}
|
|
305
|
-
// taro
|
|
306
|
-
if (taro) {
|
|
307
|
-
plugins?.gioTaroAdapter?.main();
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
// 初始化全局配置
|
|
312
|
-
initOptions = (userOptions: OriginOptions) => {
|
|
313
|
-
const { projectId, dataSourceId, appId, trackingId } = userOptions;
|
|
314
|
-
const configs: any = {};
|
|
315
|
-
// 只处理允许的配置项,之外用户的配置项无视
|
|
316
|
-
this.ALLOW_SETTING_KEYS.forEach((k) => {
|
|
317
|
-
// 配置项值不存在或类型不合法时置为默认值
|
|
318
|
-
const altp = DEFAULT_SETTINGS[k].type;
|
|
319
|
-
const invalid = isArray(altp)
|
|
320
|
-
? !altp.includes(typeOf(userOptions[k]))
|
|
321
|
-
: typeOf(userOptions[k]) !== DEFAULT_SETTINGS[k].type;
|
|
322
|
-
if (invalid) {
|
|
323
|
-
configs[k] = DEFAULT_SETTINGS[k].default;
|
|
324
|
-
} else if (k === 'ignoreFields') {
|
|
325
|
-
configs.ignoreFields = userOptions.ignoreFields.filter((o) =>
|
|
326
|
-
IGNORE_PARAMS.includes(o)
|
|
327
|
-
);
|
|
328
|
-
} else if (k === 'extraParams') {
|
|
329
|
-
configs.extraParams = userOptions.extraParams.filter((o) =>
|
|
330
|
-
EXTRA_INFO_PARAMS.includes(o)
|
|
331
|
-
);
|
|
332
|
-
} else {
|
|
333
|
-
configs[k] = userOptions[k];
|
|
334
|
-
// 数据采集和无埋点采集关闭时给提示
|
|
335
|
-
if (['dataCollect', 'autotrack'].includes(k) && !configs[k]) {
|
|
336
|
-
consoleText(`已关闭${ALLOW_SET_OPTIONS[k]}`, 'info');
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
|
-
// 发送间隔不允许设置为小数,如果是小数则取整
|
|
341
|
-
configs.uploadInterval = Math.round(configs.uploadInterval);
|
|
342
|
-
// 设置发送间隔小于0或者大于3秒的都认为不合法,改回1秒
|
|
343
|
-
if (
|
|
344
|
-
Number.isNaN(configs.uploadInterval) ||
|
|
345
|
-
configs.uploadInterval < 0 ||
|
|
346
|
-
configs.uploadInterval > 3000
|
|
347
|
-
) {
|
|
348
|
-
configs.uploadInterval = 1000;
|
|
349
|
-
}
|
|
350
|
-
// idMapping配置项兼容处理
|
|
351
|
-
if (configs.enableIdMapping && !configs.idMapping) {
|
|
352
|
-
configs.idMapping = true;
|
|
353
|
-
// 没有开启IDMapping的时候要把userKey清掉,防止之前有数被带到上报数据里
|
|
354
|
-
if (!configs.idMapping) {
|
|
355
|
-
this.growingIO.userStore.setUserKey(trackingId, '');
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
unset(configs, 'enableIdMapping');
|
|
359
|
-
// 是否开启强制插件模式
|
|
360
|
-
if (configs.pluginMode) {
|
|
361
|
-
this.growingIO.inPlugin = true;
|
|
362
|
-
}
|
|
363
|
-
// 请求超时时长设置的合法区间校验,值小于等于0ms认为不合法,改回5秒,浏览器默认有2分钟的上限
|
|
364
|
-
if (isNaN(Number(configs.requestTimeout)) || configs.requestTimeout <= 0) {
|
|
365
|
-
configs.requestTimeout = 5000;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// 曝光比例不在合法范围内改回默认值
|
|
369
|
-
if (configs.impressionScale < 0 || configs.impressionScale > 1) {
|
|
370
|
-
configs.impressionScale = 0;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// keepAlive有效时长不在合法范围内改回默认值
|
|
374
|
-
if (configs.keepAlive < 0 || configs.keepAlive > 60 * 24) {
|
|
375
|
-
configs.keepAlive = 5;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// session有效时长不在合法范围内改回默认值
|
|
379
|
-
if (configs.sessionExpires < 1 || configs.sessionExpires > 60 * 24) {
|
|
380
|
-
// -1为默认不超时切session
|
|
381
|
-
configs.sessionExpires = -1;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
return {
|
|
385
|
-
...configs,
|
|
386
|
-
projectId,
|
|
387
|
-
dataSourceId,
|
|
388
|
-
appId,
|
|
389
|
-
trackingId,
|
|
390
|
-
// 补充性能默认值
|
|
391
|
-
performance: {
|
|
392
|
-
monitor: configs.performance?.monitor ?? true,
|
|
393
|
-
exception: configs.performance?.exception ?? true,
|
|
394
|
-
network: configs.performance?.network ?? false
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
// 全局配置修改
|
|
400
|
-
setOption = (trackingId: string, k: string, v: any) => {
|
|
401
|
-
const { userStore, emitter } = this.growingIO;
|
|
402
|
-
const trackerVds = this.getTrackerVds(trackingId);
|
|
403
|
-
// 检查 k
|
|
404
|
-
const validKey = isString(k) && this.ALLOW_SETTING_KEYS.includes(k);
|
|
405
|
-
const validValue = validKey && typeof v === DEFAULT_SETTINGS[k]?.type;
|
|
406
|
-
if (validKey && validValue) {
|
|
407
|
-
const prevConfig = { ...trackerVds };
|
|
408
|
-
this.updateVdsConfig(trackingId, { ...trackerVds, [k]: v });
|
|
409
|
-
// 从关闭到打开dataCollect时补发visit和page
|
|
410
|
-
if (k === 'dataCollect' && prevConfig.dataCollect !== v && v) {
|
|
411
|
-
// 更新session
|
|
412
|
-
userStore.setSessionId(trackingId);
|
|
413
|
-
}
|
|
414
|
-
// 配置项有变更要全局广播
|
|
415
|
-
emitter.emit(EMIT_MSG.OPTION_CHANGE, { optionName: k, optionValue: v });
|
|
416
|
-
return true;
|
|
417
|
-
} else {
|
|
418
|
-
callError(`setOption > ${k}`);
|
|
419
|
-
return false;
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
// 获取全局配置
|
|
424
|
-
getOption = (trackingId: string, k?: string) => {
|
|
425
|
-
const trackerVds = this.getTrackerVds(trackingId);
|
|
426
|
-
if (k && has(this.growingIO.vdsConfig, k)) {
|
|
427
|
-
if (has(trackerVds, k)) {
|
|
428
|
-
return trackerVds[k];
|
|
429
|
-
} else {
|
|
430
|
-
return this.growingIO.vdsConfig[k];
|
|
431
|
-
}
|
|
432
|
-
} else if (isEmpty(k)) {
|
|
433
|
-
return trackerVds;
|
|
434
|
-
} else {
|
|
435
|
-
callError(`getOption > ${k}`);
|
|
436
|
-
return undefined;
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
// 根据实例更新内存和存储中的vds配置项值
|
|
441
|
-
updateVdsConfig = (trackingId: string, vds: any) => {
|
|
442
|
-
if (trackingId === this.growingIO.trackingId) {
|
|
443
|
-
this.growingIO.vdsConfig = { ...this.growingIO.vdsConfig, ...vds };
|
|
444
|
-
getGlobal().vdsConfig = this.growingIO.vdsConfig;
|
|
445
|
-
} else {
|
|
446
|
-
this.growingIO.subInstance[trackingId] = {
|
|
447
|
-
...this.growingIO.subInstance[trackingId],
|
|
448
|
-
...vds
|
|
449
|
-
};
|
|
450
|
-
getGlobal().vdsConfig.subInstance[trackingId] =
|
|
451
|
-
this.growingIO.subInstance[trackingId];
|
|
452
|
-
}
|
|
453
|
-
};
|
|
454
|
-
|
|
455
|
-
// 修改分享跳出判断值
|
|
456
|
-
toggleShareOut = (v?: boolean) => {
|
|
457
|
-
if (isBoolean(v)) {
|
|
458
|
-
this.shareOut = v;
|
|
459
|
-
} else {
|
|
460
|
-
this.shareOut = !this.shareOut;
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
|
|
464
|
-
// 手动调用visit事件的方法
|
|
465
|
-
sendVisit = (trackingId: string, props?: any) => {
|
|
466
|
-
(this.eventHooks.appEffects as any).buildVisitEvent(trackingId, props);
|
|
467
|
-
};
|
|
468
|
-
|
|
469
|
-
// 手动调用page事件的方法
|
|
470
|
-
sendPage = (trackingId: string, props?: any) => {
|
|
471
|
-
(this.eventHooks.pageEffects as any).buildPageEvent(trackingId, props);
|
|
472
|
-
};
|
|
473
|
-
|
|
474
|
-
// 事件的格式转换(同时移除无值的字段)
|
|
475
|
-
eventConverter = (event: EXTEND_EVENT) => {
|
|
476
|
-
const { dataStore, uploader } = this.growingIO;
|
|
477
|
-
const { dataCollect } = this.getTrackerVds(event.trackingId);
|
|
478
|
-
// 开启数据采集时才会处理事件、累计全局计数并将合成的事件提交到请求队列
|
|
479
|
-
if (dataCollect) {
|
|
480
|
-
//? 在4.0中用户属性事件和关闭事件不再带eventSequenceId字段
|
|
481
|
-
if (!['LOGIN_USER_ATTRIBUTES', 'APP_CLOSED'].includes(event.eventType)) {
|
|
482
|
-
//? 在4.0中event.eventSequenceId(esid)的值实际为全局的事件id,即值是globalSequenceId(gsid)为了保持向下兼容不做更名,会涉及存储
|
|
483
|
-
event.eventSequenceId = dataStore.getGsid(event.trackingId);
|
|
484
|
-
// 全局事件计数加1
|
|
485
|
-
dataStore.setGsid(event.trackingId, event.eventSequenceId + 1);
|
|
486
|
-
}
|
|
487
|
-
const convertedEvent: any = {};
|
|
488
|
-
forEach(event, (v: any, k: string) => {
|
|
489
|
-
/**
|
|
490
|
-
* 字段的转换
|
|
491
|
-
*/
|
|
492
|
-
// 无埋点element的转换
|
|
493
|
-
if (k === 'element') {
|
|
494
|
-
const target: object = head(v) ?? {};
|
|
495
|
-
forEach(target, (ev: any, ek: string) => {
|
|
496
|
-
// 判断属性是否存在,同时忽略无值属性(放入convertedEvent中)
|
|
497
|
-
if (!isEmpty(ev) || ev === 0) {
|
|
498
|
-
convertedEvent[ek] = ev;
|
|
499
|
-
}
|
|
500
|
-
});
|
|
501
|
-
} else if (!isEmpty(v) || v === 0) {
|
|
502
|
-
// 判断属性是否存在,同时忽略无值属性
|
|
503
|
-
convertedEvent[k] = v;
|
|
504
|
-
}
|
|
505
|
-
});
|
|
506
|
-
this.growingIO.emitter.emit(EMIT_MSG.ON_COMPOSE_AFTER, {
|
|
507
|
-
composedEvent: convertedEvent
|
|
508
|
-
});
|
|
509
|
-
// 提交请求队列
|
|
510
|
-
uploader.commitRequest({ ...convertedEvent, requestId: guid() });
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
|
|
514
|
-
// 事件重组
|
|
515
|
-
eventRefactor = (e: any) => {
|
|
516
|
-
// 有的事件的path和query不能再用页面的值,所以直接使用事件中已有的值
|
|
517
|
-
const ne = {
|
|
518
|
-
...e,
|
|
519
|
-
...this.eventContextBuilder(e.trackingId, {
|
|
520
|
-
path: e.path,
|
|
521
|
-
query: e.query,
|
|
522
|
-
// page事件需要使用事件中原有的时间戳,其他事件也要用原来的时间戳保证事件的准确性
|
|
523
|
-
timestamp: e.timestamp
|
|
524
|
-
})
|
|
525
|
-
};
|
|
526
|
-
// 防止事件原有的属性被通用属性直接覆盖
|
|
527
|
-
ne.attributes = {
|
|
528
|
-
...(ne.attributes ?? {}),
|
|
529
|
-
...(e.attributes ?? {})
|
|
530
|
-
};
|
|
531
|
-
return ne;
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
// 事件拦截器(有的通用维度字段需要异步获取,为了防止事件在通用维度还没返回之前就发出去)一般只有最开始的个别事件会被拦住
|
|
535
|
-
eventInterceptor = (event: any) => {
|
|
536
|
-
const { systemInfo, network } = this.growingIO.minipInstance;
|
|
537
|
-
if (isEmpty(systemInfo) || isEmpty(network)) {
|
|
538
|
-
// 没有通用维度字段,直接拦截存下来
|
|
539
|
-
this.interceptEvents.push(event);
|
|
540
|
-
} else if (!isEmpty(this.interceptEvents)) {
|
|
541
|
-
// 有通用维度字段了,且有被拦截的事件,放一起重新添加通用维度字段再提交转换
|
|
542
|
-
[...this.interceptEvents, event].forEach((e: any) => {
|
|
543
|
-
this.eventConverter(this.eventRefactor(e));
|
|
544
|
-
});
|
|
545
|
-
this.interceptEvents = [];
|
|
546
|
-
} else {
|
|
547
|
-
// 有通用维度字段了,也没有有被拦截的事件,直接提交转换
|
|
548
|
-
this.eventConverter(event);
|
|
549
|
-
}
|
|
550
|
-
};
|
|
551
|
-
|
|
552
|
-
// 系统信息异步获取完后对拦截队列进行检查,防止事件被卡住
|
|
553
|
-
eventReleaseInspector = () => {
|
|
554
|
-
const {
|
|
555
|
-
minipInstance: { systemInfo, network }
|
|
556
|
-
} = this.growingIO;
|
|
557
|
-
if (systemInfo && network && !isEmpty(this.interceptEvents)) {
|
|
558
|
-
[...this.interceptEvents].forEach((e: any) => {
|
|
559
|
-
this.eventConverter(this.eventRefactor(e));
|
|
560
|
-
});
|
|
561
|
-
this.interceptEvents = [];
|
|
562
|
-
}
|
|
563
|
-
};
|
|
564
|
-
|
|
565
|
-
// 构建应用/页面转发分享事件
|
|
566
|
-
buildAppMessageEvent = (trackingId: string, args: any) => {
|
|
567
|
-
const originResult = args[0];
|
|
568
|
-
let updateResult;
|
|
569
|
-
if (args.length >= 2) {
|
|
570
|
-
updateResult = args[1];
|
|
571
|
-
} else if (args.length === 1) {
|
|
572
|
-
updateResult = originResult;
|
|
573
|
-
}
|
|
574
|
-
const {
|
|
575
|
-
dataStore: { eventContextBuilder, eventInterceptor }
|
|
576
|
-
} = this.growingIO;
|
|
577
|
-
const uri = (updateResult?.path || '').split('?');
|
|
578
|
-
const event = {
|
|
579
|
-
eventType: 'CUSTOM',
|
|
580
|
-
eventName: '$mp_on_share',
|
|
581
|
-
...eventContextBuilder(trackingId)
|
|
582
|
-
};
|
|
583
|
-
event.attributes = limitObject({
|
|
584
|
-
...(event.attributes ?? {}),
|
|
585
|
-
$from: originResult?.from,
|
|
586
|
-
$target: originResult?.target?.id,
|
|
587
|
-
$share_title: updateResult?.title,
|
|
588
|
-
$share_path: head(uri),
|
|
589
|
-
$share_query: uri[1],
|
|
590
|
-
...updateResult?.attributes
|
|
591
|
-
});
|
|
592
|
-
eventInterceptor(event);
|
|
593
|
-
};
|
|
594
|
-
|
|
595
|
-
// 构建朋友圈分享事件
|
|
596
|
-
buildTimelineEvent = (trackingId: string, args: any) => {
|
|
597
|
-
const originResult = args[0];
|
|
598
|
-
let updateResult;
|
|
599
|
-
if (args.length >= 2) {
|
|
600
|
-
updateResult = args[1];
|
|
601
|
-
} else if (args.length === 1) {
|
|
602
|
-
updateResult = originResult;
|
|
603
|
-
}
|
|
604
|
-
const {
|
|
605
|
-
dataStore: { eventContextBuilder, eventInterceptor }
|
|
606
|
-
} = this.growingIO;
|
|
607
|
-
const uri = (updateResult?.path || '').split('?');
|
|
608
|
-
const event = {
|
|
609
|
-
eventType: 'CUSTOM',
|
|
610
|
-
eventName: '$mp_share_timeline',
|
|
611
|
-
...eventContextBuilder(trackingId)
|
|
612
|
-
};
|
|
613
|
-
event.attributes = limitObject({
|
|
614
|
-
...(event.attributes ?? {}),
|
|
615
|
-
$target: originResult?.target?.id,
|
|
616
|
-
$share_title: updateResult?.title,
|
|
617
|
-
$share_path: head(uri),
|
|
618
|
-
$share_query: uri[1],
|
|
619
|
-
...updateResult?.attributes
|
|
620
|
-
});
|
|
621
|
-
eventInterceptor(event);
|
|
622
|
-
};
|
|
623
|
-
|
|
624
|
-
// 构建添加收藏事件
|
|
625
|
-
buildAddFavorites = (trackingId: string, args: any) => {
|
|
626
|
-
const originResult = args[0];
|
|
627
|
-
let updateResult;
|
|
628
|
-
if (args.length >= 2) {
|
|
629
|
-
updateResult = args[1];
|
|
630
|
-
} else if (args.length === 1) {
|
|
631
|
-
updateResult = originResult;
|
|
632
|
-
}
|
|
633
|
-
const {
|
|
634
|
-
dataStore: { eventContextBuilder, eventInterceptor }
|
|
635
|
-
} = this.growingIO;
|
|
636
|
-
const uri = (updateResult?.path || '').split('?');
|
|
637
|
-
const event = {
|
|
638
|
-
eventType: 'CUSTOM',
|
|
639
|
-
eventName: '$mp_add_favorites',
|
|
640
|
-
...eventContextBuilder(trackingId)
|
|
641
|
-
};
|
|
642
|
-
event.attributes = limitObject({
|
|
643
|
-
...(event.attributes ?? {}),
|
|
644
|
-
$share_title: updateResult?.title,
|
|
645
|
-
$share_path: head(uri),
|
|
646
|
-
$share_query: uri[1]
|
|
647
|
-
});
|
|
648
|
-
eventInterceptor(event);
|
|
649
|
-
};
|
|
650
|
-
|
|
651
|
-
// 需要对所有trackingId执行的逻辑
|
|
652
|
-
trackersExecute = (callback: (trackingId: string) => any) => {
|
|
653
|
-
this.initializedTrackingIds.forEach((trackingId: string) => {
|
|
654
|
-
if (typeOf(callback) === 'function') {
|
|
655
|
-
callback(trackingId);
|
|
656
|
-
}
|
|
657
|
-
});
|
|
658
|
-
};
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
export default DataStore;
|