Android Deferred Deep link


Code of this article is available in Git

Deep link is a URL that sends user to a particular screen of your app.

  • Direct deep link opens a specific location within the app only when user already has the app installed on their device but for new users, a deep link does absolutely nothing because the user doesn’t have the app. If they install the app, they will see landing page of the app not the page we want
  • Deferred deep link works like a normal deep link for those users that already have the app but for new new users, it will do some magic with which they will redirect to the particular page we need.

With deferred deep link a new user can be directed to Google Play to download your app, then directly deep linked to the specific location within your app on first app open.

You can implement deferred deep linking in you app via 3rd party libraries like Branch or Tune but the best choice is native functionality of Android SDK without any dependency on 3rd party libraries.

To do this, we have two steps:

  1. Implement direct deep linking into your app
  2. Listen to Google Play Referrer to handle deferred deep links

Implement direct deep linking: SOURCE

  • Add intent filter to your activity for incoming links. Specify the ACTION_VIEW intent action so that the intent filter can be reached from Google Search.
  • Add <data> tags to represents a URI format that resolves to the activity. <data> tags must have scheme, other properties are optional.
  • If more than one of your activities want to handle deep link, you can define pathPattern or pathPrefix for them in manifest file.
  • Include the BROWSABLE category. It is required in order for the intent filter to be accessible from a web browser. Without it, clicking a link in a browser cannot resolve to your app.
  • Also include the DEFAULT category. This allows your app to respond to implicit intents. Without this, the activity can be started only if the intent specifies your app component name.

Once you've added intent filters with URIs for activity content to your app manifest, Android is able to route any Intentthat has matching URIs to your app at runtime.

<intent-filter android:autoVerify="true"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.BROWSABLE" /><data android:scheme="http" android:host="www.iranatlas.info" android:pathPrefix="/droid" /><data android:scheme="https" android:host="www.iranatlas.info" android:pathPrefix="/droid" /></intent-filter>

You can use data provided by the Intent to determine what you need to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent. You can call these methods at any time during the lifecycle of the activity, but you should generally do so during early callbacks such as onCreate() or onStart().

<intent-filter android:autoVerify="true"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.BROWSABLE" /><data android:scheme="http" android:host="www.iranatlas.info" android:pathPrefix="/droid" /><data android:scheme="https" android:host="www.iranatlas.info" android:pathPrefix="/droid" /></intent-filter>
val action = intent.action

        if (Intent.ACTION_MAIN == action) run {
            // user launches the app from app icon or widget// do your normal logic here

        } else if (Intent.ACTION_VIEW == action) {
            // deferred deep link is running the app// red data from intent.data 
           
            }
        }

So we implemented deep links successfully. Now we need to jump to step two which is Listening to Google Play Referrer to manage deferred deep links. We receive referrer from Play when we add Google Analytics SDK to our app. in that case we can recognise which campaigns, websites, and other apps are referring users to Google Play Store to download the app.

implementation 'com.google.android.gms:play-services-analytics:10.2.4'

Now we need to add InstallReferrerReceiver receiver to our app. It extends CampaignTrackingReceiver and is broadcasted when an app is installed from the Google Play Store. This BroadcastReceiver listens for that Intent, passing the install referrer data to GTM for Mobile Apps and Google Analytics.

<receiver android:name="sample.deeplinking.deferred.receiver.InstallReferrerReceiver">
     <intent-filter>
          <action android:name="com.android.vending.INSTALL_REFERRER" />
     </intent-filter>
</receiver>

What is InstallReferrerReceiver?

URL to your app in Google Play is like this:

https://play.google.com/store/apps/details?id=your.package.name

You can add referrer to your URL:

https://play.google.com/store/apps/details?id=your.package.name&referrer=<somenicetext>?<number>%3D<123456789>

When user downloads your app from above link, a broadcast will be sent from Googl Play app to your app containing the referrer you had defined previously in download URL which in our sample is "referrer=somenicetext?number%3D123456789".

Please mention, this broadcast is sent only one time when user opens the app for the very first time.

Now the world is your, you receive the broadcast and will know what is the referrer, so you can handle it and redirect user to the screen that is in the referrer. This means deferred deep linking.

Undocumented experience: If you want to create a URL, use the tool below this page.

We expect receiving encoded URL with encoded chars, for example %26 instead of "&" or %3D instead of "=" otherwise referrer doesn't work properly. App should decode receiving referrer based on classic Java URLDecoder standard.

Further work: Moving from Android Development to Web Development side.

Final work is showing the right URL to users with the correct referrer. You can define buttons in you website or send the URL by SMS or email to users.

In some cases you need to have the same URL for all users regardless their devices because you don't know if your user is a smart Android user, an iOS customer or a desktop user! If she has iOS device, you need to create a URL to Apple app store or if she has Android handset, she needs to go to Play store. So the solution is adding a simple JavaScript code to your URL to check their device and lead them to the correct store with the correct referrer.

For this reason you need to read window.location and check if it is isMobile.Android() then create androidIntentUrl. Your androidIntentUrl must have a browser_fallback_url in which you can add your referrer.

<script type="text/javascript">

function getUrlParam(param){
var parameters=window.location.search.substring(1);
var sURLVariables=parameters.split('&');
console.log(sURLVariables);
for(var i=0;i<sURLVariables.length;i++){
var sParameterName=sURLVariables[i].split('=');
if(sParameterName[0]==param){
return sParameterName[1];
}
}
}

var token=getUrlParam(token);

var androidIntentUrl="intent://www.iranatlas.info/droid/deferred-deeplink"+window.location.search+"#Intent;scheme=http;package=sample.deeplinking.deferred.deferreddeeplink;S.browser_fallback_url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails?id=sample.deeplinking.deferred.deferreddeeplink&referrer=droid%2Fcolor"+window.location.search+";end";

var isMobile={
Windows:function(){
return/IEMobile|Windows Phone|Lumia/i.test(navigator.userAgent);
},
Android:function(){
return/Android/i.test(navigator.userAgent);
},
iPhone:function(){
return/iPhone/i.test(navigator.userAgent);
}
};

// Windows return both android and iPhone in it's identity, need to check first.
if(!isMobile.Windows()){
if(isMobile.Android()){
window.location=androidIntentUrl;
}

if(isMobile.iPhone()){
//window.location=appleStoreUrl;
}
}

/******************************************************************/
</script>

Without above code, you have to send a deep link URL to users like below which is referring to Google Play URL:

https://play.google.com/store/apps/details?id=yout.package.name&referrer=somenicetext?number%3D123456789

After adding above Java Script code to your website, you can have a nice deep link URL which is pointing to your own website like below. It will check you user's device and create the right URL for her.

https://yourowndomain/yourownpage.htm?color=yellow

Image from link

And last but not the least, you can send broadcast to your app manually during the coding process. To send the broadcast open terminal and enter below commands:

adb shell setprop log.tag.GAv4 DEBUG
adb logcat -s GAv4

adb shell 
am broadcast -a com.android.vending.INSTALL_REFERRER -n <your.package>/.<path.up.until.your.BroadcastReceiver> --es "referrer" "utm_source=test_source\&utm_medium=test_medium\&utm_term=test_term\&utm_content=test_content\&utm_campaign=test_name"

PS, don't forget to clear cache of your app and force stop it in your device before sending the broadcast.

I stopped looking for any other article for “deferred deep link” after this. In 2022 also, this article has more information compared to any othe available article. Thanks for writing

Abbas Oveissi

Engineering Manager at ICE Global

3y

I've read some articles about this topic, but your article is the best!

Aditya Kumar Binju

Senior Manager Enterprise Application Development | Mobile Architect @Impetus

3y

The only article that provided, exact reference implementation/details on how deferred linking will work.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics