Skip to content

Fix support for setting custom anonymous IDs from React Native library. #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,24 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
)

@ReactMethod
fun identify(userId: String?, traits: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) =
analytics.identify(
userId,
Traits() from traits,
optionsFrom(context, integrations)
)
fun identify(userId: String?, traits: ReadableMap?, options: ReadableMap?, integrations: ReadableMap?, context: ReadableMap?) {

val mergedTraits = if (options?.hasKey("anonymousId") == true) {
val map = WritableNativeMap()
map.merge(traits)
map.putString("anonymousId", options.getString("anonymousId"))
map
} else {
traits
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iOS library expects the anonymousId to be part of the options object where the Android library looks for it at creation time inside traits.


analytics.identify(
userId,
Traits() from mergedTraits,
optionsFrom(context, integrations)
)
}


@ReactMethod
fun group(groupId: String, traits: ReadableMap?, integrations: ReadableMap, context: ReadableMap) =
Expand Down
14 changes: 10 additions & 4 deletions packages/core/ios/RNAnalytics/RNAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@ - (NSDictionary*)withContextAndIntegrations :(NSDictionary*)context :(NSDictiona
options:[self withContextAndIntegrations :context :integrations]];
}

RCT_EXPORT_METHOD(identify:(NSString*)userId :(NSDictionary*)traits :(NSDictionary*)integrations :(NSDictionary*)context) {
[SEGAnalytics.sharedAnalytics identify:userId
traits:traits
options:[self withContextAndIntegrations :context :integrations]];
RCT_EXPORT_METHOD(identify:(NSString*)userId
:(NSDictionary * _Nullable)traits
:(NSDictionary *)options
:(NSDictionary *)integrations
:(NSDictionary *)context) {
NSMutableDictionary *mergedOptions = [[self withContextAndIntegrations :context :integrations] mutableCopy];
[mergedOptions addEntriesFromDictionary: options ?: @{}];
[SEGAnalytics.sharedAnalytics identify: userId
traits: traits
options: mergedOptions];
}

RCT_EXPORT_METHOD(group:(NSString*)groupId :(NSDictionary*)traits :(NSDictionary*)integrations :(NSDictionary*)context) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ it('does .screen()', () =>
testCall('screen')('Shopping cart', { from: 'Product page' }, {}, ctx))

it('does .identify()', () =>
testCall('identify')('sloth', { eats: 'leaves' }, {}, ctx))
testCall('identify')('sloth', { eats: 'leaves' }, {}, {}, ctx))

it('does .group()', () => testCall('group')('bots', { humans: false }, {}, ctx))

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export module Analytics {
* @param options A dictionary of options, e.g. integrations (thigh analytics integration to forward the event to)
*/
public async identify(user: string, traits: JsonMap = {}, options: Options = {}) {
await this.middlewares.run('identify', { user, traits, integrations: options.integrations || {} }, options.context || {})
await this.middlewares.run('identify', { user, traits, options, integrations: options.integrations || {} }, options.context || {})
}

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export interface Context extends JsonMap {
}
}

export interface Options {
export type Options = {
integrations?: Integrations
context?: Context
}
} & JsonMap

export interface Bridge {
setup(configuration: Configuration): Promise<void>
Expand All @@ -58,8 +58,9 @@ export interface Bridge {
): Promise<void>
identify(
user: string,
traits: JsonMap,
traits: JsonMap | null,
options: Options,
integrations: Integrations,
context: JsonMap
): Promise<void>
screen(
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export interface IdentifyPayload
'identify',
{
user: string
traits: JsonMap
traits: JsonMap | null
options: JsonMap
integrations: Integrations
}
> {}
Expand Down Expand Up @@ -113,6 +114,7 @@ export class MiddlewareChain {
identify(
payload.data.user,
payload.data.traits,
payload.data.options,
payload.data.integrations,
payload.context
)
Expand Down