Skip to content

Commit cedf127

Browse files
authoredSep 18, 2023
feat: Send group telemetry (#471)
* Add group spec * Add page spec * Send telemetry.group
1 parent 875e8c3 commit cedf127

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed
 

‎src/lib/telemetry/client.ts

+46-12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ export class TelemetryClient {
6666
return this.#debug
6767
}
6868

69+
page(name: string, properties: Properties = {}): void {
70+
this.#log('page', name, properties)
71+
this.#push({ type: 'page', name, properties })
72+
}
73+
6974
screen(name: string, properties: Properties = {}): void {
7075
this.#log('screen', name, properties)
7176
this.#push({ type: 'screen', name, properties })
@@ -76,18 +81,12 @@ export class TelemetryClient {
7681
this.#push({ type: 'track', event, properties })
7782
}
7883

79-
alias(userId: string, previousId?: string): void {
80-
const resolvedPreviousId =
81-
previousId ?? this.#user?.userId ?? this.#anonymousId
82-
if (resolvedPreviousId == null) {
83-
throw new Error('Cannot resolve previous id')
84-
}
85-
this.#log('alias', userId, resolvedPreviousId)
84+
group(groupId: string, traits: Traits = {}): void {
85+
this.#log('group', groupId, traits)
8686
this.#push({
87-
type: 'alias',
88-
userId,
89-
previousId: resolvedPreviousId,
90-
anonymousId: undefined,
87+
type: 'group',
88+
groupId,
89+
traits,
9190
})
9291
}
9392

@@ -111,6 +110,21 @@ export class TelemetryClient {
111110
})
112111
}
113112

113+
alias(userId: string, previousId?: string): void {
114+
const resolvedPreviousId =
115+
previousId ?? this.#user?.userId ?? this.#anonymousId
116+
if (resolvedPreviousId == null) {
117+
throw new Error('Cannot resolve previous id')
118+
}
119+
this.#log('alias', userId, resolvedPreviousId)
120+
this.#push({
121+
type: 'alias',
122+
userId,
123+
previousId: resolvedPreviousId,
124+
anonymousId: undefined,
125+
})
126+
}
127+
114128
get #context(): Context {
115129
return {
116130
...(this.#user?.traits == null ? {} : { traits: this.#user.traits }),
@@ -164,6 +178,13 @@ export class TelemetryClient {
164178
}
165179
}
166180

181+
// https://segment.com/docs/connections/spec/screen/
182+
interface PageSpec {
183+
type: 'page'
184+
name: string
185+
properties: Properties
186+
}
187+
167188
// https://segment.com/docs/connections/spec/screen/
168189
interface ScreenSpec {
169190
type: 'screen'
@@ -178,6 +199,13 @@ interface TrackSpec {
178199
properties: Properties
179200
}
180201

202+
// https://segment.com/docs/connections/spec/group/
203+
interface GroupSpec {
204+
type: 'group'
205+
groupId: string
206+
traits: Traits
207+
}
208+
181209
// https://segment.com/docs/connections/spec/identify/
182210
interface IdentifySpec {
183211
type: 'identify'
@@ -218,7 +246,13 @@ interface User {
218246

219247
type Payload = Omit<Message, 'userId'> & CommonSpec
220248

221-
type Message = ScreenSpec | TrackSpec | IdentifySpec | AliasSpec
249+
type Message =
250+
| PageSpec
251+
| ScreenSpec
252+
| TrackSpec
253+
| GroupSpec
254+
| IdentifySpec
255+
| AliasSpec
222256

223257
type Traits = TelemetryRecord
224258

‎src/lib/telemetry/hooks.ts

+4
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ export function useUserTelemetry(): void {
4242
user_identifier_key: clientSession.user_identifier_key,
4343
publishable_key: publishableKey,
4444
})
45+
46+
telemetry.group(clientSession.workspace_id, {
47+
workspace_id: clientSession.workspace_id,
48+
})
4549
}, [clientSession, publishableKey, telemetry])
4650
}

0 commit comments

Comments
 (0)
Please sign in to comment.