@tmsfe/tms-core 0.0.89 → 0.0.90
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/package.json
CHANGED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 负责小程序级的全埋点
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// / <reference path='./types.ts'/>
|
|
6
|
+
|
|
7
|
+
import helper from './helper';
|
|
8
|
+
|
|
9
|
+
// 劫持导航api
|
|
10
|
+
function proxyNavigateApi(api: string): void {
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
const originalApi = wx[api];
|
|
13
|
+
Object.defineProperty(wx, api, {
|
|
14
|
+
writable: true,
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
value(...args: any) {
|
|
18
|
+
const { url = '' } = args[0] || {};
|
|
19
|
+
const [path, params = ''] = url.split('?');
|
|
20
|
+
helper.reportData(`wx_${api}_before`, path, params);
|
|
21
|
+
|
|
22
|
+
originalApi.apply(this, args);
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 劫持消息订阅接口
|
|
28
|
+
function proxySubscribeMessage(): void {
|
|
29
|
+
const originalApi = wx.requestSubscribeMessage;
|
|
30
|
+
Object.defineProperty(wx, 'requestSubscribeMessage', {
|
|
31
|
+
writable: true,
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
value(options: any) {
|
|
35
|
+
const { tmplIds } = options;
|
|
36
|
+
const originalSuccess = options.success || helper.emptyFunc;
|
|
37
|
+
const originalFail = options.fail || helper.emptyFunc;
|
|
38
|
+
// eslint-disable-next-line
|
|
39
|
+
options.success = function(res: any) {
|
|
40
|
+
for (const tmplId of tmplIds) {
|
|
41
|
+
const result = res[tmplId];
|
|
42
|
+
if (result) {
|
|
43
|
+
helper.reportData('wx_requestSubscribeMessage_result', tmplId, result);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
originalSuccess.call(this, res);
|
|
47
|
+
};
|
|
48
|
+
// eslint-disable-next-line
|
|
49
|
+
options.fail = function(err: any) {
|
|
50
|
+
helper.reportData('wx_requestSubscribeMessage_fail', tmplIds, err.errMsg);
|
|
51
|
+
originalFail.call(this, err);
|
|
52
|
+
};
|
|
53
|
+
originalApi.call(this, options);
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 监听onAppShow跟onAppHide
|
|
59
|
+
function listenerAppVisible(): void {
|
|
60
|
+
let showCount = 0; // 小程序切到前台次数(onAppShow)
|
|
61
|
+
let hideCount = 0; // 小程序切到后台次数(onAppHide)
|
|
62
|
+
let showTime = 0; // 触发onAppShow的时间
|
|
63
|
+
let hideTime = 0; // 触发onAppHide的时间
|
|
64
|
+
wx.onAppShow(() => {
|
|
65
|
+
showCount += 1;
|
|
66
|
+
showTime = Date.now();
|
|
67
|
+
// 距离onAppHide触发的时长
|
|
68
|
+
const lessHideTime = hideCount === 0 ? 0 : showTime - hideTime;
|
|
69
|
+
helper.reportData('App_onAppShow', { showCount, hideCount, lessHideTime });
|
|
70
|
+
});
|
|
71
|
+
wx.onAppHide(() => {
|
|
72
|
+
hideCount += 1;
|
|
73
|
+
hideTime = Date.now();
|
|
74
|
+
// 距离onAppShow触发的时长
|
|
75
|
+
const lessShowTime = hideTime - showTime;
|
|
76
|
+
helper.fastReportData('App_onAppHide', { showCount, hideCount, lessShowTime });
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 劫持App接口
|
|
81
|
+
function init(): void {
|
|
82
|
+
listenerAppVisible();
|
|
83
|
+
proxySubscribeMessage();
|
|
84
|
+
proxyNavigateApi('navigateTo');
|
|
85
|
+
proxyNavigateApi('redirectTo');
|
|
86
|
+
proxyNavigateApi('reLaunch');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export default {
|
|
90
|
+
init,
|
|
91
|
+
};
|
|
@@ -4,17 +4,26 @@
|
|
|
4
4
|
|
|
5
5
|
// / <reference path='./types.ts'/>
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
function getReporter(): any {
|
|
8
|
+
// 如果是在app.js的onLaunch中调用,则没有getApp().tms为空
|
|
9
|
+
const tms = getApp()?.tms || wx.tms;
|
|
10
|
+
return tms.getReporter();
|
|
11
|
+
}
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* 聚合上报埋点
|
|
15
|
+
*/
|
|
9
16
|
function reportData(...args: any[]): any {
|
|
10
|
-
if (reporter === null) {
|
|
11
|
-
// 如果是在app.js的onLaunch中调用,则没有getApp().tms为空
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
const tms = getApp()?.tms || wx.tms;
|
|
14
|
-
reporter = tms.getReporter();
|
|
15
|
-
}
|
|
16
17
|
console.log('自动埋点:', ...args);
|
|
17
|
-
|
|
18
|
+
getReporter().report2(...args);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 立即上报埋点
|
|
23
|
+
*/
|
|
24
|
+
function fastReportData(...args: any[]): any {
|
|
25
|
+
console.log('快速自动埋点:', ...args);
|
|
26
|
+
getReporter().fastReport2(...args);
|
|
18
27
|
}
|
|
19
28
|
|
|
20
29
|
let systemInfo: any = null;
|
|
@@ -90,6 +99,7 @@ function executeFunc(context: any, func: Function, args: any[], callback: Functi
|
|
|
90
99
|
export default {
|
|
91
100
|
emptyFunc,
|
|
92
101
|
reportData,
|
|
102
|
+
fastReportData,
|
|
93
103
|
executeFunc,
|
|
94
104
|
getSystemInfo,
|
|
95
105
|
getLastBindEvent,
|
|
@@ -2,17 +2,16 @@
|
|
|
2
2
|
* 负责页面和组件的全埋点
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import proxyApp from './app';
|
|
5
6
|
import proxyPage from './page';
|
|
6
7
|
import proxyComponent from './component';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
proxyApp.init();
|
|
10
|
+
proxyPage.init();
|
|
11
|
+
proxyComponent.init();
|
|
9
12
|
|
|
13
|
+
// 目前没有需要控制初始化时机的场景
|
|
10
14
|
function init(): void {
|
|
11
|
-
if (!isInit) {
|
|
12
|
-
isInit = true;
|
|
13
|
-
proxyPage.init();
|
|
14
|
-
proxyComponent.init();
|
|
15
|
-
}
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
export default {
|
package/src/report/proxy/page.ts
CHANGED
|
@@ -122,62 +122,9 @@ function proxyPage(): void {
|
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
// 劫持导航api
|
|
126
|
-
function proxyNavigateApi(api: string): void {
|
|
127
|
-
// @ts-ignore
|
|
128
|
-
const originalApi = wx[api];
|
|
129
|
-
Object.defineProperty(wx, api, {
|
|
130
|
-
writable: true,
|
|
131
|
-
enumerable: true,
|
|
132
|
-
configurable: true,
|
|
133
|
-
value(...args: any) {
|
|
134
|
-
const { url = '' } = args[0] || {};
|
|
135
|
-
const [path, params = ''] = url.split('?');
|
|
136
|
-
helper.reportData(`wx_${api}_before`, path, params);
|
|
137
|
-
|
|
138
|
-
originalApi.apply(this, args);
|
|
139
|
-
},
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// 劫持消息订阅接口
|
|
144
|
-
function proxySubscribeMessage(): void {
|
|
145
|
-
const originalApi = wx.requestSubscribeMessage;
|
|
146
|
-
Object.defineProperty(wx, 'requestSubscribeMessage', {
|
|
147
|
-
writable: true,
|
|
148
|
-
enumerable: true,
|
|
149
|
-
configurable: true,
|
|
150
|
-
value(options: any) {
|
|
151
|
-
const { tmplIds } = options;
|
|
152
|
-
const originalSuccess = options.success || helper.emptyFunc;
|
|
153
|
-
const originalFail = options.fail || helper.emptyFunc;
|
|
154
|
-
// eslint-disable-next-line
|
|
155
|
-
options.success = function(res: any) {
|
|
156
|
-
for (const tmplId of tmplIds) {
|
|
157
|
-
const result = res[tmplId];
|
|
158
|
-
if (result) {
|
|
159
|
-
helper.reportData('wx_requestSubscribeMessage_result', tmplId, result);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
originalSuccess.call(this, res);
|
|
163
|
-
};
|
|
164
|
-
// eslint-disable-next-line
|
|
165
|
-
options.fail = function(err: any) {
|
|
166
|
-
helper.reportData('wx_requestSubscribeMessage_fail', tmplIds, err.errMsg);
|
|
167
|
-
originalFail.call(this, err);
|
|
168
|
-
};
|
|
169
|
-
originalApi.call(this, options);
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
125
|
// 劫持Page
|
|
175
126
|
function init(): void {
|
|
176
127
|
proxyPage();
|
|
177
|
-
proxySubscribeMessage();
|
|
178
|
-
proxyNavigateApi('navigateTo');
|
|
179
|
-
proxyNavigateApi('redirectTo');
|
|
180
|
-
proxyNavigateApi('reLaunch');
|
|
181
128
|
}
|
|
182
129
|
|
|
183
130
|
export default {
|