diff --git a/gui/web/src/components/atoms/Button/Button.js b/gui/web/src/components/atoms/Button/Button.js index 206a3db38..6291c78a8 100644 --- a/gui/web/src/components/atoms/Button/Button.js +++ b/gui/web/src/components/atoms/Button/Button.js @@ -4,6 +4,7 @@ import classNames from 'classnames'; import styles from './Button.module.scss'; import Icon from '../Icon/Icon'; import LoadingAnimation from '../LoadingAnimation/LoadingAnimation'; +import sendMetricEvent from '../../../kelp-ops-api/sendMetricEvent'; const iconSizes = { small:'10px', @@ -18,6 +19,14 @@ const iconSizesRound = { } class Button extends Component { + constructor(props) { + super(props); + this.trackOnClick = this.trackOnClick.bind(this); + this.sendMetricEvent = this.sendMetricEvent.bind(this); + + this._asyncRequests = {}; + } + static defaultProps = { icon: null, size: 'medium', @@ -25,7 +34,7 @@ class Button extends Component { hsize: 'regular', loading: false, onClick: () => {}, - disabled: false + disabled: false, } static propTypes = { @@ -34,8 +43,43 @@ class Button extends Component { variant: PropTypes.string, onClick: PropTypes.func, loading: PropTypes.bool, - disabled: PropTypes.bool + disabled: PropTypes.bool, + eventName: PropTypes.string, }; + + sendMetricEvent() { + if (this._asyncRequests["sendMetricEvent"]) { + return + } + + if (this.props.eventName === "" || this.props.eventName === undefined) { + return + } + + var _this = this + var eventData = { + 'eventName': this.props.eventName, + 'type': 'generic', + 'component': 'button' + } + this._asyncRequests["sendMetricEvent"] = sendMetricEvent(this.props.baseUrl, this.props.eventName, eventData).then(resp => { + if (!_this._asyncRequests["sendMetricEvent"]) { + // if it has been deleted it means we don't want to process the result + return + } + + delete _this._asyncRequests["sendMetricEvent"]; + + if (resp["success"] === undefined) { + console.log(resp["error"]) + } + }) + } + + trackOnClick() { + this.props.onClick() + this.sendMetricEvent() + } render() { const iconOnly = this.props.children ? null : styles.iconOnly; @@ -57,7 +101,7 @@ class Button extends Component {