Skip to content

Commit

Permalink
feat: made deviceId collection configurable and de-coupled anonymousi…
Browse files Browse the repository at this point in the history
…d and deviceId (#232)

* feat: made deviceId collection configurable and de-coupled anonymousId and deviceId

* feat: added reset api with boolean flag to clear anonymousId

* chore: renamed withCollectDeviceId to collectDeviceId on rn

* chore: updated sample app ui:

* fix: fixed collectDeviceId on native side

* chore(example): update sample ios project

* chore(rudder-sdk-react-native-monorepo): update package-lock.json

* chore: updated log message for collectDeviceId

Co-authored-by: Abhishek Pandey <64667840+1abhishekpandey@users.noreply.github.com>

---------

Co-authored-by: Desu Sai Venkat <venkat@rudderstack.com>
Co-authored-by: Abhishek Pandey <64667840+1abhishekpandey@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 1, 2023
1 parent 34644df commit c600d20
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions apps/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ PODS:
- React-Core
- RNRudderSdk (1.8.1):
- React
- Rudder (< 2.0, >= 1.19.2)
- Rudder (< 2.0.0, >= 1.9.2)
- RNScreens (3.20.0):
- React-Core
- React-RCTImage
Expand Down Expand Up @@ -863,7 +863,7 @@ SPEC CHECKSUMS:
React-runtimeexecutor: 8fa50b38df6b992c76537993a2b0553d3b088004
ReactCommon: 1fed1243105330aa50ad7051207b61656f5e570b
RNCAsyncStorage: a46ee6bf15cf1ba863d0a47287236f9c95d5b213
RNRudderSdk: 7efd553fa0ef684d61ba27548d8e6d7af54db9a6
RNRudderSdk: 8dfb5b5a38bd98f95c205ba59a9f7be6539b752d
RNScreens: 218801c16a2782546d30bd2026bb625c0302d70f
RNSVG: c1e76b81c76cdcd34b4e1188852892dc280eb902
Rudder: 26a9b6756628cb953f3151d835104ac9f5635861
Expand Down
15 changes: 9 additions & 6 deletions apps/example/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const initRudderReactNativeSDK = async () => {
const config = {
dataPlaneUrl: TEST_DATAPLANE_URL,
autoCollectAdvertId: true,
collectDeviceId: false,
recordScreenViews: false,
logLevel: RUDDER_LOG_LEVEL.VERBOSE,
sessionTimeout: 0,
Expand Down Expand Up @@ -136,7 +137,6 @@ const App = () => {
<Stack.Screen name="Home" component={HomeScreen} options={{ title: 'Welcome' }} />
<Stack.Screen name="Profile" component={ProfileScreen} />
</Stack.Navigator>
<RudderEvents />
</NavigationContainer>
);
};
Expand All @@ -161,16 +161,19 @@ const HomeScreen = ({ navigation }) => {
return (
<>
<Text>Dataplane: {TEST_DATAPLANE_URL}</Text>
<Text>Write key: {TEST_WRITE_KEY}</Text>
<Button
title="Go to Jane's profile"
onPress={() => navigation.navigate('Profile', { name: 'Jane' })}
/>
<Text>
Write key: {TEST_WRITE_KEY} {'\n'}
</Text>
<Button
testID="init_btn"
title="Initialization"
onPress={async () => await initialization()}
/>
<RudderEvents />
<Button
title="Go to Jane's profile"
onPress={() => navigation.navigate('Profile', { name: 'Jane' })}
/>
</>
);
};
Expand Down
2 changes: 1 addition & 1 deletion apps/example/src/app/RudderEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const RudderEvents = () => {
};

const reset = () => {
rudderClient.reset();
rudderClient.reset(true);
};

return (
Expand Down
4 changes: 1 addition & 3 deletions libs/sdk/CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
## [1.8.1](https://github.com/rudderlabs/rudder-sdk-react-native/compare/rudder-sdk-react-native@1.8.0...rudder-sdk-react-native@1.8.1) (2023-08-24)


### Bug Fixes

* ios module compilation error ([#233](https://github.com/rudderlabs/rudder-sdk-react-native/issues/233)) ([5ff512f](https://github.com/rudderlabs/rudder-sdk-react-native/commit/5ff512fff9c260936542f92571859b83367af3d6))

- ios module compilation error ([#233](https://github.com/rudderlabs/rudder-sdk-react-native/issues/233)) ([5ff512f](https://github.com/rudderlabs/rudder-sdk-react-native/commit/5ff512fff9c260936542f92571859b83367af3d6))
2 changes: 1 addition & 1 deletion libs/sdk/RNRudderSdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Pod::Spec.new do |s|
s.requires_arc = true

s.dependency "React"
s.dependency "Rudder", ">= 1.19.2", "< 2.0"
s.dependency "Rudder", '>= 1.9.2', '< 2.0.0'
end


Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ private RudderConfig.Builder buildConfig() {
if (config.hasKey("logLevel")) {
configBuilder.withLogLevel(config.getInt("logLevel"));
}
if (config.hasKey("collectDeviceId")) {
configBuilder.withCollectDeviceId(config.getBoolean("collectDeviceId"));
}
if (config.hasKey("dbEncryption")) {
Map<String, Object> dbEncryption = Utility.convertReadableMapToMap(config.getMap("dbEncryption"));
String key = (String) (Utility.getValueFromMap(dbEncryption, "key", ""));
Expand All @@ -92,4 +95,4 @@ private void disableAutoConfigFlagsForNativeSDK(RudderConfig.Builder configBuild
configBuilder.withTrackLifecycleEvents(false);
configBuilder.withAutoSessionTracking(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ public void getRudderContext(Promise promise) throws JSONException {
}

@ReactMethod
public void reset() {
public void reset(boolean clearAnonymousId) {
if (!isRudderClientInitializedAndReady()) {
return;
}
rudderClient.reset();
rudderClient.reset(clearAnonymousId);
}

@ReactMethod
Expand Down
5 changes: 4 additions & 1 deletion libs/sdk/ios/RNParamsConfigurator.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ - (instancetype) initWithConfig:(NSDictionary*)config {
self = [super init];
if (self) {
self->config = config;

// Default values
self.trackLifeCycleEvents = true;
self.recordScreenViews = false;
Expand Down Expand Up @@ -78,6 +78,9 @@ -(RSConfigBuilder*)buildConfig {
if ([config objectForKey:@"logLevel"]) {
[configBuilder withLoglevel:[config[@"logLevel"] intValue]];
}
if ([config objectForKey:@"collectDeviceId"]) {
[configBuilder withCollectDeviceId:[config[@"collectDeviceId"] boolValue]];
}
if ([config objectForKey:@"dbEncryption"]) {
NSDictionary *dbEncryption = config[@"dbEncryption"];
NSString *key = dbEncryption[@"key"];
Expand Down
22 changes: 11 additions & 11 deletions libs/sdk/ios/RNRudderSdkModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ - (dispatch_queue_t)methodQueue
if (rsClient == nil) {
self->configParams = [[RNParamsConfigurator alloc] initWithConfig:config];
RSConfigBuilder *configBuilder = [self->configParams handleConfig];

[RSLogger logDebug:@"setup: Initiating RNPreferenceManager"];
self->preferenceManager = [RNPreferenceManager getInstance];

rsClient = [RSClient getInstance:self->configParams.writeKey config:[RNRudderAnalytics buildWithIntegrations:configBuilder] options:[self getRudderOptionsObject:_options]];

[RSLogger logDebug:@"setup: Initiating RNUserSessionPlugin"];
self->session = [[RNUserSessionPlugin alloc] initWithAutomaticSessionTrackingStatus:self->configParams.autoSessionTracking withLifecycleEventsTrackingStatus:self->configParams.trackLifeCycleEvents withSessionTimeout:self->configParams.sessionTimeout];
[self->session handleSessionTracking];

[RSLogger logDebug:@"setup: Initiating RNBackGroundModeManager"];
self->backGroundModeManager = [[RNBackGroundModeManager alloc] initWithEnableBackgroundMode:self->configParams.enableBackgroundMode];

[RSLogger logDebug:@"setup: Initiating RNApplicationLifeCycleManager"];
self->applicationLifeCycleManager = [[RNApplicationLifeCycleManager alloc] initWithTrackLifecycleEvents:self->configParams.trackLifeCycleEvents andBackGroundModeManager:self->backGroundModeManager withLaunchOptions:_bridge.launchOptions withSessionPlugin:self->session];
[self->applicationLifeCycleManager trackApplicationLifeCycle];

if (self->configParams.recordScreenViews) {
[RSLogger logDebug:@"setup: Enabling automatic recording of screen views"];
[self->applicationLifeCycleManager prepareScreenRecorder];
Expand Down Expand Up @@ -72,7 +72,7 @@ -(BOOL) isRudderClientInitializedAndReady {
[builder setEventName:_event];
[builder setPropertyDict:_properties];
[builder setRSOption:[self getRudderOptionsObject:_options]];

[[RSClient sharedInstance] trackWithBuilder:builder];
}
RCT_EXPORT_METHOD(screen:(NSString*)_event properties:(NSDictionary*)_properties options:(NSDictionary*)_options)
Expand All @@ -84,7 +84,7 @@ -(BOOL) isRudderClientInitializedAndReady {
// [builder setEventName:_event];
// [builder setPropertyDict:_properties];
// [builder setRSOption:[[RSOption alloc] init]];

[self->session saveEventTimestamp];
[[RSClient sharedInstance] screen:_event properties:_properties options:[self getRudderOptionsObject:_options]];
}
Expand Down Expand Up @@ -124,7 +124,7 @@ -(BOOL) isRudderClientInitializedAndReady {
}
if([_groupId isEqual:@""])
{
[RSLogger logWarn:@"Dropping the Group call as groupId can not be empty"];
[RSLogger logWarn:@"Dropping the Group call as groupId can not be empty"];
return;
}
[self->session saveEventTimestamp];
Expand All @@ -138,12 +138,12 @@ -(BOOL) isRudderClientInitializedAndReady {
}
}

RCT_EXPORT_METHOD(reset)
RCT_EXPORT_METHOD(reset:(BOOL) clearAnonymousId)
{
if (![self isRudderClientInitializedAndReady]) {
return;
}
[[RSClient sharedInstance] reset];
[[RSClient sharedInstance] reset:clearAnonymousId];
}

RCT_EXPORT_METHOD(flush)
Expand Down
1 change: 1 addition & 0 deletions libs/sdk/src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export const SDK_VERSION = '1.9.0';
export const AUTO_SESSION_TRACKING = true;
export const SESSION_TIMEOUT = 300000;
export const ENABLE_BACKGROUND_MODE = false;
export const COLLECT_DEVICE_ID = true;
3 changes: 2 additions & 1 deletion libs/sdk/src/NativeBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Configuration {
autoSessionTracking?: boolean;
sessionTimeout?: number;
enableBackgroundMode?: boolean;
collectDeviceId?: boolean;
dbEncryption?: DBEncryption;
// eslint-disable-next-line @typescript-eslint/ban-types
withFactories?: Array<Record<string, unknown> | Function>;
Expand Down Expand Up @@ -43,7 +44,7 @@ export interface Bridge {
traits: Record<string, unknown> | null,
options: Record<string, unknown> | null,
): Promise<void>;
reset(): Promise<void>;
reset(clearAnonymousId: boolean): Promise<void>;
flush(): Promise<void>;
optOut(optOut: boolean): Promise<void>;
putDeviceToken(token: string): Promise<void>;
Expand Down
8 changes: 6 additions & 2 deletions libs/sdk/src/RudderClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ function validateConfiguration(configuration: Configuration) {
logWarn("setup : 'enableBackgroundMode' must be a boolen. Falling back to the default value");
delete configuration.enableBackgroundMode;
}
if (configuration.collectDeviceId && typeof configuration.collectDeviceId != 'boolean') {
logWarn("setup : 'collectDeviceId' must be a boolean. Falling back to the default value");
delete configuration.collectDeviceId;
}
}

// setup the RudderSDK with writeKey and Config
Expand Down Expand Up @@ -274,8 +278,8 @@ async function putAnonymousId(anonymousId: string) {
}
}

async function reset() {
bridge.reset();
async function reset(clearAnonymousId = false) {
bridge.reset(clearAnonymousId);
}

async function flush() {
Expand Down
3 changes: 3 additions & 0 deletions libs/sdk/src/RudderConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AUTO_SESSION_TRACKING,
SESSION_TIMEOUT,
ENABLE_BACKGROUND_MODE,
COLLECT_DEVICE_ID,
} from './Constants';

export const configure = async (
Expand All @@ -31,6 +32,7 @@ export const configure = async (
autoCollectAdvertId = AUTO_COLLECT_ADVERT_ID,
trackAppLifecycleEvents = TRACK_LIFECYCLE_EVENTS,
recordScreenViews = RECORD_SCREEN_VIEWS,
collectDeviceId = COLLECT_DEVICE_ID,
dbEncryption,
withFactories = [],
}: Configuration,
Expand Down Expand Up @@ -61,6 +63,7 @@ export const configure = async (
trackAppLifecycleEvents,
enableBackgroundMode,
recordScreenViews,
collectDeviceId,
};

if (dbEncryption !== undefined) {
Expand Down

0 comments on commit c600d20

Please sign in to comment.