Plugins :
app_links:
android_play_install_referrer:
Link to share :
https://play.google.com/store/apps/details?id=com.erer&referrer=referral_code%3DTEST123
Flutter Code :
import 'package:app_links/app_links.dart';
import 'package:android_play_install_referrer/android_play_install_referrer.dart';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
class DeepLinkService {
static const String _referralKey = 'referral_code';
static final AppLinks _appLinks = AppLinks();
static Future<void> initDeepLinks() async {
debugPrint('Initializing deep links...');
// 1️⃣ Handle real-time deep/link stream
try {
final Uri? initialUri = await _appLinks.getInitialAppLink();
if (initialUri != null) {
debugPrint('Initial deep link: $initialUri');
_processDeepLink(initialUri);
}
} catch (e) {
debugPrint('Error getting initial deep link: $e');
}
_appLinks.uriLinkStream.listen((Uri? uri) {
if (uri != null) {
debugPrint('Stream deep link: $uri');
_processDeepLink(uri);
}
});
// 2️⃣ Handle deferred deep link (after installation)
await _checkInstallReferrer();
}
static void _processDeepLink(Uri uri) {
debugPrint('Processing deep link: $uri');
if (uri.host == 'bike9pro.in' && uri.path.startsWith('/referral')) {
final referralCode = uri.queryParameters['code'];
if (referralCode != null) {
debugPrint('Saving referral from deep link: $referralCode');
_saveReferralCode(referralCode);
}
}
}
static Future<void> _checkInstallReferrer() async {
if (defaultTargetPlatform == TargetPlatform.android) {
try {
final details = await AndroidPlayInstallReferrer.installReferrer;
final raw = details.installReferrer; // e.g. "referral_code=TEST123"
debugPrint('Install referrer raw: $raw');
final Map<String, String> params = Uri.splitQueryString(raw!);
final code = params['referral_code'];
if (code != null) {
debugPrint('Saving deferred referral: $code');
_saveReferralCode(code);
}
} catch (e) {
debugPrint('Failed to get install referrer: $e');
}
}
}
static Future<void> _saveReferralCode(String code) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_referralKey, code);
}
static Future<String?> getReferralCode() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_referralKey);
}
static Future<void> clearReferralCode() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_referralKey);
}
}
main.dart
await DeepLinkService.initDeepLinks();
Files on web for deeplink :
https://yourweb.com/.well-known/assetlinks.json
[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.app",
"sha256_cert_fingerprints": [""]
}
}]
Response In log for referral
I/flutter (17407): Initializing deep links...
W/FlutterJNI(17407): FlutterJNI.loadLibrary called more than once
W/FlutterJNI(17407): FlutterJNI.prefetchDefaultFontManager called more than once
W/FlutterJNI(17407): FlutterJNI.init called more than once
I/flutter (17407): Install referrer raw: referral_code=TEST123
I/flutter (17407): Saving deferred referral: TEST123
Comments
Post a Comment