-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGAListener.jsx
62 lines (50 loc) · 1.51 KB
/
GAListener.jsx
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
import React from "react";
import { PropTypes } from "prop-types";
import ReactGA from "react-ga";
import GAEventContext from "./GAEventContext";
import GAListenerActive from "./GAListenerActive";
// Renders GA+children in production, only children for the rest
class GAListener extends React.Component {
createEvent = (...args) => {
const event = {};
if (args.length > 1) {
// Generate a React-GA event from arguments
const [origin, help, status] = args;
Object.assign(event, {
category: origin,
action: help,
});
if (typeof status === "string") {
event.label = status;
}
} else {
// Arguments is already a React-GA event
Object.assign(event, args[0]);
}
console.log("event:", event);
return event;
};
sendEvent = (...args) => ReactGA.event(this.createEvent(...args));
render() {
const { children, trackOptIn, ...props } = this.props;
// Disable GA unless user has opt'ed in to tracking
return trackOptIn ? (
<GAEventContext.Provider value={this.sendEvent}>
<GAListenerActive {...props}>{children}</GAListenerActive>
</GAEventContext.Provider>
) : (
<GAEventContext.Provider value={this.createEvent}>
{children}
</GAEventContext.Provider>
);
}
}
GAListener.defaultProps = {
...GAListenerActive.defaultProps,
trackOptIn: false,
};
GAListener.propTypes = {
...GAListenerActive.propTypes,
trackOptIn: PropTypes.bool.isRequired,
};
export default GAListener;