Skip to content

Commit

Permalink
Merge pull request #334 from rudderlabs/feature/sdk-1429-allow-settin…
Browse files Browse the repository at this point in the history
…g-of-the-advertisingid-in-rn-ios-before-sdk-init

feat: allow setting of the advertisingId in rn ios before sdk init and add clearAdvertisingId API
  • Loading branch information
1abhishekpandey authored Apr 9, 2024
2 parents 040468b + ccfbe78 commit f7b1572
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 168 deletions.
136 changes: 79 additions & 57 deletions apps/example/ios/Example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion apps/example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ target 'Example' do
config = use_native_modules!

use_react_native!(
:path => config[:reactNativePath],
:path => "../../#{config[:reactNativePath]}", # I've commented out the next line and added this line to resolve the build error caused by the use of use_frameworks! on react-native v0.73.x, due to yoga header path issue, refer here: https://github.com/facebook/react-native/issues/41938#issuecomment-1925888879.
# :path => config[:reactNativePath],
# Enables Flipper.
#
# Note that if you have use_frameworks! enabled, Flipper will not work and
Expand Down Expand Up @@ -76,6 +77,18 @@ target 'Example' do

# Setting the deployment target to 13.0 to solve the build error
# config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'

# XCdoe 15.3 build issue: https://github.com/facebook/react-native/issues/43335#issuecomment-1980708164
if target.name == 'Flipper'
file_path = 'Pods/Flipper/xplat/Flipper/FlipperTransportTypes.h'
contents = File.read(file_path)
unless contents.include?('#include <functional>')
File.open(file_path, 'w') do |file|
file.puts('#include <functional>')
file.puts(contents)
end
end
end
end
end

Expand Down
202 changes: 101 additions & 101 deletions apps/example/ios/Podfile.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion apps/example/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const initRudderReactNativeSDK = async () => {
collectDeviceId: false,
recordScreenViews: false,
logLevel: RUDDER_LOG_LEVEL.VERBOSE,
sessionTimeout: 0,
enableBackgroundMode: true,
trackAppLifecycleEvents: true,
autoSessionTracking: true,
Expand Down
24 changes: 22 additions & 2 deletions apps/example/src/app/RudderEvents.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Button } from 'react-native';
import { Button, Platform } from 'react-native';
import rudderClient, { IRudderContext } from '@rudderstack/rudder-sdk-react-native';

const RudderEvents = () => {
Expand Down Expand Up @@ -100,7 +100,7 @@ const RudderEvents = () => {
};

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

const getSessionId = async () => {
Expand All @@ -122,6 +122,24 @@ const RudderEvents = () => {
console.log(`${JSON.stringify(context)}`);
};

const putAdvertisingId = async () => {
switch (Platform.OS) {
case 'ios':
await rudderClient.putAdvertisingId('iOS-ADVERTISING-ID');
console.log('Setting iOS Advertising ID');
break;
case 'android':
await rudderClient.putAdvertisingId('ANDROID-ADVERTISING-ID');
console.log('Setting Android Advertising ID');
break;
}
};

const clearAdvertisingId = async () => {
await rudderClient.clearAdvertisingId();
console.log('Cleared Advertising ID');
};

return (
<>
<Button title="Identify" onPress={identify} />
Expand All @@ -138,6 +156,8 @@ const RudderEvents = () => {
<Button title="enableOptOut()" onPress={enableOptOut} />
<Button title="disableOptOut()" onPress={disableOptOut} />
<Button title="getRudderContext()" onPress={getRudderContext} />
<Button title="putAdvertisingId()" onPress={putAdvertisingId} />
<Button title="clearAdvertisingId()" onPress={clearAdvertisingId} />
</>
);
};
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/RNRudderSdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.tvos.deployment_target = '11.0'

s.dependency "React"
s.dependency "Rudder", '>= 1.24.1', '< 2.0.0'
s.dependency "Rudder", '>= 1.26.0', '< 2.0.0'
end


2 changes: 1 addition & 1 deletion libs/sdk/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ repositories {
dependencies {
compileOnly 'com.facebook.react:react-native:+'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.rudderstack.android.sdk:core:[1.19.1, 2.0.0)'
implementation 'com.rudderstack.android.sdk:core:[1.22.0, 2.0.0)'
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ public void putAdvertisingId(String id) {
}
}

@ReactMethod
public void clearAdvertisingId() {
if (!isRudderClientInitializedAndReady()) {
return;
}
rudderClient.clearAdvertisingId();
}

@ReactMethod
public void putAnonymousId(String id) {
if (!TextUtils.isEmpty(id)) {
Expand Down
9 changes: 5 additions & 4 deletions libs/sdk/ios/RNRudderSdkModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@ -(BOOL) isRudderClientInitializedAndReady {
}

RCT_EXPORT_METHOD(putAdvertisingId:(NSString*)id) {
[RSClient putAdvertisingId:id];
}

RCT_EXPORT_METHOD(clearAdvertisingId) {
if (![self isRudderClientInitializedAndReady]) {
return;
}
RSContext* rudderContext = [[RSClient sharedInstance] getContext];
if (rudderContext != nil && id != nil && [id length] != 0) {
[rudderContext putAdvertisementId:id];
}
[[RSClient sharedInstance] clearAdvertisingId];
}

RCT_EXPORT_METHOD(putAnonymousId:(NSString*)id) {
Expand Down
1 change: 1 addition & 0 deletions libs/sdk/src/NativeBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface Bridge {
optOut(optOut: boolean): Promise<void>;
putDeviceToken(token: string): Promise<void>;
putAdvertisingId(id: string): Promise<void>;
clearAdvertisingId(): Promise<void>;
putAnonymousId(id: string): Promise<void>;
// eslint-disable-next-line @typescript-eslint/ban-types
registerCallback(integrationName: string, callback: Function): Promise<void>;
Expand Down
5 changes: 5 additions & 0 deletions libs/sdk/src/RudderClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ async function putAdvertisingId(advertisingId: string) {
}
}

async function clearAdvertisingId() {
bridge.clearAdvertisingId();
}

/**
* @deprecated use putAnonymousId{@link putAnonymousId(anonymousId: string)} instead
*/
Expand Down Expand Up @@ -356,6 +360,7 @@ const rudderClient = {
putDeviceToken,
putAdvertisingId,
setAdvertisingId,
clearAdvertisingId,
putAnonymousId,
setAnonymousId,
registerCallback,
Expand Down

0 comments on commit f7b1572

Please sign in to comment.