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

events.js 11 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
  1. const request = require('supertest')
  2. const nock = require('nock')
  3. const cheerio = require('cheerio')
  4. const app = require('../../lib/app')
  5. describe('POST /events', () => {
  6. jest.setTimeout(60 * 1000)
  7. let csrfToken = ''
  8. let agent
  9. beforeEach(async () => {
  10. process.env.AIRTABLE_API_KEY = '$AIRTABLE_API_KEY$'
  11. process.env.AIRTABLE_BASE_KEY = '$AIRTABLE_BASE_KEY$'
  12. process.env.HYDRO_SECRET = '$HYDRO_SECRET$'
  13. process.env.HYDRO_ENDPOINT = 'http://example.com/hydro'
  14. agent = request.agent(app)
  15. const csrfRes = await agent.get('/en')
  16. const $ = cheerio.load(csrfRes.text || '', { xmlMode: true })
  17. csrfToken = $('meta[name="csrf-token"]').attr('content')
  18. nock('http://example.com')
  19. .post('/hydro')
  20. .reply(200, {})
  21. })
  22. afterEach(() => {
  23. delete process.env.AIRTABLE_API_KEY
  24. delete process.env.AIRTABLE_BASE_KEY
  25. delete process.env.HYDRO_SECRET
  26. delete process.env.HYDRO_ENDPOINT
  27. csrfToken = ''
  28. })
  29. async function checkEvent (data, code) {
  30. return agent
  31. .post('/events')
  32. .send(data)
  33. .set('Accept', 'application/json')
  34. .set('csrf-token', csrfToken)
  35. .expect(code)
  36. }
  37. const baseExample = {
  38. context: {
  39. // Primitives
  40. event_id: 'a35d7f88-3f48-4f36-ad89-5e3c8ebc3df7',
  41. user: '703d32a8-ed0f-45f9-8d78-a913d4dc6f19',
  42. version: '1.0.0',
  43. created: '2020-10-02T17:12:18.620Z',
  44. // Content information
  45. path: '/github/docs/issues',
  46. hostname: 'github.com',
  47. referrer: 'https://github.com/github/docs',
  48. search: '?q=is%3Aissue+is%3Aopen+example+',
  49. href: 'https://github.com/github/docs/issues?q=is%3Aissue+is%3Aopen+example+',
  50. site_language: 'en',
  51. // Device information
  52. os: 'linux',
  53. os_version: '18.04',
  54. browser: 'chrome',
  55. browser_version: '85.0.4183.121',
  56. viewport_width: 1418,
  57. viewport_height: 501,
  58. // Location information
  59. timezone: -7,
  60. user_language: 'en-US'
  61. }
  62. }
  63. describe('page', () => {
  64. const pageExample = { ...baseExample, type: 'page' }
  65. it('should record a page event', () =>
  66. checkEvent(pageExample, 200)
  67. )
  68. it('should require a type', () =>
  69. checkEvent(baseExample, 400)
  70. )
  71. it('should require an event_id in uuid', () =>
  72. checkEvent({
  73. ...pageExample,
  74. context: {
  75. ...pageExample.context,
  76. event_id: 'asdfghjkl'
  77. }
  78. }, 400)
  79. )
  80. it('should require a user in uuid', () =>
  81. checkEvent({
  82. ...pageExample,
  83. context: {
  84. ...pageExample.context,
  85. user: 'asdfghjkl'
  86. }
  87. }, 400)
  88. )
  89. it('should require a version', () =>
  90. checkEvent({
  91. ...pageExample,
  92. context: {
  93. ...pageExample.context,
  94. version: undefined
  95. }
  96. }, 400)
  97. )
  98. it('should require created timestamp', () =>
  99. checkEvent({
  100. ...pageExample,
  101. context: {
  102. ...pageExample.context,
  103. timestamp: 1234
  104. }
  105. }, 400)
  106. )
  107. it('should allow page_event_id', () =>
  108. checkEvent({
  109. ...pageExample,
  110. context: {
  111. ...pageExample.context,
  112. page_event_id: baseExample.context.event_id
  113. }
  114. }, 200)
  115. )
  116. it('should not allow a honeypot token', () =>
  117. checkEvent({
  118. ...pageExample,
  119. context: {
  120. ...pageExample.context,
  121. token: 'zxcv'
  122. }
  123. }, 400)
  124. )
  125. it('should path be uri-reference', () =>
  126. checkEvent({
  127. ...pageExample,
  128. context: {
  129. ...pageExample.context,
  130. path: ' '
  131. }
  132. }, 400)
  133. )
  134. it('should hostname be uri-reference', () =>
  135. checkEvent({
  136. ...pageExample,
  137. context: {
  138. ...pageExample.context,
  139. hostname: ' '
  140. }
  141. }, 400)
  142. )
  143. it('should referrer be uri-reference', () =>
  144. checkEvent({
  145. ...pageExample,
  146. context: {
  147. ...pageExample.context,
  148. referrer: ' '
  149. }
  150. }, 400)
  151. )
  152. it('should search a string', () =>
  153. checkEvent({
  154. ...pageExample,
  155. context: {
  156. ...pageExample.context,
  157. search: 1234
  158. }
  159. }, 400)
  160. )
  161. it('should href be uri', () =>
  162. checkEvent({
  163. ...pageExample,
  164. context: {
  165. ...pageExample.context,
  166. href: '/example'
  167. }
  168. }, 400)
  169. )
  170. it('should site_language is a valid option', () =>
  171. checkEvent({
  172. ...pageExample,
  173. context: {
  174. ...pageExample.context,
  175. site_language: 'nl'
  176. }
  177. }, 400)
  178. )
  179. it('should os a valid os option', () =>
  180. checkEvent({
  181. ...pageExample,
  182. context: {
  183. ...pageExample.context,
  184. os: 'ubuntu'
  185. }
  186. }, 400)
  187. )
  188. it('should os_version a string', () =>
  189. checkEvent({
  190. ...pageExample,
  191. context: {
  192. ...pageExample.context,
  193. os_version: 25
  194. }
  195. }, 400)
  196. )
  197. it('should browser a valid option', () =>
  198. checkEvent({
  199. ...pageExample,
  200. context: {
  201. ...pageExample.context,
  202. browser: 'opera'
  203. }
  204. }, 400)
  205. )
  206. it('should browser_version a string', () =>
  207. checkEvent({
  208. ...pageExample,
  209. context: {
  210. ...pageExample.context,
  211. browser_version: 25
  212. }
  213. }, 400)
  214. )
  215. it('should viewport_width a number', () =>
  216. checkEvent({
  217. ...pageExample,
  218. context: {
  219. ...pageExample.context,
  220. viewport_width: -500
  221. }
  222. }, 400)
  223. )
  224. it('should viewport_height a number', () =>
  225. checkEvent({
  226. ...pageExample,
  227. context: {
  228. ...pageExample.context,
  229. viewport_height: '53px'
  230. }
  231. }, 400)
  232. )
  233. it('should timezone in number', () =>
  234. checkEvent({
  235. ...pageExample,
  236. context: {
  237. ...pageExample.context,
  238. timezone: 'GMT-0700'
  239. }
  240. }, 400)
  241. )
  242. it('should user_language is a string', () =>
  243. checkEvent({
  244. ...pageExample,
  245. context: {
  246. ...pageExample.context,
  247. user_language: true
  248. }
  249. }, 400)
  250. )
  251. })
  252. describe('exit', () => {
  253. const exitExample = {
  254. ...baseExample,
  255. type: 'exit',
  256. exit_render_duration: 0.9,
  257. exit_first_paint: 0.1,
  258. exit_dom_interactive: 0.2,
  259. exit_dom_complete: 0.3,
  260. exit_visit_duration: 5,
  261. exit_scroll_length: 0.5
  262. }
  263. it('should record an exit event', () =>
  264. checkEvent(exitExample, 200)
  265. )
  266. it('should exit_render_duration is a positive number', () =>
  267. checkEvent({
  268. ...exitExample,
  269. exit_render_duration: -0.5
  270. }, 400)
  271. )
  272. it('exit_first_paint is a number', () =>
  273. checkEvent({ ...exitExample, exit_first_paint: 'afjdkl' }, 400)
  274. )
  275. it('exit_dom_interactive is a number', () =>
  276. checkEvent({ ...exitExample, exit_dom_interactive: '202' }, 400)
  277. )
  278. it('exit_visit_duration is a number', () =>
  279. checkEvent({ ...exitExample, exit_visit_duration: '75' }, 400)
  280. )
  281. it('exit_scroll_length is a number between 0 and 1', () =>
  282. checkEvent({ ...exitExample, exit_scroll_length: 1.1 }, 400)
  283. )
  284. })
  285. describe('link', () => {
  286. const linkExample = {
  287. ...baseExample,
  288. type: 'link',
  289. link_url: 'https://example.com'
  290. }
  291. it('should send a link event', () =>
  292. checkEvent(linkExample, 200)
  293. )
  294. it('link_url is a required uri formatted string', () =>
  295. checkEvent({ ...linkExample, link_url: 'foo' }, 400)
  296. )
  297. })
  298. describe('search', () => {
  299. const searchExample = {
  300. ...baseExample,
  301. type: 'search',
  302. search_query: 'github private instances',
  303. search_context: 'private'
  304. }
  305. it('should record a search event', () =>
  306. checkEvent(searchExample, 200)
  307. )
  308. it('search_query is required string', () =>
  309. checkEvent({ ...searchExample, search_query: undefined }, 400)
  310. )
  311. it('search_context is optional string', () =>
  312. checkEvent({ ...searchExample, search_context: undefined }, 200)
  313. )
  314. })
  315. describe('navigate', () => {
  316. const navigateExample = {
  317. ...baseExample,
  318. type: 'navigate',
  319. navigate_label: 'drop down'
  320. }
  321. it('should record a navigate event', () =>
  322. checkEvent(navigateExample, 200)
  323. )
  324. it('navigate_label is optional string', () =>
  325. checkEvent({ ...navigateExample, navigate_label: undefined }, 200)
  326. )
  327. })
  328. describe('survey', () => {
  329. const surveyExample = {
  330. ...baseExample,
  331. type: 'survey',
  332. survey_vote: true,
  333. survey_comment: 'I love this site.',
  334. survey_email: 'daisy@example.com'
  335. }
  336. it('should record a survey event', () =>
  337. checkEvent(surveyExample, 200)
  338. )
  339. it('survey_vote is boolean', () =>
  340. checkEvent({ ...surveyExample, survey_vote: undefined }, 400)
  341. )
  342. it('survey_comment is string', () => {
  343. checkEvent({ ...surveyExample, survey_comment: 1234 }, 400)
  344. })
  345. it('survey_email is email', () => {
  346. checkEvent({ ...surveyExample, survey_email: 'daisy' }, 400)
  347. })
  348. })
  349. describe('experiment', () => {
  350. const experimentExample = {
  351. ...baseExample,
  352. type: 'experiment',
  353. experiment_name: 'change-button-copy',
  354. experiment_variation: 'treatment',
  355. experiment_success: true
  356. }
  357. it('should record an experiment event', () =>
  358. checkEvent(experimentExample, 200)
  359. )
  360. it('experiment_name is required string', () =>
  361. checkEvent({ ...experimentExample, experiment_name: undefined }, 400)
  362. )
  363. it('experiment_variation is required string', () =>
  364. checkEvent({ ...experimentExample, experiment_variation: undefined }, 400)
  365. )
  366. it('experiment_success is optional boolean', () =>
  367. checkEvent({ ...experimentExample, experiment_success: undefined }, 200)
  368. )
  369. })
  370. describe('redirect', () => {
  371. const redirectExample = {
  372. ...baseExample,
  373. type: 'redirect',
  374. redirect_from: 'http://example.com/a',
  375. redirect_to: 'http://example.com/b'
  376. }
  377. it('should record an redirect event', () =>
  378. checkEvent(redirectExample, 200)
  379. )
  380. it('redirect_from is required url', () =>
  381. checkEvent({ ...redirectExample, redirect_from: ' ' }, 400)
  382. )
  383. it('redirect_to is required url', () =>
  384. checkEvent({ ...redirectExample, redirect_to: undefined }, 400)
  385. )
  386. })
  387. describe('clipboard', () => {
  388. const clipboardExample = {
  389. ...baseExample,
  390. type: 'clipboard',
  391. clipboard_operation: 'copy'
  392. }
  393. it('should record an clipboard event', () =>
  394. checkEvent(clipboardExample, 200)
  395. )
  396. it('clipboard_operation is required copy, paste, cut', () =>
  397. checkEvent({ ...clipboardExample, clipboard_operation: 'destroy' }, 400)
  398. )
  399. })
  400. describe('print', () => {
  401. const printExample = {
  402. ...baseExample,
  403. type: 'print'
  404. }
  405. it('should record a print event', () =>
  406. checkEvent(printExample, 200)
  407. )
  408. })
  409. })
Tip!

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

Comments

Loading...