aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/helpers/higher-order-components/metametrics/metametrics.provider.js
blob: 6086e03fb678da1944b259ad0eb1588a64a37fbd (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Component } from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { withRouter } from 'react-router-dom'
import { compose } from 'recompose'
import {
  getCurrentNetworkId,
  getSelectedAsset,
  getAccountType,
  getNumberOfAccounts,
  getNumberOfTokens,
} from '../../../selectors/selectors'
import {
  txDataSelector,
} from '../../../selectors/confirm-transaction'
import { getEnvironmentType } from '../../../../../app/scripts/lib/util'
import {
  sendMetaMetricsEvent,
  sendCountIsTrackable,
} from '../../utils/metametrics.util'

class MetaMetricsProvider extends Component {
  static propTypes = {
    network: PropTypes.string.isRequired,
    environmentType: PropTypes.string.isRequired,
    activeCurrency: PropTypes.string.isRequired,
    accountType: PropTypes.string.isRequired,
    metaMetricsSendCount: PropTypes.number.isRequired,
    children: PropTypes.object.isRequired,
    history: PropTypes.object.isRequired,
  }

  static childContextTypes = {
    metricsEvent: PropTypes.func,
  }

  constructor (props) {
    super(props)

    this.state = {
      previousPath: '',
      currentPath: window.location.href,
    }

    props.history.listen(locationObj => {
      this.setState({
        previousPath: this.state.currentPath,
        currentPath: window.location.href,
      })
    })
  }

  getChildContext () {
    const props = this.props
    const { pathname } = location
    const { previousPath, currentPath } = this.state

    return {
      metricsEvent: (config = {}, overrides = {}) => {
        const { eventOpts = {} } = config
        const { name = '' } = eventOpts
        const { pathname: overRidePathName = '' } = overrides
        const isSendFlow = Boolean(name.match(/^send|^confirm/) || overRidePathName.match(/send|confirm/))

        if (props.participateInMetaMetrics || config.isOptIn) {
          return sendMetaMetricsEvent({
            ...props,
            ...config,
            previousPath,
            currentPath,
            pathname,
            excludeMetaMetricsId: isSendFlow && !sendCountIsTrackable(props.metaMetricsSendCount + 1),
            ...overrides,
          })
        }
      },
    }
  }

  render () {
    return this.props.children
  }
}

const mapStateToProps = state => {
  const txData = txDataSelector(state) || {}

  return {
    network: getCurrentNetworkId(state),
    environmentType: getEnvironmentType(),
    activeCurrency: getSelectedAsset(state),
    accountType: getAccountType(state),
    confirmTransactionOrigin: txData.origin,
    metaMetricsId: state.metamask.metaMetricsId,
    participateInMetaMetrics: state.metamask.participateInMetaMetrics,
    metaMetricsSendCount: state.metamask.metaMetricsSendCount,
    numberOfTokens: getNumberOfTokens(state),
    numberOfAccounts: getNumberOfAccounts(state),
  }
}

module.exports = compose(
  withRouter,
  connect(mapStateToProps)
)(MetaMetricsProvider)