File tree Expand file tree Collapse file tree 6 files changed +67
-3
lines changed
packages/use-analytics/src/analytics Expand file tree Collapse file tree 6 files changed +67
-3
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @scaleway/use-analytics " : patch
3+ ---
4+
5+ Add normalize id on anonymousId, userId, groupId
Original file line number Diff line number Diff line change @@ -9,3 +9,4 @@ export type {
99export { userMigrationsTraits } from './segments/userMigrationsTraits'
1010export { defaultLoadOptions } from './constants'
1111export { useDestinations } from './useDestinations'
12+ export { normalizeId } from './normalizeId'
Original file line number Diff line number Diff line change 1+ type ID = string | number | undefined | null
2+
3+ export const normalizeId = ( id : ID ) => {
4+ if ( id === null || id === undefined ) return undefined
5+
6+ // Already a string but possibly JSON-encoded
7+ if ( typeof id === 'string' ) {
8+ try {
9+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
10+ const parsed = JSON . parse ( id )
11+ // Only use parsed if it’s a string too
12+
13+ if ( typeof parsed === 'string' ) {
14+ return parsed
15+ }
16+ } catch {
17+ // not JSON, keep original
18+ }
19+
20+ return id
21+ }
22+
23+ return String ( id )
24+ }
Original file line number Diff line number Diff line change 1+ import type { RudderAnalytics } from '@rudderstack/analytics-js'
2+ import { normalizeId } from './normalizeId'
3+
4+ export const normalizeIdsMigration = ( rudderAnalytics : RudderAnalytics ) => {
5+ // normalize id issue with segment migration
6+ const anonymousId = rudderAnalytics . getAnonymousId ( )
7+ const normalizeAnonymousId = normalizeId ( anonymousId )
8+ if ( normalizeAnonymousId !== anonymousId ) {
9+ rudderAnalytics . setAnonymousId ( normalizeAnonymousId )
10+ }
11+
12+ const userId = rudderAnalytics . getUserId ( )
13+ const normalizeUserId = userId ? normalizeId ( userId ) : null
14+
15+ if ( userId !== normalizeUserId && normalizeUserId ) {
16+ rudderAnalytics . identify ( normalizeUserId )
17+ }
18+
19+ const groupId = rudderAnalytics . getGroupId ( )
20+ const normalizeGroupId = groupId ? normalizeId ( groupId ) : null
21+
22+ if ( userId !== normalizeGroupId && normalizeGroupId ) {
23+ rudderAnalytics . group ( normalizeGroupId )
24+ }
25+ }
Original file line number Diff line number Diff line change 11import type { RudderAnalytics } from '@rudderstack/analytics-js'
2+ import { normalizeId } from '../normalizeId'
23
34const SEGMENT_COOKIES_KEY = {
45 ANONYMOUS_ID : 'ajs_anonymous_id' ,
@@ -16,14 +17,20 @@ export const userMigrationsTraits = (rudderAnalytics: RudderAnalytics) => {
1617 const rudderGroupId = rudderAnalytics . getGroupId ( )
1718
1819 if ( segmentAnonymousId ) {
19- rudderAnalytics . setAnonymousId ( segmentAnonymousId )
20+ rudderAnalytics . setAnonymousId ( normalizeId ( segmentAnonymousId ) )
2021 }
2122
2223 if ( segmentUserId && ( ! rudderUserId || rudderUserId !== segmentUserId ) ) {
23- rudderAnalytics . identify ( segmentUserId )
24+ const normalizedUserId = normalizeId ( segmentUserId )
25+ if ( normalizedUserId ) {
26+ rudderAnalytics . identify ( normalizedUserId )
27+ }
2428 }
2529
2630 if ( segmentGroupId && ( ! rudderGroupId || rudderGroupId !== segmentGroupId ) ) {
27- rudderAnalytics . group ( segmentGroupId )
31+ const normalizedGroupId = normalizeId ( segmentGroupId )
32+ if ( normalizedGroupId ) {
33+ rudderAnalytics . group ( normalizedGroupId )
34+ }
2835 }
2936}
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
66import { destSDKBaseURL , pluginsSDKBaseURL } from '../constants'
77import type { CategoryKind } from '../types'
88import { defaultConsentOptions , defaultLoadOptions } from './constants'
9+ import { normalizeIdsMigration } from './normalizeIdsMigration'
910import { userMigrationsTraits } from './segments/userMigrationsTraits'
1011
1112type Analytics = RudderAnalytics
@@ -130,6 +131,7 @@ export function AnalyticsProvider<T extends Events>({
130131 pluginsSDKBaseURL : pluginsSDKBaseURL ( settings . cdnURL ) ,
131132 onLoaded : ( rudderAnalytics : Analytics ) => {
132133 userMigrationsTraits ( rudderAnalytics )
134+ normalizeIdsMigration ( rudderAnalytics )
133135
134136 rudderAnalytics . consent ( {
135137 ...defaultConsentOptions ,
You can’t perform that action at this time.
0 commit comments