@codeleap/analytics 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@codeleap/analytics",
3
+ "version": "4.3.0",
4
+ "main": "src/index.ts",
5
+ "license": "UNLICENSED",
6
+ "repository": {
7
+ "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
8
+ "type": "git",
9
+ "directory": "packages/analytics"
10
+ },
11
+ "devDependencies": {
12
+ "@codeleap/config": "4.3.0",
13
+ "ts-node-dev": "1.1.8"
14
+ },
15
+ "scripts": {
16
+ "build": "echo 'No build needed'"
17
+ },
18
+ "peerDependencies": {
19
+ "typescript": "5.0.4"
20
+ }
21
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@codeleap/analytics",
3
+ "version": "4.3.0",
4
+ "main": "src/index.ts",
5
+ "license": "UNLICENSED",
6
+ "repository": {
7
+ "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
8
+ "type": "git",
9
+ "directory": "packages/analytics"
10
+ },
11
+ "devDependencies": {
12
+ "@codeleap/config": "workspace:*",
13
+ "ts-node-dev": "1.1.8"
14
+ },
15
+ "scripts": {
16
+ "build": "echo 'No build needed'"
17
+ },
18
+ "peerDependencies": {
19
+ "typescript": "5.0.4"
20
+ }
21
+ }
@@ -0,0 +1,101 @@
1
+ import { SegmentGeneralConfig, SegmentUser, DefaultMode, SegmentMethods, SegmentConfig, AnyRecord } from './types'
2
+
3
+ export * from './types'
4
+
5
+ const defaultConfig: SegmentGeneralConfig<any, SegmentUser, DefaultMode> = {
6
+ getUserTraits: (user) => user,
7
+ mode: 'prod',
8
+ prefix: {
9
+ 'dev': 'dev',
10
+ 'prod': null,
11
+ }
12
+ }
13
+
14
+ export class Segment<C extends SegmentMethods, U extends SegmentUser = SegmentUser, M extends string = DefaultMode> {
15
+ private config: SegmentGeneralConfig<C, U, M> = defaultConfig as unknown as SegmentGeneralConfig<C, U, M>
16
+
17
+ private _enabled = true
18
+
19
+ private apiKey: string | null = null
20
+
21
+ public client: C | null = null
22
+
23
+ public identifiedUser = false
24
+
25
+ public get enabled() {
26
+ return this.client !== null && this._enabled
27
+ }
28
+
29
+ private get eventPrefix() {
30
+ const modePrefix = this.config.prefix?.[this.config.mode] ?? null
31
+ return !modePrefix ? '' : `${modePrefix}_`
32
+ }
33
+
34
+ constructor(config: SegmentConfig<C, U, M>) {
35
+ const {
36
+ apiKey,
37
+ enabled = true,
38
+ createClient,
39
+ ...generalConfig
40
+ } = config
41
+
42
+ this.config = {
43
+ ...this.config,
44
+ ...generalConfig,
45
+ }
46
+
47
+ this.apiKey = apiKey
48
+ this._enabled = enabled
49
+
50
+ if (enabled) {
51
+ this.client = createClient(this.apiKey)
52
+ }
53
+ }
54
+
55
+ public async track<T extends AnyRecord>(eventName: string, properties: T | undefined = undefined) {
56
+ if (!this.enabled) return
57
+
58
+ await this.client?.track?.(`${this.eventPrefix}${eventName}`, properties)
59
+ }
60
+
61
+ public async screen(name: string, options: AnyRecord | undefined = undefined) {
62
+ if (!this.enabled) return
63
+
64
+ await this.client?.screen?.(`${this.eventPrefix}${name}`, options)
65
+ }
66
+
67
+ public async group(groupId: string, groupTraits: AnyRecord | undefined = undefined) {
68
+ if (!this.enabled) return
69
+
70
+ await this.client?.group(`${this.eventPrefix}${groupId}`, groupTraits)
71
+ }
72
+
73
+ public async alias(newUserId: string) {
74
+ if (!this.enabled) return
75
+
76
+ await this.client?.alias(newUserId)
77
+ }
78
+
79
+ public async reset() {
80
+ if (!this.enabled) return
81
+
82
+ await this.client?.reset()
83
+ }
84
+
85
+ public async identifyUser(user: U) {
86
+ if (!this.enabled || this.identifiedUser) return
87
+
88
+ const userTraits = this.config.getUserTraits(user)
89
+
90
+ await this.client?.identify?.(user?.id, userTraits)
91
+
92
+ this.identifiedUser = true
93
+ }
94
+
95
+ public setMode(newMode: M) {
96
+ this.config = {
97
+ ...this.config,
98
+ mode: newMode,
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,32 @@
1
+ export type AnyRecord = {
2
+ [x:string]: any
3
+ }
4
+
5
+ export type SegmentConfig<C, U extends SegmentUser, M extends string = DefaultMode> = {
6
+ apiKey: string
7
+ enabled?: boolean
8
+ getUserTraits: (user: U) => AnyRecord
9
+ mode: M
10
+ prefix?: Record<M, string | null>
11
+ createClient: (writeKey: string) => C
12
+ }
13
+
14
+ export type SegmentMethods = {
15
+ track: (eventName: string, properties?: AnyRecord) => Promise<void>
16
+ screen: (name: string, options?: AnyRecord) => Promise<void>
17
+ group: (groupId: string, groupTraits?: AnyRecord) => Promise<void>
18
+ alias: (newUserId: string) => Promise<void>
19
+ reset: () => Promise<void>
20
+ identify: (userId: string, userTraits?: AnyRecord) => Promise<void>
21
+ }
22
+
23
+ export type SegmentUser = {
24
+ id: string
25
+ }
26
+
27
+ export type DefaultMode = 'dev' | 'prod'
28
+
29
+ export type SegmentGeneralConfig<C, U extends SegmentUser, M extends string> = Omit<
30
+ SegmentConfig<C, U, M>,
31
+ 'apiKey' | 'createClient' | 'enabled'
32
+ >
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './Segment'