Skip to content

Commit 8366b91

Browse files
authored
feat(OTel): Push Amplitude IDs to Baggage (#7238)
1 parent 9109123 commit 8366b91

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

frontend/common/service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as amplitude from '@amplitude/analytics-browser'
12
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/dist/query/react'
23

34
import { FetchBaseQueryArgs } from '@reduxjs/toolkit/dist/query/fetchBaseQuery'
@@ -36,6 +37,17 @@ const baseApiOptions = (queryArgs?: Partial<FetchBaseQueryArgs>) => {
3637
} catch (e) {}
3738
}
3839

40+
const deviceId = amplitude.getDeviceId()
41+
const sessionId = amplitude.getSessionId()
42+
const userId = amplitude.getUserId()
43+
if (deviceId || sessionId || userId) {
44+
const entries: string[] = []
45+
if (deviceId) entries.push(`amplitude.device_id=${deviceId}`)
46+
if (sessionId) entries.push(`amplitude.session_id=${sessionId}`)
47+
if (userId) entries.push(`amplitude.user_id=${userId}`)
48+
headers.set('baggage', entries.join(','))
49+
}
50+
3951
return headers
4052
},
4153
...queryArgs,

frontend/common/types/responses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ export type Account = {
694694
pylon_email_signature: string
695695
sign_up_type: SignupType
696696
id: number
697+
uuid: string
697698
email: string
698699
auth_type: AuthType
699700
is_superuser: boolean

frontend/web/project/api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ const API = {
112112
}
113113

114114
if (Project.amplitude) {
115-
amplitude.setUserId(id)
115+
const amplitudeUserId = user.uuid || id
116+
amplitude.setUserId(amplitudeUserId)
116117
API.trackTraits({ email: id })
117118
if (window.engagement) {
118119
window.engagement.boot({
@@ -122,7 +123,7 @@ const API = {
122123
amplitude.track(event.event_type, event.event_properties),
123124
},
124125
],
125-
user: { user_id: id, user_properties: {} },
126+
user: { user_id: amplitudeUserId, user_properties: {} },
126127
})
127128
}
128129
}

0 commit comments

Comments
 (0)