getIsFromRefferal method

Future<bool> getIsFromRefferal()

Implementation

Future<bool> getIsFromRefferal() async {
  // 1. Get remote config values
  final bool isAdsRefer = admodel?.isAdsRefer ?? false;
  final String adsReferKeywords = admodel?.adsRefer ?? "not set,organic";
  const String testReferrer = String.fromEnvironment('TEST_INSTALL_REFERRER');

  // 2. Fetch Install Referrer from Google Play Store
  String referrerUrl = "not set";
  if (testReferrer.isNotEmpty) {
    referrerUrl = Uri.decodeComponent(testReferrer).toLowerCase();
  } else {
    try {
      final ReferrerDetails details =
          await PlayInstallReferrer.installReferrer;
      referrerUrl = details.installReferrer?.toLowerCase() ?? "not set";
    } catch (e) {
      referrerUrl = "not set";
    }
  }

  // 3. Evaluate Referrer Condition
  bool isReferredUser = false;

  if (isAdsRefer) {
    // Split keywords e.g. ["not set", "organic"]
    final List<String> keywords = adsReferKeywords
        .split(',')
        .map((e) => e.trim().toLowerCase())
        .where((e) => e.isNotEmpty)
        .toList();

    // Check if installReferrer contains any organic keyword
    final bool isOrganic = keywords.any(
      (keyword) => referrerUrl.contains(keyword),
    );

    // If it is NOT organic, then it is a paid/referred user
    isReferredUser = !isOrganic;
  }

  // 4. Return the selected ad profile
  if (isReferredUser) {
    if (kDebugMode) {
      print("Referrer Condition MATCHED -> Serving Paid Ads (ads_refer_res)");
    }
    return true; // 🟢 Paid / Marketing Install -> Enable Ads
  } else {
    if (kDebugMode) {
      print(
        "Referrer Condition ORGANIC -> Serving Safe/Organic Profile (organic_res)",
      );
    }
    return false; // 🔴 Organic / Google Play Reviewer -> Disable Ads
  }
}