/
index.ts
823 lines (752 loc) · 20.2 KB
/
index.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
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
820
821
822
823
/* tslint:disable */
/* eslint-disable */
/**
* Ampli - A strong typed wrapper for your Analytics
*
* This file is generated by Amplitude.
* To update run 'ampli pull browser-ts-ampli-v2'
*
* Required dependencies: @amplitude/analytics-browser@^1.3.0
* Tracking Plan Version: 1
* Build: 1.0.0
* Runtime: browser:typescript-ampli-v2
*
* [View Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest)
*
* [Full Setup Instructions](https://data.amplitude.com/test-codegen/Test%20Codegen/implementation/browser-ts-ampli-v2)
*/
import * as amplitude from '@amplitude/analytics-browser';
export type Environment = 'prod' | 'dev';
export const ApiKey: Record<Environment, string> = {
prod: '',
dev: ''
};
/**
* Default Amplitude configuration options. Contains tracking plan information.
*/
export const DefaultConfiguration: BrowserOptions = {
plan: {
version: '1',
branch: 'main',
source: 'browser-ts-ampli-v2',
versionId: 'a61c3908-ca4d-4c8d-8f81-54ad3ba17b9c'
},
...{
ingestionMetadata: {
sourceName: 'browser-typescript-ampli',
sourceVersion: '2.0.0'
}
}
};
export interface LoadOptionsBase { disabled?: boolean }
export type LoadOptionsWithEnvironment = LoadOptionsBase & { environment: Environment; client?: { configuration?: BrowserOptions; }; };
export type LoadOptionsWithApiKey = LoadOptionsBase & { client: { apiKey: string; configuration?: BrowserOptions; } };
export type LoadOptionsWithClientInstance = LoadOptionsBase & { client: { instance: BrowserClient; } };
export type LoadOptions = LoadOptionsWithEnvironment | LoadOptionsWithApiKey | LoadOptionsWithClientInstance;
export interface IdentifyProperties {
/**
* Description for identify optionalArray
*
* | Rule | Value |
* |---|---|
* | Unique Items | false |
* | Item Type | string |
*/
optionalArray?: string[];
/**
* Description for identify requiredNumber
*
* | Rule | Value |
* |---|---|
* | Type | number |
*/
requiredNumber: number;
}
export interface EventObjectTypesProperties {
/**
* Property Object Type
*/
requiredObject: any;
/**
* Property Object Array Type
*/
requiredObjectArray: any[];
}
export interface EventWithAllPropertiesProperties {
/**
* Event 2 Property - Optional String * * Examples: * Some string, or another
*/
optionalString?: string;
/**
* Event 2 Property - Array
*
* | Rule | Value |
* |---|---|
* | Min Items | 0 |
* | Item Type | string |
*
* @minItems 0
*/
requiredArray: string[];
/**
* Event 2 Property - Boolean
*/
requiredBoolean: boolean;
/**
* Event 2 Property - Enum
*
* | Rule | Value |
* |---|---|
* | Enum Values | Enum1, Enum2 |
*/
requiredEnum: "Enum1" | "Enum2";
/**
* Event 2 Property - Integer * * Examples: * 5, 4, 3
*
* | Rule | Value |
* |---|---|
* | Type | integer |
*/
requiredInteger: number;
/**
* Event 2 Property - Number
*
* | Rule | Value |
* |---|---|
* | Type | number |
*/
requiredNumber: number;
/**
* Event 2 Property - String
*/
requiredString: string;
}
export interface EventWithArrayTypesProperties {
/**
* description for required boolean array
*
* | Rule | Value |
* |---|---|
* | Item Type | boolean |
*/
requiredBooleanArray: boolean[];
/**
* Description for enum array property
*
* | Rule | Value |
* |---|---|
* | Item Type | string |
*/
requiredEnumArray: ("enum1" | "enum2")[];
/**
* Description for required number array
*
* | Rule | Value |
* |---|---|
* | Item Type | number |
*/
requiredNumberArray: number[];
/**
* Description for required object array
*/
requiredObjectArray: any[];
/**
* description for required string array
*
* | Rule | Value |
* |---|---|
* | Item Type | string |
*/
requiredStringArray: string[];
}
export interface EventWithEnumTypesProperties {
/**
* Description for required enum
*
* | Rule | Value |
* |---|---|
* | Enum Values | optional enum 1, optional enum 2 |
*/
"optional enum"?: "optional enum 1" | "optional enum 2";
/**
* Description for optional enum
*
* | Rule | Value |
* |---|---|
* | Enum Values | required enum 1, required enum 2 |
*/
"required enum": "required enum 1" | "required enum 2";
}
export interface EventWithOptionalArrayTypesProperties {
/**
* Description for optional boolean array
*
* | Rule | Value |
* |---|---|
* | Item Type | boolean |
*/
optionalBooleanArray?: boolean[];
/**
* Description for optional enum array
*
* | Rule | Value |
* |---|---|
* | Item Type | string |
*/
optionalEnumArray?: ("enum1" | "enum2")[];
/**
* Description for optional object array
*/
optionalJSONArray?: any[];
/**
* Description for optional number array
*
* | Rule | Value |
* |---|---|
* | Item Type | number |
*/
optionalNumberArray?: number[];
/**
* Description for optional string array
*
* | Rule | Value |
* |---|---|
* | Item Type | string |
*/
optionalStringArray?: string[];
}
export interface EventWithOptionalPropertiesProperties {
/**
* | Rule | Value |
* |---|---|
* | Item Type | number |
*/
optionalArrayNumber?: number[];
/**
* | Rule | Value |
* |---|---|
* | Item Type | string |
*/
optionalArrayString?: string[];
optionalBoolean?: boolean;
/**
* | Rule | Value |
* |---|---|
* | Type | number |
*/
optionalNumber?: number;
/**
* Optional String property description
*/
optionalString?: string;
}
export interface EventWithTemplatePropertiesProperties {
/**
* optional_event_property description
*
* | Rule | Value |
* |---|---|
* | Type | number |
*/
optional_event_property?: number;
/**
* optional_template_property description
*
* | Rule | Value |
* |---|---|
* | Type | number |
*/
optional_template_property?: number;
/**
* required_event_property description
*/
required_event_property: string;
/**
* required_template_property description
*/
required_template_property: string;
}
export interface EventWithDifferentCasingTypesProperties {
/**
* Description for enum with space
*
* | Rule | Value |
* |---|---|
* | Enum Values | enum with space |
*/
"enum with space": "enum with space";
/**
* description_for_enum_snake_case
*
* | Rule | Value |
* |---|---|
* | Enum Values | enum_snake_case |
*/
enum_snake_case: "enum_snake_case";
/**
* descriptionForEnumCamelCase
*
* | Rule | Value |
* |---|---|
* | Enum Values | enumCamelCase |
*/
enumCamelCase: "enumCamelCase";
/**
* DescirptionForEnumPascalCase
*
* | Rule | Value |
* |---|---|
* | Enum Values | EnumPascalCase |
*/
EnumPascalCase: "EnumPascalCase";
/**
* Description for case with space
*/
"property with space": string;
/**
* Description_for_snake_case
*/
property_with_snake_case: string;
/**
* descriptionForCamelCase
*/
propertyWithCamelCase: string;
/**
* DescriptionForPascalCase
*/
PropertyWithPascalCase: string;
}
export interface EventMaxIntForTestProperties {
/**
* property to test schema validation
*
* | Rule | Value |
* |---|---|
* | Type | integer |
* | Max Value | 10 |
*/
intMax10: number;
}
export interface EventTemplateProperties {
/**
* optional_template_property description
*
* | Rule | Value |
* |---|---|
* | Type | number |
*/
optional_template_property?: number;
/**
* required_template_property description
*/
required_template_property: string;
}
export interface SourceTemplateProperties {
/**
* description for context optionalEnum
*
* | Rule | Value |
* |---|---|
* | Enum Values | Value 1, Value 2 |
*/
optionalEnum?: "Value 1" | "Value 2";
/**
* description for context requiredString
*/
requiredString: string;
}
export class Identify implements BaseEvent {
event_type = amplitude.Types.SpecialEventType.IDENTIFY;
constructor(
public event_properties: IdentifyProperties,
) {
this.event_properties = event_properties;
}
}
export class EventNoProperties implements BaseEvent {
event_type = 'Event No Properties';
}
export class EventObjectTypes implements BaseEvent {
event_type = 'Event Object Types';
constructor(
public event_properties: EventObjectTypesProperties,
) {
this.event_properties = event_properties;
}
}
export class EventWithAllProperties implements BaseEvent {
event_type = 'Event With All Properties';
event_properties: EventWithAllPropertiesProperties & {
'requiredConst': "some-const-value";
};
constructor(
event_properties: EventWithAllPropertiesProperties,
) {
this.event_properties = {
...event_properties,
'requiredConst': "some-const-value",
};
}
}
export class EventWithArrayTypes implements BaseEvent {
event_type = 'Event With Array Types';
constructor(
public event_properties: EventWithArrayTypesProperties,
) {
this.event_properties = event_properties;
}
}
export class EventWithConstTypes implements BaseEvent {
event_type = 'Event With Const Types';
event_properties = {
'Boolean Const': true,
'Integer Const': 10,
'Number Const': 2.2,
'String Const': "String-Constant",
'String Const WIth Quotes': "\"String \"Const With\" Quotes\"",
'String Int Const': 0,
};
}
export class EventWithEnumTypes implements BaseEvent {
event_type = 'Event With Enum Types';
constructor(
public event_properties: EventWithEnumTypesProperties,
) {
this.event_properties = event_properties;
}
}
export class EventWithOptionalArrayTypes implements BaseEvent {
event_type = 'Event With Optional Array Types';
constructor(
public event_properties?: EventWithOptionalArrayTypesProperties,
) {
this.event_properties = event_properties;
}
}
export class EventWithOptionalProperties implements BaseEvent {
event_type = 'Event With Optional Properties';
constructor(
public event_properties?: EventWithOptionalPropertiesProperties,
) {
this.event_properties = event_properties;
}
}
export class EventWithTemplateProperties implements BaseEvent {
event_type = 'Event With Template Properties';
constructor(
public event_properties: EventWithTemplatePropertiesProperties,
) {
this.event_properties = event_properties;
}
}
export class EventWithDifferentCasingTypes implements BaseEvent {
event_type = 'event withDifferent_CasingTypes';
constructor(
public event_properties: EventWithDifferentCasingTypesProperties,
) {
this.event_properties = event_properties;
}
}
export class EventMaxIntForTest implements BaseEvent {
event_type = 'EventMaxIntForTest';
constructor(
public event_properties: EventMaxIntForTestProperties,
) {
this.event_properties = event_properties;
}
}
export type PromiseResult<T> = { promise: Promise<T | void> };
const getVoidPromiseResult = () => ({ promise: Promise.resolve() });
// prettier-ignore
export class Ampli {
private disabled: boolean = false;
private amplitude?: BrowserClient;
get client(): BrowserClient {
this.isInitializedAndEnabled();
return this.amplitude!;
}
get isLoaded(): boolean {
return this.amplitude != null;
}
private isInitializedAndEnabled(): boolean {
if (!this.amplitude) {
console.error('ERROR: Ampli is not yet initialized. Have you called ampli.load() on app start?');
return false;
}
return !this.disabled;
}
/**
* Initialize the Ampli SDK. Call once when your application starts.
*
* @param options Configuration options to initialize the Ampli SDK with.
*/
load(options: LoadOptions): PromiseResult<void> {
this.disabled = options.disabled ?? false;
if (this.amplitude) {
console.warn('WARNING: Ampli is already intialized. Ampli.load() should be called once at application startup.');
return getVoidPromiseResult();
}
let apiKey: string | null = null;
if (options.client && 'apiKey' in options.client) {
apiKey = options.client.apiKey;
} else if ('environment' in options) {
apiKey = ApiKey[options.environment];
}
if (options.client && 'instance' in options.client) {
this.amplitude = options.client.instance;
} else if (apiKey) {
this.amplitude = amplitude.createInstance();
const configuration = (options.client && 'configuration' in options.client) ? options.client.configuration : {};
return this.amplitude.init(apiKey, undefined, { ...DefaultConfiguration, ...configuration });
} else {
console.error("ERROR: ampli.load() requires 'environment', 'client.apiKey', or 'client.instance'");
}
return getVoidPromiseResult();
}
/**
* Identify a user and set user properties.
*
* @param userId The user's id.
* @param properties The user properties.
* @param options Optional event options.
*/
identify(
userId: string | undefined,
properties: IdentifyProperties,
options?: EventOptions,
): PromiseResult<Result> {
if (!this.isInitializedAndEnabled()) {
return getVoidPromiseResult();
}
if (userId) {
options = {...options, user_id: userId};
}
const amplitudeIdentify = new amplitude.Identify();
const eventProperties = properties;
if (eventProperties != null) {
for (const [key, value] of Object.entries(eventProperties)) {
amplitudeIdentify.set(key, value);
}
}
return this.amplitude!.identify(
amplitudeIdentify,
options,
);
}
/**
* Track event
*
* @param event The event to track.
* @param options Optional event options.
*/
track(event: Event, options?: EventOptions): PromiseResult<Result> {
if (!this.isInitializedAndEnabled()) {
return getVoidPromiseResult();
}
return this.amplitude!.track(event, undefined, options);
}
/**
* Event No Properties
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20No%20Properties)
*
* Event w no properties description
*
* Owner: Test codegen
*
* @param options Amplitude event options.
*/
eventNoProperties(
options?: EventOptions,
) {
return this.track(new EventNoProperties(), options);
}
/**
* Event Object Types
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20Object%20Types)
*
* Event with Object and Object Array
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. requiredObject)
* @param options Amplitude event options.
*/
eventObjectTypes(
properties: EventObjectTypesProperties,
options?: EventOptions,
) {
return this.track(new EventObjectTypes(properties), options);
}
/**
* Event With All Properties
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20With%20All%20Properties)
*
* Event w all properties description
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. optionalString)
* @param options Amplitude event options.
*/
eventWithAllProperties(
properties: EventWithAllPropertiesProperties,
options?: EventOptions,
) {
return this.track(new EventWithAllProperties(properties), options);
}
/**
* Event With Array Types
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20With%20Array%20Types)
*
* Description for event with Array Types
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. requiredBooleanArray)
* @param options Amplitude event options.
*/
eventWithArrayTypes(
properties: EventWithArrayTypesProperties,
options?: EventOptions,
) {
return this.track(new EventWithArrayTypes(properties), options);
}
/**
* Event With Const Types
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20With%20Const%20Types)
*
* Description for event with const types
*
* Owner: Test codegen
*
* @param options Amplitude event options.
*/
eventWithConstTypes(
options?: EventOptions,
) {
return this.track(new EventWithConstTypes(), options);
}
/**
* Event With Enum Types
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20With%20Enum%20Types)
*
* Description for event with enum types
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. optional enum)
* @param options Amplitude event options.
*/
eventWithEnumTypes(
properties: EventWithEnumTypesProperties,
options?: EventOptions,
) {
return this.track(new EventWithEnumTypes(properties), options);
}
/**
* Event With Optional Array Types
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20With%20Optional%20Array%20Types)
*
* Description for event with optional array types
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. optionalBooleanArray)
* @param options Amplitude event options.
*/
eventWithOptionalArrayTypes(
properties?: EventWithOptionalArrayTypesProperties,
options?: EventOptions,
) {
return this.track(new EventWithOptionalArrayTypes(properties), options);
}
/**
* Event With Optional Properties
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20With%20Optional%20Properties)
*
* Event w optional properties description
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. optionalArrayNumber)
* @param options Amplitude event options.
*/
eventWithOptionalProperties(
properties?: EventWithOptionalPropertiesProperties,
options?: EventOptions,
) {
return this.track(new EventWithOptionalProperties(properties), options);
}
/**
* Event With Template Properties
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/Event%20With%20Template%20Properties)
*
* Event with template properties description
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. optional_event_property)
* @param options Amplitude event options.
*/
eventWithTemplateProperties(
properties: EventWithTemplatePropertiesProperties,
options?: EventOptions,
) {
return this.track(new EventWithTemplateProperties(properties), options);
}
/**
* event withDifferent_CasingTypes
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/event%20withDifferent_CasingTypes)
*
* Description for case with space
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. enum with space)
* @param options Amplitude event options.
*/
eventWithDifferentCasingTypes(
properties: EventWithDifferentCasingTypesProperties,
options?: EventOptions,
) {
return this.track(new EventWithDifferentCasingTypes(properties), options);
}
/**
* EventMaxIntForTest
*
* [View in Tracking Plan](https://data.amplitude.com/test-codegen/Test%20Codegen/events/main/latest/EventMaxIntForTest)
*
* Event to test schema validation
*
* Owner: Test codegen
*
* @param properties The event's properties (e.g. intMax10)
* @param options Amplitude event options.
*/
eventMaxIntForTest(
properties: EventMaxIntForTestProperties,
options?: EventOptions,
) {
return this.track(new EventMaxIntForTest(properties), options);
}
}
export const ampli = new Ampli();
// BASE TYPES
type BrowserOptions = amplitude.Types.BrowserOptions;
export type BrowserClient = amplitude.Types.BrowserClient;
export type BaseEvent = amplitude.Types.BaseEvent;
export type IdentifyEvent = amplitude.Types.IdentifyEvent;
export type GroupEvent = amplitude.Types.GroupIdentifyEvent;
export type Event = amplitude.Types.Event;
export type EventOptions = amplitude.Types.EventOptions;
export type Result = amplitude.Types.Result;