forked from segmentio/segment-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
819 lines (819 loc) · 23 KB
/
index.js
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
* This client was automatically generated by Segment Typewriter. ** Do Not Edit **
*/
/**
* Ajv is a peer dependency for development builds. It's used to apply run-time validation
* to message payloads before passing them on to the underlying analytics instance.
*
* Note that the production bundle does not depend on Ajv.
*
* You can install it with: `npm install --save-dev ajv`.
*/
import Ajv from 'ajv'
/**
* The default handler that is fired if none is supplied with setTypewriterOptions.
* This handler will log a warning message to the console.
*/
export const defaultValidationErrorHandler = (message, violations) => {
const msg = JSON.stringify(
{
type: 'Typewriter JSON Schema Validation Error',
description:
`You made an analytics call (${message.event}) using Typewriter that doesn't match the ` +
'Tracking Plan spec.',
errors: violations,
},
undefined,
2
)
console.warn(msg)
}
let onViolation = defaultValidationErrorHandler
let analytics = () => {
return window.analytics
}
/**
* Updates the run-time configuration of this Typewriter client.
*
* @param {TypewriterOptions} options - the options to upsert
*
* @typedef {Object} TypewriterOptions
* @property {Segment.AnalyticsJS} [analytics] - Underlying analytics instance where analytics
* calls are forwarded on to. Defaults to window.analytics.
* @property {Function} [onViolation] - Handler fired when if an event does not match its spec. This handler does not fire in
* production mode, because it requires inlining the full JSON Schema spec for each event in your Tracking Plan. By default,
* it will throw errors if NODE_ENV="test" so that tests will fail if a message does not match the spec. Otherwise, errors
* will be logged to stderr.
*/
export function setTypewriterOptions(options) {
analytics = options.analytics
? () => options.analytics || window.analytics
: analytics
onViolation = options.onViolation || onViolation
}
/**
* Validates a message against a JSON Schema using Ajv. If the message
* is invalid, the `onViolation` handler will be called.
*/
function validateAgainstSchema(message, schema) {
const ajv = new Ajv({ schemaId: 'auto', allErrors: true, verbose: true })
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'))
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'))
if (!ajv.validate(schema, message) && ajv.errors) {
onViolation(message, ajv.errors)
}
}
/**
* Helper to attach metadata on Typewriter to outbound requests.
* This is used for attribution and debugging by the Segment team.
*/
function withTypewriterContext(message = {}) {
return {
...message,
context: {
...(message.context || {}),
typewriter: {
language: 'javascript',
version: '7.0.1',
},
},
}
}
/**
* @typedef DocsSearched
* @property {string} query -
*/
/**
* @typedef FeedbackCommentProvided
* @property {string} [comment] - the comment
* @property {boolean} [helpful] - the rating given prior to the comment
* @property {string} [section] - Was the feedback form in the right-nav or footer clicked?
* @property {string} [title] -
*/
/**
* @typedef FeedbackProvided
* @property {string} [comment] -
* @property {boolean} helpful - Boolean representing the value of the feedback, true is helpful, false is not helpful
* @property {string} section - Was the feedback form in the right-nav or footer clicked?
* @property {string} title -
*/
/**
* @typedef LeadCaptured
* @property {string} email -
* @property {string} location -
* @property {string} url -
*/
/**
* @typedef NavigationControlUsed
* @property {string} control_value - Name of control used
* @property {string} [search_value] - Value of search term if search bar is used
*/
/**
* @typedef PageViewed
* @property {string} [browser_language] - Custom property to identify user's browser language
* @property {string} [frontmatter] - Custom property to add additional frontmatter context to each page call
* @property {string} [ip] -
* @property {string} [name] -
* @property {string} [path] -
* @property {string} [referrer] -
* @property {string} [search] -
* @property {string} [timestamp] -
* @property {string} [timezone] -
* @property {string} [title] -
* @property {string} [url] -
*/
/**
* @typedef ScrolledToBottom
* @property {string} url -
*/
/**
* @typedef TocClicked
* @property {string} link - link clicked
* @property {string} name - name of the link clicked
* @property {string} url - The url of the page (hostname + path)
*/
/**
* Fires a 'Docs Searched' track call.
*
* @param {DocsSearched} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function docsSearched(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
labels: {},
properties: {
context: {},
properties: {
properties: {
query: {
description: '',
type: 'string',
},
},
required: ['query'],
type: 'object',
},
traits: {
type: 'object',
},
},
required: ['properties'],
title: 'Docs Searched',
type: 'object',
}
const message = {
event: 'Docs Searched',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Docs Searched',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* User submits comments after their thumbs/down rating
*
* @param {FeedbackCommentProvided} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function feedbackCommentProvided(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
description: 'User submits comments after their thumbs/down rating',
labels: {},
properties: {
context: {},
properties: {
properties: {
comment: {
description: 'the comment',
type: 'string',
},
helpful: {
description: 'the rating given prior to the comment',
type: 'boolean',
},
section: {
description:
'Was the feedback form in the right-nav or footer clicked?',
type: 'string',
},
title: {
description: '',
type: 'string',
},
},
type: 'object',
},
traits: {
type: 'object',
},
},
title: 'Feedback Comment Provided',
type: 'object',
}
const message = {
event: 'Feedback Comment Provided',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Feedback Comment Provided',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* User submits a thumbs up/down rating for a docs article
*
* @param {FeedbackProvided} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function feedbackProvided(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
description: 'User submits a thumbs up/down rating for a docs article',
labels: {},
properties: {
context: {},
properties: {
properties: {
comment: {
description: '',
type: 'string',
},
helpful: {
description:
'Boolean representing the value of the feedback, true is helpful, false is not helpful',
type: 'boolean',
},
section: {
description:
'Was the feedback form in the right-nav or footer clicked?',
pattern: 'right-nav|footer',
type: 'string',
},
title: {
description: '',
type: 'string',
},
},
required: ['helpful', 'section', 'title'],
type: 'object',
},
traits: {
type: 'object',
},
},
required: ['properties'],
title: 'Feedback Provided',
type: 'object',
}
const message = {
event: 'Feedback Provided',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Feedback Provided',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* Fires a 'Home Button Clicked' track call.
*
* @param {Record<string, any>} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function homeButtonClicked(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
labels: {},
properties: {
context: {},
properties: {
type: 'object',
},
traits: {
type: 'object',
},
},
title: 'Home Button Clicked',
type: 'object',
}
const message = {
event: 'Home Button Clicked',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Home Button Clicked',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* Fires a 'Lead Captured' track call.
*
* @param {LeadCaptured} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function leadCaptured(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
labels: {},
properties: {
context: {},
properties: {
properties: {
email: {
description: '',
type: 'string',
},
location: {
description: '',
type: 'string',
},
url: {
description: '',
type: 'string',
},
},
required: ['email', 'location', 'url'],
type: 'object',
},
traits: {
type: 'object',
},
},
required: ['properties'],
title: 'Lead Captured',
type: 'object',
}
const message = {
event: 'Lead Captured',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Lead Captured',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* User clicks one of the navigation elements like the home button, ToC, or searches
*
* @param {NavigationControlUsed} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function navigationControlUsed(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
description:
'User clicks one of the navigation elements like the home button, ToC, or searches',
labels: {},
properties: {
context: {},
properties: {
properties: {
control_value: {
description: 'Name of control used',
type: 'string',
},
search_value: {
description: 'Value of search term if search bar is used',
type: 'string',
},
},
required: ['control_value'],
type: 'object',
},
traits: {
type: 'object',
},
},
required: ['properties'],
title: 'Navigation Control Used',
type: 'object',
}
const message = {
event: 'Navigation Control Used',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Navigation Control Used',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* Fires a 'Page Viewed' track call.
*
* @param {PageViewed} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function pageViewed(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
labels: {},
properties: {
context: {},
properties: {
properties: {
browser_language: {
description: "Custom property to identify user's browser language",
type: 'string',
},
frontmatter: {
description:
'Custom property to add additional frontmatter context to each page call',
type: 'string',
},
ip: {
description: '',
type: 'string',
},
name: {
description: '',
type: 'string',
},
path: {
description: '',
type: 'string',
},
referrer: {
description: '',
type: 'string',
},
search: {
description: '',
type: 'string',
},
timestamp: {
description: '',
type: 'string',
},
timezone: {
description: '',
type: 'string',
},
title: {
description: '',
type: 'string',
},
url: {
description: '',
type: 'string',
},
},
type: 'object',
},
traits: {
type: 'object',
},
},
title: 'Page Viewed',
type: 'object',
}
const message = {
event: 'Page Viewed',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Page Viewed',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* Fires a 'Scroll to Top Clicked' track call.
*
* @param {Record<string, any>} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function scrollToTopClicked(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
labels: {},
properties: {
context: {},
properties: {
type: 'object',
},
traits: {
type: 'object',
},
},
title: 'Scroll to Top Clicked',
type: 'object',
}
const message = {
event: 'Scroll to Top Clicked',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Scroll to Top Clicked',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* User scrolled to the bottom of the page
*
* @param {ScrolledToBottom} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function scrolledToBottom(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
description: 'User scrolled to the bottom of the page',
labels: {},
properties: {
context: {},
properties: {
properties: {
url: {
description: '',
type: 'string',
},
},
required: ['url'],
type: 'object',
},
traits: {
type: 'object',
},
},
required: ['properties'],
title: 'Scrolled To Bottom',
type: 'object',
}
const message = {
event: 'Scrolled To Bottom',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'Scrolled To Bottom',
props || {},
withTypewriterContext(options),
callback
)
}
}
/**
* Table of Contents Clicked
*
* @param {TocClicked} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
export function tocClicked(props, options, callback) {
const schema = {
$schema: 'http://json-schema.org/draft-07/schema#',
description: 'Table of Contents Clicked',
labels: {},
properties: {
context: {},
properties: {
properties: {
link: {
description: 'link clicked',
type: 'string',
},
name: {
description: 'name of the link clicked',
type: 'string',
},
url: {
description: 'The url of the page (hostname + path)',
type: 'string',
},
},
required: ['link', 'name', 'url'],
type: 'object',
},
traits: {
type: 'object',
},
},
required: ['properties'],
title: 'TOC Clicked',
type: 'object',
}
const message = {
event: 'TOC Clicked',
properties: props || {},
options,
}
validateAgainstSchema(message, schema)
const a = analytics()
if (a) {
a.track(
'TOC Clicked',
props || {},
withTypewriterContext(options),
callback
)
}
}
const clientAPI = {
/**
* Updates the run-time configuration of this Typewriter client.
*
* @param {TypewriterOptions} options - the options to upsert
*
* @typedef {Object} TypewriterOptions
* @property {Segment.AnalyticsJS} [analytics] - Underlying analytics instance where analytics
* calls are forwarded on to. Defaults to window.analytics.
* @property {Function} [onViolation] - Handler fired when if an event does not match its spec. This handler does not fire in
* production mode, because it requires inlining the full JSON Schema spec for each event in your Tracking Plan. By default,
* it will throw errors if NODE_ENV="test" so that tests will fail if a message does not match the spec. Otherwise, errors
* will be logged to stderr.
*/
setTypewriterOptions,
/**
* Fires a 'Docs Searched' track call.
*
* @param {DocsSearched} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
docsSearched,
/**
* User submits comments after their thumbs/down rating
*
* @param {FeedbackCommentProvided} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
feedbackCommentProvided,
/**
* User submits a thumbs up/down rating for a docs article
*
* @param {FeedbackProvided} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
feedbackProvided,
/**
* Fires a 'Home Button Clicked' track call.
*
* @param {Record<string, any>} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
homeButtonClicked,
/**
* Fires a 'Lead Captured' track call.
*
* @param {LeadCaptured} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
leadCaptured,
/**
* User clicks one of the navigation elements like the home button, ToC, or searches
*
* @param {NavigationControlUsed} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
navigationControlUsed,
/**
* Fires a 'Page Viewed' track call.
*
* @param {PageViewed} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
pageViewed,
/**
* Fires a 'Scroll to Top Clicked' track call.
*
* @param {Record<string, any>} [props] - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
scrollToTopClicked,
/**
* User scrolled to the bottom of the page
*
* @param {ScrolledToBottom} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
scrolledToBottom,
/**
* Table of Contents Clicked
*
* @param {TocClicked} props - The analytics properties that will be sent to Segment.
* @param {Object} [options] - A dictionary of options. For example, enable or disable specific destinations for the call.
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
* call is fired.
*/
tocClicked,
}
export default new Proxy(clientAPI, {
get(target, method) {
if (typeof method === 'string' && target.hasOwnProperty(method)) {
return target[method]
}
return () => {
console.warn(`⚠️ You made an analytics call (${String(
method
)}) that can't be found. Either:
a) Re-generate your typewriter client: \`npx typewriter\`
b) Add it to your Tracking Plan: https://app.segment.com/segment_prod/protocols/tracking-plans/rs_1Ohr9MJskSjbjKIZJ8ixf5dIAJ1`)
const a = analytics()
if (a) {
a.track(
'Unknown Analytics Call Fired',
{
method,
},
withTypewriterContext()
)
}
}
},
})