Skip to content

Commit 8982d20

Browse files
authored
Migrate experiments and helpfulness to Hydro (github#16059)
* Migrate experiments and helpfulness to Hydro * Clean out old tests * ...and more old tests to delete
1 parent 23ad32b commit 8982d20

8 files changed

Lines changed: 285 additions & 642 deletions

File tree

javascripts/experiment.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,21 @@
11
import murmur from 'imurmurhash'
2-
import { v4 as uuidv4 } from 'uuid'
3-
import Cookies from 'js-cookie'
4-
import getCsrf from './get-csrf'
2+
import { getUserEventsId, sendEvent } from './events'
53

64
const TREATMENT = 'TREATMENT'
75
const CONTROL = 'CONTROL'
8-
const COOKIE_NAME = '_docs-experiment'
9-
10-
let cookieValue
11-
12-
export function getUserExperimentId () {
13-
if (cookieValue) return cookieValue
14-
cookieValue = Cookies.get(COOKIE_NAME)
15-
if (cookieValue) return cookieValue
16-
cookieValue = uuidv4()
17-
Cookies.set(COOKIE_NAME, cookieValue, {
18-
secure: true,
19-
sameSite: 'strict',
20-
expires: 365
21-
})
22-
return cookieValue
23-
}
246

257
export function bucket (test) {
26-
const id = getUserExperimentId()
8+
const id = getUserEventsId()
279
const hash = murmur(test).hash(id).result()
2810
return hash % 2 ? TREATMENT : CONTROL
2911
}
3012

3113
export async function sendSuccess (test) {
32-
return fetch('/events', {
33-
method: 'POST',
34-
headers: {
35-
'Content-Type': 'application/json',
36-
'CSRF-Token': getCsrf()
37-
},
38-
body: JSON.stringify({
39-
type: 'EXPERIMENT',
40-
user: getUserExperimentId(),
41-
test,
42-
group: bucket(test).toLowerCase(),
43-
success: 'yes'
44-
})
14+
return sendEvent({
15+
type: 'experiment',
16+
experiment_name: test,
17+
experiment_variation: bucket(test).toLowerCase(),
18+
experiment_success: true
4519
})
4620
}
4721

javascripts/helpfulness.js

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import getCsrf from './get-csrf'
1+
import { sendEvent } from './events'
22

33
export default function helpfulness () {
4-
const EVENT_TYPE = 'HELPFULNESS'
5-
64
const forms = Array.from(document.querySelectorAll('.js-helpfulness'))
75
const texts = Array.from(document.querySelectorAll('.js-helpfulness input, .js-helpfulness textarea'))
86
const votes = Array.from(document.querySelectorAll('.js-helpfulness [type=radio]'))
97
if (!forms.length || !texts.length || !votes.length) return
108

11-
let id = '' // So that we only create one event per pageview
12-
139
forms.forEach(form => {
1410
form.addEventListener('submit', async evt => {
1511
evt.preventDefault()
@@ -82,25 +78,13 @@ export default function helpfulness () {
8278
return trackEvent(data)
8379
}
8480

85-
async function trackEvent ({ token, vote, email, comment, category }) {
86-
const response = await fetch(id ? '/events/' + id : '/events', {
87-
method: id ? 'PUT' : 'POST',
88-
headers: {
89-
'Content-Type': 'application/json',
90-
'CSRF-Token': getCsrf()
91-
},
92-
body: JSON.stringify({
93-
type: EVENT_TYPE,
94-
token, // Honeypot
95-
url: window.location.origin + window.location.pathname,
96-
vote,
97-
email,
98-
comment,
99-
category
100-
})
81+
async function trackEvent ({ token, vote, email, comment }) {
82+
return sendEvent({
83+
type: 'survey',
84+
token, // Honeypot
85+
survey_vote: vote === 'Yes',
86+
survey_comment: comment,
87+
survey_email: email
10188
})
102-
const data = response.ok ? await response.json() : {}
103-
if (data.id) id = data.id
104-
return id
10589
}
10690
}

0 commit comments

Comments
 (0)