about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/components/admin/Retention.js
blob: 9127839f6abd465775f211e36919e646d200c1f6 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React from 'react';
import PropTypes from 'prop-types';
import api from 'flavours/glitch/util/api';
import { FormattedMessage, FormattedNumber, FormattedDate } from 'react-intl';
import classNames from 'classnames';
import { roundTo10 } from 'flavours/glitch/util/numbers';

const dateForCohort = cohort => {
  switch(cohort.frequency) {
  case 'day':
    return <FormattedDate value={cohort.period} month='long' day='2-digit' />;
  default:
    return <FormattedDate value={cohort.period} month='long' year='numeric' />;
  }
};

export default class Retention extends React.PureComponent {

  static propTypes = {
    start_at: PropTypes.string,
    end_at: PropTypes.string,
    frequency: PropTypes.string,
  };

  state = {
    loading: true,
    data: null,
  };

  componentDidMount () {
    const { start_at, end_at, frequency } = this.props;

    api().post('/api/v1/admin/retention', { start_at, end_at, frequency }).then(res => {
      this.setState({
        loading: false,
        data: res.data,
      });
    }).catch(err => {
      console.error(err);
    });
  }

  render () {
    const { loading, data } = this.state;
    const { frequency } = this.props;

    let content;

    if (loading) {
      content = <FormattedMessage id='loading_indicator.label' defaultMessage='Loading...' />;
    } else {
      content = (
        <table className='retention__table'>
          <thead>
            <tr>
              <th>
                <div className='retention__table__date retention__table__label'>
                  <FormattedMessage id='admin.dashboard.retention.cohort' defaultMessage='Sign-up month' />
                </div>
              </th>

              <th>
                <div className='retention__table__number retention__table__label'>
                  <FormattedMessage id='admin.dashboard.retention.cohort_size' defaultMessage='New users' />
                </div>
              </th>

              {data[0].data.slice(1).map((retention, i) => (
                <th key={retention.date}>
                  <div className='retention__table__number retention__table__label'>
                    {i + 1}
                  </div>
                </th>
              ))}
            </tr>

            <tr>
              <td>
                <div className='retention__table__date retention__table__average'>
                  <FormattedMessage id='admin.dashboard.retention.average' defaultMessage='Average' />
                </div>
              </td>

              <td>
                <div className='retention__table__size'>
                  <FormattedNumber value={data.reduce((sum, cohort, i) => sum + ((cohort.data[0].value * 1) - sum) / (i + 1), 0)} maximumFractionDigits={0} />
                </div>
              </td>

              {data[0].data.slice(1).map((retention, i) => {
                const average = data.reduce((sum, cohort, k) => cohort.data[i + 1] ? sum + (cohort.data[i + 1].percent - sum)/(k + 1) : sum, 0);

                return (
                  <td key={retention.date}>
                    <div className={classNames('retention__table__box', 'retention__table__average', `retention__table__box--${roundTo10(average * 100)}`)}>
                      <FormattedNumber value={average} style='percent' />
                    </div>
                  </td>
                );
              })}
            </tr>
          </thead>

          <tbody>
            {data.slice(0, -1).map(cohort => (
              <tr key={cohort.period}>
                <td>
                  <div className='retention__table__date'>
                    {dateForCohort(cohort)}
                  </div>
                </td>

                <td>
                  <div className='retention__table__size'>
                    <FormattedNumber value={cohort.data[0].value} />
                  </div>
                </td>

                {cohort.data.slice(1).map(retention => (
                  <td key={retention.date}>
                    <div className={classNames('retention__table__box', `retention__table__box--${roundTo10(retention.percent * 100)}`)}>
                      <FormattedNumber value={retention.percent} style='percent' />
                    </div>
                  </td>
                ))}
              </tr>
            ))}
          </tbody>
        </table>
      );
    }

    let title = null;
    switch(frequency) {
    case 'day':
      title = <FormattedMessage id='admin.dashboard.daily_retention' defaultMessage='User retention rate by day after sign-up' />;
      break;
    default:
      title = <FormattedMessage id='admin.dashboard.monthly_retention' defaultMessage='User retention rate by month after sign-up' />;
    };

    return (
      <div className='retention'>
        <h4>{title}</h4>

        {content}
      </div>
    );
  }

}