Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

survey.ts 2.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. import { sendEvent, EventType } from './events'
  2. function showElement(el: HTMLElement) {
  3. el.removeAttribute('hidden')
  4. }
  5. function hideElement(el: HTMLElement) {
  6. el.setAttribute('hidden', 'hidden')
  7. }
  8. function updateDisplay(form: HTMLFormElement, state: string) {
  9. const allSelector = ['start', 'yes', 'no', 'end']
  10. .map((xstate) => '[data-help-' + xstate + ']')
  11. .join(',')
  12. const stateSelector = '[data-help-' + state + ']'
  13. const allEls = Array.from(form.querySelectorAll(allSelector)) as Array<HTMLElement>
  14. allEls.forEach(hideElement)
  15. const stateEls = Array.from(form.querySelectorAll(stateSelector)) as Array<HTMLElement>
  16. stateEls.forEach(showElement)
  17. }
  18. function submitForm(form: HTMLFormElement) {
  19. const formData = new FormData(form)
  20. return trackEvent(formData)
  21. }
  22. function trackEvent(formData: FormData) {
  23. return sendEvent({
  24. type: EventType.survey,
  25. survey_token: (formData.get('survey-token') as string) || undefined, // Honeypot
  26. survey_vote: formData.get('survey-vote') === 'Yes',
  27. survey_comment: (formData.get('survey-comment') as string) || undefined,
  28. survey_email: (formData.get('survey-email') as string) || undefined,
  29. })
  30. }
  31. export default function survey() {
  32. // @ts-ignore
  33. if (window.IS_NEXTJS_PAGE) return
  34. const form = document.querySelector('.js-survey') as HTMLFormElement | null
  35. const texts = Array.from(
  36. document.querySelectorAll('.js-survey input, .js-survey textarea')
  37. ) as Array<HTMLElement>
  38. const votes = Array.from(document.querySelectorAll('.js-survey [type=radio]'))
  39. if (!form || !texts.length || !votes.length) return
  40. form.addEventListener('submit', (evt) => {
  41. evt.preventDefault()
  42. submitForm(form)
  43. updateDisplay(form, 'end')
  44. })
  45. votes.forEach((voteEl) => {
  46. voteEl.addEventListener('change', (evt) => {
  47. const radio = evt.target as HTMLInputElement
  48. const state = radio.value.toLowerCase()
  49. submitForm(form)
  50. updateDisplay(form, state)
  51. })
  52. })
  53. // Prevent the site search from overtaking your input
  54. texts.forEach((text) => {
  55. text.addEventListener('keydown', (evt: KeyboardEvent) => {
  56. if (evt.code === 'Slash') evt.stopPropagation()
  57. })
  58. })
  59. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...