-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalytics.ts
165 lines (144 loc) · 3.64 KB
/
analytics.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { PerfKeeper, PerfEntry } from '@perf-tools/keeper';
import { ReCatpchaAttempt, ReCaptchaWidgetCfg } from './src/api';
import { ReCaptchaProps } from './react';
export type ReCaptchaAnalyticsOptions = {
keeper?: PerfKeeper;
keeperGroupName?: string;
keeperSendBySubmit?: boolean;
}
export type ReCaptchaAnalytics<N extends string> = {
name: N;
clear: () => void;
get: () => ReCaptchaAnalyticsStats;
mixin<
K extends keyof ReCaptchaAnalyticsMixin,
R extends ReCaptchaAnalyticsMixin[K],
>(type: K, override?: Partial<R>): R;
}
export type ReCaptchaAnalyticsStats = Record<
Exclude<ReCatpchaAttempt['state'] | 'show' | 'hide', 'start'>,
number
> & {
attempts: ReCatpchaAttempt[];
}
export type ReCaptchaAnalyticsMixin = {
native: Pick<ReCaptchaWidgetCfg,
| 'onchallengeshow'
| 'onchallengehide'
| 'onstartattempt'
| 'onattempt'
>;
react: Pick<ReCaptchaProps,
| 'onChallengeShow'
| 'onChallengeHide'
| 'onStartAttempt'
| 'onAttempt'
>;
}
export function createReCaptchaAnalytics<N extends string>(
name: N,
options: ReCaptchaAnalyticsOptions = {},
): ReCaptchaAnalytics<N> {
let stats: ReCaptchaAnalyticsStats;
let timer: PerfEntry | undefined;
const {
keeper,
keeperGroupName = 'recaptcha',
keeperSendBySubmit = true,
} = options;
const onChallengeShow = () => { stats.show++; };
const onChallengeHide = () => { stats.hide++; };
const onStartAttempt = (attempt: ReCatpchaAttempt) => {
timer = keeper && keeper.group(keeperGroupName, true).group(name).time('try');
stats.attempts.push(attempt);
};
const onAttempt = (attempt: ReCatpchaAttempt) => {
if (timer) {
timer.stop();
timer.parent.add(`try_${attempt.state}`, attempt.start, attempt.start + attempt.duration);
timer.parent.stop();
timer.parent.parent.stop();
timer = undefined;
}
stats[attempt.state]++;
};
function clear() {
stats = {
show: 0,
hide: 0,
verified: 0,
cancelled: 0,
expired: 0,
error: 0,
reset: 0,
attempts: [],
};
}
if (keeper) {
const send = () => {
const count = stats.attempts.length;
if (count > 0) {
const group = keeper.group(keeperGroupName, 0).group(name, 0);
group.unit = 'raw';
Object.keys(stats).forEach(key => {
if (key !== 'attempts') {
group.add(key, 0, stats[key]);
}
});
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
let avg = 0;
stats.attempts.forEach(({duration}) => {
min = Math.min(min, duration);
max = Math.max(max, duration);
avg += duration;
});
group.add('attempts', 0, count);
group.unit = 'ms';
group.add('time_min', 0, min);
group.add('time_max', 0, max);
group.add('time_avg', 0, avg / count);
group.stop(max);
group.parent.stop(max);
clear();
}
};
window.addEventListener('beforeunload', send);
keeperSendBySubmit && document.addEventListener('submit', send, true);
}
clear();
return {
name,
clear,
get: () => stats,
mixin<
K extends keyof ReCaptchaAnalyticsMixin,
R extends ReCaptchaAnalyticsMixin[K],
>(type: K, override?: Partial<R>): R {
let props: R;
if (type === 'native') {
props = {
onchallengeshow: onChallengeShow,
onchallengehide: onChallengeHide,
onstartattempt: onStartAttempt,
onattempt: onAttempt,
} as R;
} else if (type === 'react') {
props = {
onChallengeShow,
onChallengeHide,
onStartAttempt,
onAttempt,
} as R;
}
override && Object.keys(override).forEach(key => {
const fn = props[key];
props[key] = function () {
fn.apply(this, arguments);
override[key].apply(this, arguments);
};
});
return props;
},
};
}