Create a Branch Deep Link URL with encapsulated data. Create a short URL for a specific BranchUniversalObject instance, and associate a LinkProperties object with it.
This method is essentially a synchronous method that retrieves the latest referring link parameters stored by the native SDK. However, React Native does not support synchronous calls to native code from JavaScript, so the method returns a promise. You must await the response or use then to receive the result.
However, this is only a restriction of React Native. The purpose of getLatestReferringParams is to retrieve those parameters one time. The promise will only return one result. It will not continue to return results when links are opened or wait for a link to be opened. This method is not intended to notify the app when a link has been opened.
Best practice for this method is to receive the data from the listener (to prevent a race condition).
Returns the parameters associated with the link that referred the session.
Argument
Type
Description
synchronous
boolean
Set to true to allow deferred promise resolution.
Returns
A promise that resolves to a BranchParams object.
Example Usage
import branch from 'react-native-branch'
// Listener
branch.subscribe({
onOpenStart: ({
uri,
cachedInitialEvent
}) => {
console.log(
'subscribe onOpenStart, will open ' +
uri +
' cachedInitialEvent is ' +
cachedInitialEvent,
);
},
onOpenComplete: ({
error,
params,
uri
}) => {
if (error) {
console.error(
'subscribe onOpenComplete, Error from opening uri: ' +
uri +
' error: ' +
error,
);
return;
}
else if (params) {
if (!params['+clicked_branch_link']) {
if (params['+non_branch_link']) {
console.log('non_branch_link: ' + uri);
// Route based on non-Branch links
return;
}
} else {
// Handle params
let deepLinkPath = params.$deeplink_path as string;
let canonicalUrl = params.$canonical_url as string;
// Route based on Branch link data
return
}
}
},
});
let latestParams = await branch.getLatestReferringParams() // Params from last open
getFirstReferringParams
Retrieve data from a Branch Deep Link.
This method is essentially a synchronous method that retrieves the latest referring link parameters stored by the native SDK. However, React Native does not support synchronous calls to native code from JavaScript, so the method returns a promise. You must await the response or use then to receive the result.
Best practice for this method is to receive the data from the listener (to prevent a race condition).
Returns the parameters associated with the link that referred the user. This is only set once, when the user is first referred by a link. Think of this as the user referral parameters.
Returns
A promise that resolves to a BranchParams object.
Example Usage
import branch from 'react-native-branch'
// Listener
branch.subscribe({
onOpenStart: ({
uri,
cachedInitialEvent
}) => {
console.log(
'subscribe onOpenStart, will open ' +
uri +
' cachedInitialEvent is ' +
cachedInitialEvent,
);
},
onOpenComplete: ({
error,
params,
uri
}) => {
if (error) {
console.error(
'subscribe onOpenComplete, Error from opening uri: ' +
uri +
' error: ' +
error,
);
return;
}
else if (params) {
if (!params['+clicked_branch_link']) {
if (params['+non_branch_link']) {
console.log('non_branch_link: ' + uri);
// Route based on non-Branch links
return;
}
} else {
// Handle params
let deepLinkPath = params.$deeplink_path as string;
let canonicalUrl = params.$canonical_url as string;
// Route based on Branch link data
return
}
}
},
});
let installParams = await branch.getFirstReferringParams() // Params from original install
A promise that resolves to a string representing the QR code.
Example Usage
import branch from 'react-native-branch'
var qrCodeSettings = {
width: 500,
codeColor: "#3b2016",
backgroundColor: "#a8e689",
centerLogo: "https://cdn.branch.io/branch-assets/159857dsads5682753-og_image.png",
margin: 1,
imageFormat: "PNG"
};
var buoOptions = {
title: "A Test Title",
contentDescription: "A test content desc",
contentMetadata: {
price: "200",
productName: "QR Code Scanner",
customMetadata: { "someKey": "someValue", "anotherKey": "anotherValue" }
}
};
var lp = {
feature: "qrCode",
tags: ["test", "working"],
channel: "facebook",
campaign: "posters"
};
var controlParams = {
$desktop_url: "https://www.desktop.com",
$fallback_url: "https://www.fallback.com"
};
try {
var result = await branch.getBranchQRCode(qrCodeSettings, buoOptions, lp, controlParams);
}
catch (err) {
console.log('QR Code Err: ', err);
}
setIdentity
This method may be helpful if you have your own user IDs for customers, or you want referral and event data to persist across platforms or uninstall/reinstall. Using this method can make it easier to know when users access your service from different devices.
You can validate that an identity has been set using the Branch Dashboard.
Warning: Do not use this method to send PII to Branch.
Method
Description
setIdentity: (identity: string) => void;
Identifies the current user to the Branch API by supplying a unique identifier as a string value.
Argument
Type
Description
identity
string
A string value containing the unique identifier of the user.
Example Usage
import branch from 'react-native-branch'
branch.setIdentity('theUserId')
branch.logout()
logEvent
By default, the Branch React Native SDK tracks clicks, opens, installs, reinstalls and impressions automatically (out-of-the-box).
To log other Branch Events:
Create a BranchUniversalObject instance.
Create a BranchEventParams instance.
Use new BranchEvent to create a Branch Event with the newly created BranchUniversalObject and BranchEventParams instances
Use the logEvent() method.
Learn more about tracking events with the Branch React Native SDK in our Advanced Features guide.
Method
Description
logEvent: () => Promise<null>;
Log an event to Branch for tracking and analytics.
Deep link into your own app from within the app itself.
Warning: Handling a new Branch Deep Link in your app will clear the current session data and a new referred "open" will be attributed.
Argument
Type
Description
url
string
The URL to deep link to.
options
{ newActivity?: boolean }
If true, Branch will allow Android to finish the current activity before opening the link. This results in a new activity window.
Ignored on iOS.
Example Usage
import branch from 'react-native-branch'
branch.openURL("https://example.app.link/u3fzDwyyjF")
// Finish the Android current activity before opening the link
// Results in a new activity window
// Ignored on iOS
branch.openURL("https://example.app.link/u3fzDwyyjF", {newActivity: true})
subscribe
Method
Description
Method: subscribe: BranchSubscribe;
Related interface: interface BranchSubscribeOptions {onOpenComplete: BranchSubscribeCallback; onOpenStart?:(event: BranchOpenStartEvent) => void;}
Receive a notification whenever a link is opened, including at app launch.
There is no need to call getLatestReferringParams() at app launch to check for an initial link. Use subscribe() to handle all link opens.
Argument
Type
Description
onOpenComplete
BranchSubscribeCallback
The callback to this method will return any initial link that launched the app and all subsequent link opens.
Related to: interface BranchSubscriptionEventBase { params: BranchParams | undefined; error: string | null | undefined; uri: string | undefined; }
onOpenStart
event: BranchOpenStartEvent
Related to: export interface BranchOpenStartEvent { uri: string; cachedInitialEvent?: boolean; }
Example Usage
import branch from 'react-native-branch'
branch.subscribe({
onOpenStart: ({
uri,
cachedInitialEvent
}) => {
console.log(
'subscribe onOpenStart, will open ' +
uri +
' cachedInitialEvent is ' +
cachedInitialEvent,
);
},
onOpenComplete: ({
error,
params,
uri
}) => {
if (error) {
console.error(
'subscribe onOpenComplete, Error from opening uri: ' +
uri +
' error: ' +
error,
);
return;
}
else if (params) {
if (!params['+clicked_branch_link']) {
if (params['+non_branch_link']) {
console.log('non_branch_link: ' + uri);
// Route based on non-Branch links
return;
}
} else {
// Handle params
let deepLinkPath = params.$deeplink_path as string;
let canonicalUrl = params.$canonical_url as string;
// Route based on Branch link data
return
}
}
},
});
setRequestMetadata
Some third-party Data Integration Partners require setting certain identifiers before initializing Branch.
To do this:
AdddeferInitForPluginRuntime to your branch.json file.
Use the setRequestMetadata() method to set your identifiers.
The Branch Link to send with the push notification payload data.
options
newActivity?: boolean
If set to true, finish the current Android activity before opening the link. Results in a new activity window. Ignored on iOS.
Example Usage
import branch from 'react-native-branch'
let data = {
"aps": {
"alert": "Push notification with a Branch deep link",
"badge": "1"
},
"branch": "https://example.app.link/u3fzDwyyjF"
}
branch.openURL(data["branch"],{newActivity: true})
disableTracking
Method
Description
disableTracking: (disable: boolean) => void;
Method to change the tracking state. If disabled, the Branch React Native SDK will not track any user data or state. The SDK will not send any network calls, except for deep linking, when tracking is disabled.
Argument
Type
Description
disable
boolean
When set to true, tracking is disabled.
Example Usage
import branch from 'react-native-branch'
branch.disableTracking(true)
isTrackingDisabled
Method
Description
isTrackingDisabled: () => Promise<boolean>;
Checks to see whether tracking is disabled.
Returns
A promise that resolves to whether tracking is disabled.
Example Usage
import branch from 'react-native-branch'
branch.isTrackingDisabled()
logout
Method
Description
logout: () => void;
Call this method if you know that a different person is about to use the app. For example, if you allow users to log out and let their friends use the app, you should call logout() to notify Branch to create a new user for this device. This will clear the first and latest params, and a new session is created.
Example Usage
import branch from 'react-native-branch'
branch.logout()
import branch from 'react-native-branch'
// Retrieve ATT authorization status
// ...
// Pass ATT authorization status to Branch
let ATTAuthorizationStatus = "restricted"
branch.handleATTAuthorizationStatus(ATTAuthorizationStatus)
import branch from 'react-native-branch'
branch.setPreinstallCampaign("My Pre-Install Campaign")
setPreInstallPartner
Method
Description
setPreInstallPartner: (partner: string) => void;
Add the pre-install partner name.
Argument
Type
Description
partner
string
The pre-install partner name.
Example Usage
import branch from 'react-native-branch'
branch.setPreInstallPartner("My Pre-Install Partner")
setDMAParamsForEEA
🚧
Warning: Omitted by Default
Please note that the 3 consent parameters related to setDMAParamsForEEA are all omitted by default.
Failure to include user consent signals may result in attribution or campaign performance degradation. For additional information, please reach out to your Google AM.
Making a successful setDMAParamsForEEA() call requires that all 3 parameters be set.
Sets the value of parameters required by Google Conversion APIs for DMA Compliance in the EEA region.
Argument
Type
Description
eeaRegion
boolean
Set to true if user is included in European Union regulations. For example, if the user is located within the EEA, they are within the scope of DMA.
Set to false if user is considered excluded from European Union regulations.
adPersonalizationConsent
boolean
Set to true if user has granted consent for ads personalization.
Set to false if user has denied consent for ads personalization.
adUserDataUsageConsent
boolean
Set to true if user has granted consent for 3P transmission of user-level data for ads.
Set to false is user has denied consent for 3P transmission of user-level data for ads.
Example Usage
import branch from 'react-native-branch'
// Example for an EEA resident who has denied both ad personalization and data usage consent
branch.setDMAParamsForEEA(true,false,false)
// Example for an EEA resident who has consented to ad personalization but denied data usage consent
branch.setDMAParamsForEEA(true,true,false)
// Example for an EEA resident who has denied ad personalization but granted data usage consent
branch.setDMAParamsForEEA(true,false,true)