Skip to content

Commit

Permalink
Merge pull request #150 from aswathy-deriv/userid_distribution
Browse files Browse the repository at this point in the history
Aswathy/fix: added the user_id and anonymous_id attribute
  • Loading branch information
prince-deriv authored Dec 23, 2024
2 parents 762080e + 216f809 commit 9bed60d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export function createAnalyticsInstance(options?: Options) {
...(growthbookOptions?.attributes?.network_downlink && {
network_downlink: growthbookOptions?.attributes.network_downlink,
}),
...(growthbookOptions?.attributes?.user_id && {
user_id: growthbookOptions?.attributes?.user_id,
}),
...(growthbookOptions?.attributes?.anonymous_id && {
anonymous_id: growthbookOptions?.attributes?.anonymous_id || _rudderstack.getAnonymousId(),
}),
}
growthbookOptions ??= {}
growthbookOptions.attributes ??= {}
Expand Down Expand Up @@ -115,6 +121,7 @@ export function createAnalyticsInstance(options?: Options) {
device_type,
account_type,
user_id,
anonymous_id,
app_id,
utm_source,
utm_medium,
Expand Down Expand Up @@ -148,8 +155,13 @@ export function createAnalyticsInstance(options?: Options) {
url,
domain,
loggedIn,
user_id,
anonymous_id,
}
if (user_identity) {
config.id = user_identity
config.user_id = user_identity
}
if (user_identity) config.id = user_identity
_growthbook.setAttributes(config)
}

Expand All @@ -167,6 +179,8 @@ export function createAnalyticsInstance(options?: Options) {
...(network_downlink && { network_downlink }),
...(network_rtt && { network_rtt }),
...(network_type && { network_type }),
...(user_id && { user_id }),
...(anonymous_id && { anonymous_id }),
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/growthbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ export class Growthbook {
loggedIn,
network_type,
network_downlink,
user_id,
anonymous_id,
}: TGrowthbookAttributes) => {
const CURRENT_ATTRIBUTES = this.GrowthBook.getAttributes()
this.GrowthBook.setAttributes({
...CURRENT_ATTRIBUTES,
id,
...(user_id && { user_id }),
...(anonymous_id && { anonymous_id }),
...(country && { country }),
...(residence_country && { residence_country }),
...(user_language && { user_language }),
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type TGrowthbookAttributes = {
loggedIn?: boolean
network_type?: string
network_downlink?: number
user_id?: string
anonymous_id?: string
}

export type TGrowthbookOptions = Partial<Omit<Context, 'attributes'> & { attributes: TCoreAttributes }>
Expand Down

0 comments on commit 9bed60d

Please sign in to comment.