Sub topic 16
Deferred Deep Links and Install Attribution
This chapter assumes you have read the earlier chapters. You should know how verified links reach a Flutter app, how go_router maps a URL to a screen, and how cold start differs from a running app. A normal deep link needs the app to be installed already. A deferred deep link covers the case where it is not.
The Problem: The App Is Not Installed
Suppose a friend sends you https://example.com/products/42. You tap it on a phone that does not have the app. Three things can happen:
- The browser opens the web page for the product. The app is never offered.
- The link sends you to the store, and you install the app. When you open it, the app shows its home screen. Product 42 is lost.
- The link sends you to the store, you install the app, and the first launch opens product 42.
Option 3 is a deferred deep link. The link's destination is deferred until after install. It is useful for referral programs, invitations, marketing campaigns, and any flow where the first screen matters.
Why the Platform Does Not Do This for You
Android App Links and iOS Universal Links, covered in earlier chapters, only work when the app is already installed and verified. When the app is missing, the operating system opens the link in the browser. The app store install flow does not pass the original URL to the new app. Neither Apple nor Google provides a general-purpose mechanism that carries your own link data across the install.
So deferred deep linking is something you build by matching two events:
- Before install: a user taps a link and lands on a server you control or a service you use.
- After install: the app starts for the first time and asks that server whether this device recently tapped a link.
The central question is how the server decides that the person who tapped the link is the same person who just opened the app. The answer is matching, and each method has different accuracy and privacy costs.
Ways to Match a Click to an Install
| Method | How it works | Strength | Weakness |
|---|---|---|---|
| Install Referrer (Android) | Google Play passes a referrer string you put in the store URL to the installed app | Exact, no fingerprinting | Android only, and only for installs from Google Play |
| Clipboard | The landing page copies a token, and the app reads it on first launch | Works on iOS | Triggers a paste notice on recent iOS versions and feels intrusive |
| Probabilistic matching | The server compares IP address, OS version, and device model at click time and at first launch | Needs no user action | Can match the wrong device, and may conflict with platform privacy rules |
| Sign-in matching | The invite is tied to an email or account, and the app applies it after login | Reliable and privacy-friendly | Only works when the user signs in |
On Android, the Play Install Referrer is the most reliable method. Your web link redirects to a Play Store URL with a referrer parameter, for example:
https://play.google.com/store/apps/details?id=com.example.app&referrer=product%3D42After install, the app can read the product=42 string through the Play Install Referrer API.
On iOS there is no equivalent. Apps that need deferral on iOS usually combine a landing page, a clipboard token or probabilistic match, and sign-in matching as a fallback.
Install Attribution
Install attribution answers a different question: which campaign, link, or channel caused this install? Deferred deep linking and attribution use the same click-to-install matching, so they are usually sold together. They have different goals:
- Deferred deep link: change what the app shows on first launch.
- Attribution: record where the user came from for reporting, such as the ad campaign or the referring user.
You can do attribution without routing, and routing without attribution. Keep them separate in your code so a change to one does not break the other.
Build It Yourself or Use a Service
There are two common approaches.
Attribution services. Third-party link platforms provide short links, a landing page, store redirects, matching logic, and Flutter plugins. The app calls the plugin on startup and receives link data. You save the work of building matching and keeping up with platform changes. The costs are a dependency, recurring fees, user data sent to a vendor, and a need to review their privacy practices. Because vendors, pricing, and plugin APIs change often, check the current documentation of any service before choosing one.
A small custom solution. For Android-only needs or invite flows tied to login, you can write your own. You need a server endpoint that stores a pending link and an app step that claims it. This gives you control and keeps the data in your system, but you take on maintenance and edge cases.
The decision comes down to a few criteria:
- Do you need iOS matching without sign-in? A service helps most here.
- Do you need cross-channel campaign reports? A service is likely to pay off.
- Is the link tied to an account, such as "join this team"? A custom solution can be simple and reliable.
- Are there privacy or regulatory limits on sending data to third parties? Lean toward custom.
Handling the Result in Flutter
Whichever source supplies the link, the app should treat it like any other incoming link. Do not write a second routing system. Convert the deferred link into a path and pass it through the router, and the redirects, guards, and unknown-link fallback you built earlier all apply.
The following example shows a first-launch check. It assumes you have a function fetchPendingLink() that returns a path such as /products/42, or null. That function would call your server, a plugin, or the Play Install Referrer, depending on your design.
import 'package:go_router/go_router.dart';
import 'package:shared_preferences/shared_preferences.dart';
const _checkedKey = 'deferred_link_checked';
Future<void> claimDeferredLink(
GoRouter router,
Future<String?> Function() fetchPendingLink,
) async {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool(_checkedKey) ?? false) return; // only on first launch
// Mark first, so a crash or a failed request does not cause repeated checks.
await prefs.setBool(_checkedKey, true);
final path = await fetchPendingLink();
if (path == null) return;
final uri = Uri.tryParse(path);
if (uri == null || uri.hasScheme || uri.hasAuthority) return; // paths only
if (!uri.path.startsWith('/products/')) return; // allowlist
router.go(uri.toString());
}Several choices in this code matter:
- It runs once. The check is saved in
SharedPreferences, so later launches do not replay an old link. - It accepts paths only. A deferred link comes from a server, so treat it as untrusted input. The security rules from the earlier chapter apply here too.
- It uses an allowlist. Only paths your app expects are routed.
- It goes through the router. Authentication redirects still run, so a signed-out user lands on login with the target saved.
Edge Cases and Failure Modes
- Timing. The first-launch request takes time. Show the normal home screen, or a short splash, and navigate when the result arrives. Set a timeout of a few seconds so a slow server does not block the user. Avoid yanking the user to a new screen after they have already started interacting.
- Wrong matches. Probabilistic matching can send user A's link to user B, for example when both share a network. Do not use it for anything sensitive, such as granting access or applying account-level changes. Use it for display routing only.
- Expired links. Store pending links for a short time, such as a day, and delete them after one claim.
- Reinstalls. Clearing app data resets the saved flag, so a reinstall may check again. Decide whether that is acceptable, and make the server return a link only once.
- Users who open the store page but never tap the link again. Some store flows lose the referrer. Always have a sensible default screen.
- Platform privacy rules. iOS and Android restrict tracking and device identifiers. Fingerprinting and advertising IDs are subject to consent rules and store policy. Review current App Store and Google Play policies, and applicable privacy law, before you ship matching that uses device signals.
- Web fallback. If the user never installs, the landing page should still show the product content. The deferred link then costs nothing when it fails.
Testing Deferred Links
You cannot fully test this with adb or simctl link commands, because those assume the app is already installed. Instead:
- Test
claimDeferredLinkwith a fakefetchPendingLink. Return a valid path, an unexpected path, a full URL, andnull. Confirm only the valid path navigates. - Test the run-once behavior by calling the function twice and checking the router moves only once.
- For Android, use Play's internal testing track with a referrer parameter, and uninstall the app before each run.
- For iOS, use TestFlight or a clean simulator reset. Check behavior with the clipboard prompt and without a match.
Summary
A deferred deep link carries a link across an install by storing it before install and claiming it at first launch. The platforms do not do this for you, so you choose a matching method and accept its trade-offs. Route the result through the same go_router setup as every other link, validate it as untrusted input, and make it run once.
Exercise
Your app needs to send new users to a team invitation screen after install. The invitation was sent to their email address, and they must sign in to use the app. Which matching method would you choose, and why would you avoid applying the team membership automatically from a probabilistic match?
Show answer
Use sign-in matching. The server already knows the invitation belongs to that email address, so after login the app asks for pending invitations for that account and routes to the invitation screen. This is exact and needs no device fingerprinting. A probabilistic match can pair the wrong device with a link, for example two users on the same network. Adding someone to a team based on that guess could grant access to the wrong person. A guess is acceptable for choosing which screen to show, not for changing permissions.
Exercise
In claimDeferredLink, why does the code save the "checked" flag before calling fetchPendingLink instead of after it succeeds? Name one downside of this choice.
Show answer
Saving first guarantees the check runs at most once, even if the request crashes or the app is killed mid-request, so an old link is never replayed on a later launch. The downside is that a temporary network failure on first launch means the link is lost for good, because the app will not try again. If that matters, save the flag only after a successful response, and keep an attempt counter and an expiry so retries stop eventually.