forked from azoth1991/autoTrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoTrack.js
63 lines (56 loc) · 1.5 KB
/
AutoTrack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 页面级的埋点只执行一次
* onclick的埋点可以反复执行
*/
import React from 'react';
const trackList = [];
let logpage = (options) => {
if (trackList.indexOf(options.category) === -1) {
trackList.push(options.category || 'category');
}
};
let logevent = (options) => {
// console.log('logevent',options);
};
const onMouseEvent = function (callback, logData, event) {
if (typeof callback === 'function') {
callback(event);
}
logevent(logData);
};
const propsWithEvents = function (type, props) {
const newProps = Object.assign(props);
if (!Object.isFrozen(newProps)) {
newProps.onClick = onMouseEvent.bind(null, props.onClick, props.logevent);
if (typeof Object.freeze === 'function') {
Object.freeze(newProps);
}
}
return newProps;
};
const autoTrack = (config) => {
if (config.pageCallback) {
let orginlogpage = logpage;
logpage = function() {
orginlogpage.apply(this,arguments);
config.pageCallback.apply(this,arguments);
}
}
if (config.eventCallback) {
logevent = config.eventCallback;
}
const originCreateElement = React.createElement;
React.createElement = function (...args) {
const type = args[0];
const props = args[1] || {};
const newArgs = args;
if (props.logpage) {
logpage(props.logpage);
}
if (props.onClick && props.logevent) {
newArgs[1] = propsWithEvents(type, props || {});
}
return originCreateElement.apply(null, newArgs);
};
};
export default autoTrack;