2

I'm using Firebase dynamic link for the purpose of Refer and Earn. Generally, a user (say Receiver) can download the app in 2 ways:

  • Installing the app from Play Store (organic download).

  • Installing the app using the dynamic link.

Now, how would I know if the app was installed in Receiver's phone using the dynamic link shared with him by another user (say Sender).


This is the code for listening app open and it runs for both scenario mentioned above. I am not able to detect if the app was installed using the dynamic link.

void _listeningAppOpen() async {
  PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
  Uri deepLink = data?.link;

  if (deepLink != null) {
    // app opened by dynamic link
  }

  FirebaseDynamicLinks.instance.onLink(onSuccess: (PendingDynamicLinkData dynamicLink) async {
    Uri deepLink = dynamicLink?.link;

    if (deepLink != null) {
      // app was already opened (in background) and user clicked on dynamic link, we are here now
    }
  });
}
iDecode
  • 22,623
  • 19
  • 99
  • 186

2 Answers2

1

Try using the click timestamp from the PendingDynamicLinkData.

boolean isAppInstalledViaDynamicLink(
    Context context,
    PendingDynamicLinkData pendingDynamicLinkData
) {
    long clickTmestamp = pendingDynamicLinkData.getClickTimestamp();
    long appInstallTimestamp = getAppInstallTimestamp(context);
    return clickTmestamp < appInstallTimestamp;
}

private long getAppInstallTimestamp(Context context) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
    return pi.firstInstallTime;
}
Ashwin
  • 7,277
  • 1
  • 48
  • 70
0

When you create dynamic link from your Firebase Console it asks you, if the user does not have the app where you want to redirect. There are two option one is PlayStore and second is website. In your case just select PlayStore and Firebase will automatically handle whether the app is installed by the user. If not, then it will redirect it to your app on playstore.

When the sender will click share referral link on his device, then a dynamic link will be sent having a unique senderId as parameter in the link. The link will look like: https://yourApp.page.link/?link=https://yourDomain.com/referral?senderId=uniqueSenderId "uniqueSenderId" will be used to distinguish whether the app is first time installed or again installing.

For us to know whether the app is already installed on the receiver's device: The sender's link would contain a parameter "senderId" having unique a unique string for every sender. When the receiver will open that link we can get the sender's unique ID by calling:

final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;
if(deepLink != null){
   String senderId = deepLink.queryParameters['senderId']
   //Store senderId in Database.
}
else{
   //App is downloaded from playstore. Remember here, in first line above 
   //getInitialLink() is only called when app is opened through a dynamicLink
}

If deepLink is null then app is downloaded directly from playStore otherwise it is downloaded from anyone's link. Although as mentioned in above comment getInitialLink() is called only when app is opened through dynamic link. So, if it is not called then app is downloaded from PlayStore. After getting the senderId, we can check whether the receiver already has a referral id stored in his database. If yes then we cancel the newly received dynamic link and if referral id is null we can store the sender's id in it. Thus, we got to know that app was a new install or an re-install.

Priyansh jain
  • 1,065
  • 10
  • 17
  • "Firebase will automatically handle whether the app is installed by the user" -- How do I get to know about it? – iDecode Apr 13 '21 at 12:31
  • @iDecode I have edited the post. I misunderstood the question before. – Priyansh jain Apr 14 '21 at 20:31
  • With the "database", do you mean our own created one or some other kind of database provided by Firebase? – iDecode Apr 15 '21 at 09:04
  • Quoting your "After getting the senderId, we can check whether the receiver already has a referral id stored in his database." -- Do you mean once we get to know that the user installed the app using Firebase dynamic link, we can store the id in the database and next time we can compare the ID with the stored ID? – iDecode Apr 15 '21 at 20:00
  • @iDecode Yes, we can compare and we will know that whether user already installed with other's referral id or not. – Priyansh jain Apr 16 '21 at 07:35
  • In order to store the ID, we need to know if the user downloaded the app from the link, right? But that is what the question is all about. So, basically you're confused yourself (according to what you wrote) – iDecode Apr 16 '21 at 12:56
  • @iDecode To store id you can pass parameter in your dynamic link. The parameter will contain the sender's id. After the receiver clicked on the link. We can get the parameter by calling .queryParamteres which will give you the sender's Id. Then you can store the sender's Id in your database. I am updating the detailed answer above. I thought that you knew about passing parameter in link. So, I was not confused. – Priyansh jain Apr 16 '21 at 19:15