Skip to content

Commit

Permalink
Add DynamicLink.utmParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Minishlink committed Aug 13, 2021
1 parent 90476b8 commit 1f479af
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import com.facebook.react.bridge.*;
import com.google.android.gms.tasks.Tasks;
Expand All @@ -41,6 +42,7 @@ public class ReactNativeFirebaseDynamicLinksModule extends ReactNativeFirebaseMo

private String initialLinkUrl = null;
private int initialLinkMinimumVersion = 0;
private WritableMap initialLinkUtmParameters = new WritableNativeMap();

/**
* Ensures calls to getInitialLink only tries to retrieve the link from getDynamicLink once.
Expand Down Expand Up @@ -164,12 +166,13 @@ public void getInitialLink(Promise promise) {
if (pendingDynamicLinkData != null) {
initialLinkUrl = pendingDynamicLinkData.getLink().toString();
initialLinkMinimumVersion = pendingDynamicLinkData.getMinimumAppVersion();
initialLinkUtmParameters = Arguments.makeNativeMap(pendingDynamicLinkData.getUtmParameters());
}

// Guard against the scenario where the app was launched using a dynamic link,
// then, the app was backgrounded using the Back button, and resumed from the Overview (screen).
if (initialLinkUrl != null && !launchedFromHistory) {
promise.resolve(dynamicLinkToWritableMap(initialLinkUrl, initialLinkMinimumVersion));
promise.resolve(dynamicLinkToWritableMap(initialLinkUrl, initialLinkMinimumVersion, initialLinkUtmParameters));
} else {
promise.resolve(null);
}
Expand All @@ -191,7 +194,8 @@ public void resolveLink(String link, Promise promise) {
if (linkData != null && linkData.getLink() != null && linkData.getLink().toString() != null) {
String linkUrl = linkData.getLink().toString();
int linkMinimumVersion = linkData.getMinimumAppVersion();
promise.resolve(dynamicLinkToWritableMap(linkUrl, linkMinimumVersion));
Bundle linkUtmParameters = linkData.getUtmParameters();
promise.resolve(dynamicLinkToWritableMap(linkUrl, linkMinimumVersion, Arguments.makeNativeMap(linkUtmParameters)));
} else {
rejectPromiseWithCodeAndMessage(promise, "not-found", "Dynamic link not found");
}
Expand All @@ -206,7 +210,7 @@ public void resolveLink(String link, Promise promise) {
}
}

private WritableMap dynamicLinkToWritableMap(String url, int minVersion) {
private WritableMap dynamicLinkToWritableMap(String url, int minVersion, WritableMap utmParameters) {
WritableMap writableMap = Arguments.createMap();

writableMap.putString("url", url);
Expand All @@ -217,6 +221,8 @@ private WritableMap dynamicLinkToWritableMap(String url, int minVersion) {
writableMap.putInt("minimumAppVersion", minVersion);
}

writableMap.putMap("utmParameters", utmParameters);

return writableMap;
}

Expand Down Expand Up @@ -420,7 +426,7 @@ public void onNewIntent(Intent intent) {
if (pendingDynamicLinkData != null) {
ReactNativeFirebaseEventEmitter.getSharedInstance().sendEvent(new ReactNativeFirebaseEvent(
"dynamic_links_link_received",
dynamicLinkToWritableMap(pendingDynamicLinkData.getLink().toString(), pendingDynamicLinkData.getMinimumAppVersion())
dynamicLinkToWritableMap(pendingDynamicLinkData.getLink().toString(), pendingDynamicLinkData.getMinimumAppVersion(), Arguments.makeNativeMap(pendingDynamicLinkData.getUtmParameters()))
));
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/dynamic-links/e2e/dynamicLinks.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe('dynamicLinks()', function () {

dynamicLink.should.be.an.Object();
dynamicLink.url.should.equal('https://rnfirebase.io');
dynamicLink.utmParameters.should.eql({});
});
});

Expand All @@ -172,6 +173,7 @@ describe('dynamicLinks()', function () {

spy.getCall(0).args[0].should.be.an.Object();
spy.getCall(0).args[0].url.should.equal('https://invertase.io/hire-us');
spy.getCall(0).args[0].utmParameters.should.eql({});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static NSString *const LINK_RECEIVED_EVENT = @"dynamic_links_link_received";
@interface RNFBDynamicLinksAppDelegateInterceptor : NSObject <UIApplicationDelegate>
@property (strong, readwrite) NSString *_Nullable initialLinkUrl;
@property (strong, readwrite) NSString *_Nullable initialLinkMinimumAppVersion;
@property (strong, readwrite) NSDictionary<NSString *, id> *_Nonnull initialLinkUtmParametersDictionary;

+ (instancetype)sharedInstance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ + (instancetype)sharedInstance {
sharedInstance = [[RNFBDynamicLinksAppDelegateInterceptor alloc] init];
sharedInstance.initialLinkUrl = nil;
sharedInstance.initialLinkMinimumAppVersion = nil;
sharedInstance.initialLinkUtmParametersDictionary = @{};
[GULAppDelegateSwizzler proxyOriginalDelegate];
[GULAppDelegateSwizzler registerAppDelegateInterceptor:sharedInstance];
});
Expand Down Expand Up @@ -64,10 +65,12 @@ - (BOOL)application:(UIApplication *)application
if (_initialLinkUrl == nil) {
_initialLinkUrl = dynamicLink.url.absoluteString;
_initialLinkMinimumAppVersion = dynamicLink.minimumAppVersion;
_initialLinkUtmParametersDictionary = dynamicLink.utmParametersDictionary;
}
[[RNFBRCTEventEmitter shared] sendEventWithName:LINK_RECEIVED_EVENT body:@{
@"url": dynamicLink.url.absoluteString,
@"minimumAppVersion": dynamicLink.minimumAppVersion == nil ? [NSNull null] : dynamicLink.minimumAppVersion,
@"utmParameters": dynamicLink.utmParametersDictionary == nil ? @{} : dynamicLink.utmParametersDictionary,
}];
}

Expand All @@ -85,10 +88,12 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct
if (_initialLinkUrl == nil) {
_initialLinkUrl = dynamicLink.url.absoluteString;
_initialLinkMinimumAppVersion = dynamicLink.minimumAppVersion;
_initialLinkUtmParametersDictionary = dynamicLink.utmParametersDictionary;
}
[[RNFBRCTEventEmitter shared] sendEventWithName:LINK_RECEIVED_EVENT body:@{
@"url": dynamicLink.url.absoluteString,
@"minimumAppVersion": dynamicLink.minimumAppVersion == nil ? [NSNull null] : dynamicLink.minimumAppVersion,
@"utmParameters": dynamicLink.utmParametersDictionary == nil ? @{} : dynamicLink.utmParametersDictionary,
}];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ - (id)init {
resolve(@{
@"url": dynamicLink.url.absoluteString,
@"minimumAppVersion": dynamicLink.minimumAppVersion == nil ? [NSNull null] : dynamicLink.minimumAppVersion,
@"utmParameters": dynamicLink.utmParametersDictionary == nil ? @{} : dynamicLink.utmParametersDictionary,
});
} else if ([RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUrl != nil) {
resolve(@{
@"url": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUrl,
@"minimumAppVersion": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkMinimumAppVersion == nil ? [NSNull null] : [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkMinimumAppVersion,
@"utmParameters": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUtmParametersDictionary == nil ? @{} : [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUtmParametersDictionary,
});
} else {
resolve([NSNull null]);
Expand All @@ -151,11 +153,13 @@ - (id)init {
resolve(@{
@"url": dynamicLink.url.absoluteString,
@"minimumAppVersion": dynamicLink.minimumAppVersion == nil ? [NSNull null] : dynamicLink.minimumAppVersion,
@"utmParameters": dynamicLink.utmParametersDictionary == nil ? @{} : dynamicLink.utmParametersDictionary,
});
} else if (!error && [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUrl != nil) {
resolve(@{
@"url": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUrl,
@"minimumAppVersion": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkMinimumAppVersion == nil ? [NSNull null] : [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkMinimumAppVersion,
@"utmParameters": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUtmParametersDictionary == nil ? @{} : [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUtmParametersDictionary,
});
} else if (error) {
[RNFBSharedUtils rejectPromiseWithUserInfo:reject userInfo:(NSMutableDictionary *) @{
Expand All @@ -176,6 +180,7 @@ - (id)init {
resolve(@{
@"url": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUrl,
@"minimumAppVersion": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkMinimumAppVersion == nil ? [NSNull null] : [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkMinimumAppVersion,
@"utmParameters": [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUtmParametersDictionary == nil ? @{} : [RNFBDynamicLinksAppDelegateInterceptor sharedInstance].initialLinkUtmParametersDictionary,
});
} else {
resolve([NSNull null]);
Expand All @@ -192,6 +197,7 @@ - (id)init {
resolve(@{
@"url": dynamicLink.url.absoluteString,
@"minimumAppVersion": dynamicLink.minimumAppVersion == nil ? [NSNull null] : dynamicLink.minimumAppVersion,
@"utmParameters": dynamicLink.utmParametersDictionary == nil ? @{} : dynamicLink.utmParametersDictionary,
});
} else if (!error || (error && [error.localizedDescription containsString:@"dynamicLinks error 404"])) {
[RNFBSharedUtils rejectPromiseWithUserInfo:reject userInfo:(NSMutableDictionary *) @{
Expand Down
7 changes: 7 additions & 0 deletions packages/dynamic-links/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ export namespace FirebaseDynamicLinksTypes {
* On iOS this returns a string value representing the minimum app version (not the iOS system version).
*/
minimumAppVersion: number | string | null;

/**
* The potential UTM parameters linked to this dynamic link
*
* It will only work for short links, not long links
*/
utmParameters: Record<string, string>;
}

/**
Expand Down

0 comments on commit 1f479af

Please sign in to comment.