Getting Started with Firebase Dynamic Linking

Abhishek Srivastava
10 min readNov 8, 2022

--

Dynamic Links are deep links into an app that work whether or not users have installed the app yet. When users open a Dynamic Link into an app that is not installed, the app’s Play Store page opens, where users can install the app. After users install and open the app, the app displays the deep-linked content.

See the Dynamic Links developer documentation for more information.

How are Dynamic links different from deep links?

The usual reference of deep links are made in terms of app navigation (a link that directs the user to a specific in-app view) whereas dynamic links are more holistic in nature with web navigation built in their logic. If a user is opening the dynamic link on Android or iOS, it will take them to the linked content directly in your native application. More amazingly, by opening the same dynamic link in their browser, it will redirect them to the website’s equivalent content.

Hence, dynamic links are not exactly the same as deep links as they have more logic built in them and they perform differently compared to deep links, depending on the platform they are used on (iOS vs Android vs web).

How can create Firebase Dynamic Link

There are multiple ways to build dynamic links:

  • Using the Firebase console — This is useful if you’re creating promo links to share on social media. You can select a custom suffix and a name for the link in the Firebase console and track the performance of these dynamic links in the Firebase console or via the Analytics REST API.
  • Using the dynamic link builder API on iOS and Android — Creating dynamic links using link builder API is helpful if you need links for user-to-user sharing or in any situation that requires many links.
  • Using the REST API — This is the preferred way to dynamically create links on platforms that don’t have a builder API.
  • Manually building dynamic links (not recommended) — If you don’t need to track click data and if it’s alright to have long links, you can manually construct dynamic links using URL parameters, and by doing so, avoid an extra network round trip.

How Do Firebase Dynamic Links Works?

When a user opens a dynamic link on an iOS or Android device, they are taken directly to the linked content in your native app. In the case of a desktop browser, they are taken to the equivalent content of the linked content on your website. For users who don’t have your app installed, you can send them to your website, or take them to the play store, or show them interstitial describing the benefits of your app before you take them to the app store for a smoother transition.

Now that you know what dynamic links are and how they work, it is time to see how to create dynamic links in firebase. Below we have step-by-step tutorials along with firebase dynamic links examples to programmatically create dynamic links for Android as well as iOS platforms. Watch below video for more detail.

How to Add our project into Firebase

  • Add Firebase to your Android Project.
  • Follow the quickstart guide to set up your project.
  • Configure the sample:
  • Replace the app_code value in app/build.gradle with your personal app code.
  • Replace the applicationId in app/build.gradle with the package name that matches your app code.
  • Run the sample on your Android device or emulator.
  • Using the sample:
  • When the application is started, a deep link will be generated using your app code.
  • Click Share to share this deep link to another application.
  • The application checks if it was launched from a deep link. If so, the link data will be displayed under the Receive heading.
  • Try sharing the deep link from the application and use that deep link to re-launch the application.

How to receive Firebase Dynamic Links on Android

First complete the project setup step as like above mentioned and will add dynamic link in firebase portal.

Follow the steps mentioned below to create a dynamic link for your mobile app campaign:

Step 1 — After logging in to the Firebase console and navigating to the Firebase project with the linked Android and iOS apps, navigate to the left-hand menu bar and scroll down to the Engage section. Make sure that you have “Editor” permission for the selected project (highlighted in the screenshot below). Under the Engage section, click on “Dynamic Links”.

Step 2 — If you are trying to create a dynamic link for the selected project for the first time, you will be asked to add a domain (URL prefix) for which you would like to create the link. You will have an option to use the domain that you own or the one provided by Google. Firebase limits 10 URL prefixes per project.

Step 3 — Once your URL prefix (domain) has been verified you will notice that it appears on top of the dynamic link list section.

Though the next section in this step is not mandatory, it is highly recommended to whitelist the URL pattern that should be allowed for the deep link (in-app link) that your dynamic link will point to. To do that, click on the three dots right after the “New Dynamic Link” button and select “Allow URL pattern”.

You will get a pop-up box with a text field to add the Regex URL pattern that will be allowed. Please refer to the examples in the screenshot and this link on how to build this URL pattern.

Step 4 — Once you have completed the previous steps, you are now ready to create a dynamic link. Click on the “New Dynamic Link” button.

Add a short URI to your URL to make it more contextual yet descriptive of the campaign it will be used for and then click “Next”.

Step 5 — Add a deep link (the in-app link) and a name to the dynamic link to which you will refer to when tracking data, such as clicks on this link.

Step 6 — This is the most important step in creating the dynamic link. In this step, you will get to define the link behavior: whether to open the link in a browser or open the link in the app. From here on, this article will focus on describing the steps for Android App but the steps for iOS are exactly the same.

Step 7 — The last step is to add campaign tracking parameters and social tags to the dynamic link. Again, this step is optional but highly recommended especially the UTM parameters for accurate attribution. Add the parameters and click on “Create”.

After setup dynamic link on firebase console now open project in Android IDE. To see more click here.

Here we will use Firebase Android BoM to control library versioning of firebase dynamic links.

First open app.gradle file and add related firebase library

dependencies {
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:31.0.2')


// Add the dependencies for the Dynamic Links
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-dynamic-links-ktx'
}

If you don’t want to use Firebase BoM, You can do like as below

dependencies {
// Add the dependencies for the Dynamic Links
// When NOT using the BoM, you must specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-dynamic-links-ktx:21.1.0'
}

Add an intent filter for respective page to open

To handle particular URL intent we need to add intent filter on specific activity on AndroidManifest.xml

<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:host="exampleUrl.com"
android:scheme="https"/>
</intent-filter>
</activity>

How to handle firebase links in Android Activity class

To handle firebase links, In respective activity class we need to add below code.

Firebase.dynamicLinks
.getDynamicLink(intent)
.addOnSuccessListener(this) { pendingDynamicLinkData: PendingDynamicLinkData? ->
// Get deep link from result (may be null if no link is found)
var deepLink: Uri? = null
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.link
}
// Handle the deep link. For example, open the linked
// content, or apply promotional credit to the user's
// account.
// ...
}
.addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }

Here getDynamicLink() method used to receive firebase dynamic links .

How can create dynamic links from Android App

For creating a Dynamic-link programmatically

  1. Create a newDynamicLink object with its builder.
  2. Specify the Dynamic Link parameters with the Builder methods.
  3. Call buildDynamicLink or buildShortDynamicLink.

There is the following example to create a long Dynamic Link to https://www.testurl.com/. It opens with our Android app on Android and the app com.testurl.iOS on iOS.

val dynamicLink = Firebase.dynamicLinks.dynamicLink {
link = Uri.parse("https://www.testurl.com/")
domainUriPrefix = "https://testurl.page.link"
// Opening links with Android app
androidParameters("com.example.android") {
minimumVersion = 16
}
// Opening links with IOs App
iosParameters("com.example.ios") {
appStoreId = "123456789"
minimumVersion = "1.0.1"
}
googleAnalyticsParameters {
source = "orkut"
medium = "social"
campaign = "example-promo"
}
itunesConnectAnalyticsParameters {
providerToken = "123456"
campaignToken = "example-promo"
}
socialMetaTagParameters {
title = "Example of a Dynamic Link"
description = "This link works whether the app is installed or not!"
}
}

How to Create a Short Dynamic Link

For creating short Dynamic Link, build a Dynamic Link in the same way, and then call buildShortDynamicLink(). It needs a network call, so instead of directly returning the link, buildShortDynamicLink() returns a Task and makes a short link available when the request complete.

val shortLinkTask = Firebase.dynamicLinks.shortLinkAsync {
link =
Uri.parse(invitationLink)
domainUriPrefix = "https://spaceo.page.link"

androidParameters {
// The versionCode of the minimum version of your app that can open the link.
// If the installed app is an older version, the user is taken to the Play Store to upgrade the app.
minimumVersion = 1
}
// Set parameters
// ...
}.addOnSuccessListener { result ->
// Short link created
val shortLink = result.shortLink
//val flowchartLink = result.previewLink
this.shortLink.value = shortLink.toString()
}.addOnFailureListener {
// Error
// ...
Log.d("log_tag", "==> ${it.localizedMessage}", it)
this.shortLink.value = it.localizedMessage
}

Use Firebase Dynamic Links with instant apps

Key benefits

  • Wrapping your links with Firebase Dynamic Links guarantees that clicks on links always take users to your instant app. Otherwise, apps can force links to be opened inside an in-app browser instead of an instant app. Firebase Dynamic Links allows you to control the behavior of clicks on links.
  • Firebase Dynamic Links allows you to track analytics on events like clicks, first-opens, re-opens, and installs. Dynamic Links events also are recorded in Google Analytics for Firebase.

Integrating Firebase Dynamic Links with an instant app project

You can integrate Firebase Dynamic Links with your instant app project the same way you would integrate a standard Android app.

After you integrate with Firebase Dynamic Links, you just have to set the androidFallbackLink parameter to your Instant Apps link.

Use Cases of Dynamic Links

Here are some of the ways that you can use dynamic links.

  1. Converting web users to app users

The issue with standard deep links is that they do not survive the app installation process. If a user clicks on a deep link and is directed to the app install page, all the original linked content gets lost. Dynamic links have overcome this problem. So, if your user installs your app by clicking on a dynamic link, all that information is available when your user opens the app for the first time.

2. Marketing Campaigns

You can use Firebase Dynamic Link for marketing campaigns, from e-mails to social media to banner advertisements to even QR codes. You can know which campaigns are getting you the highest quality users. You can also give your users a customized first-time experience based on the campaign that brought them there.

So, if the users installed your music app because you showed them an advertisement for classical music, you can make sure your app takes them to the classical music section the first time they open your app.

3. Sharing

Any dynamic link, Firebase or not, is great for sharing too. The users can use such links to share recipes, links to their favorite level on a game, or even coupon codes. In fact, dynamic links are the technology that powers Firebase invites.

4. Analytics

As dynamic links are Firebase products you can see their stats directly through the Firebase Console. You can find out how many people clicked on a link or use Firebase Analytics to find out which of your users first opened your app through a particular link.

Thanks for reading.

--

--

Abhishek Srivastava

Senior Software Engineer | Android | Java | Kotlin | Xamarin Native Android | Flutter | Go