非Google Play分发的Android应用邀请追踪实现方案咨询
Hey there! I’ve built a few referral systems for apps distributed outside Google Play, so I can walk you through the most reliable, actionable approaches here. Let’s break this down step by step:
Since we can’t use Google Play’s Install Referrer API, we need two key things:
- A way to get the
referred_by_userparameter from the invite link to the installed app. - A secure, repeatable process to validate the referral and issue rewards.
First, set up your server to respond to the invite link (https://example.com/download_app?referred_by_user=12345678_john) with a web page that does two things:
- Automatically triggers the APK download for your app.
- Prepares to pass the referral code to the app (we’ll cover the "how" in Step 2).
Keep the web page simple: add a clear message like "App downloading! Open it after installation to claim your referral reward."
Here are the most practical methods to get the referral code into the app post-installation, ranked by reliability and ease of implementation:
Option 1: Clipboard + Web Page Prompt (Simplest, Works for Most Cases)
When a user clicks the invite link, your web page uses JavaScript to copy the referred_by_user value to the device’s clipboard, then prompts the user about it.
On the app’s first launch:
- Check the clipboard for a valid referral code pattern (e.g., matches
\d{8}_\w+). - If found, use it for verification, then clear the clipboard to avoid reprocessing.
Sample Android code to read the clipboard:
val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager if (clipboardManager.hasPrimaryClip() && clipboardManager.primaryClip!!.itemCount > 0) { val clipboardText = clipboardManager.primaryClip!!.getItemAt(0).text.toString() if (clipboardText.matches(Regex("\\d{8}_\\w+"))) { // Pass code to your processing logic validateAndRewardReferral(clipboardText) // Clear clipboard to prevent duplicate processing clipboardManager.setPrimaryClip(ClipData.newPlainText("", "")) } }
Pros: No complex server setup, easy to implement.
Cons: Users might clear the clipboard before opening the app; requires clear user instructions.
Option 2: Server-Side Device Matching (Most Reliable)
Tie the referral code to the user’s device before installation:
- When a user clicks the invite link, your server captures device identifiers (IP address, user agent, or mobile browser-specific details via JS) and stores them with the
referred_by_usercode. - On first app launch, send a unique device identifier (e.g., a randomly generated UUID stored in
SharedPreferences) to your server. - Your server matches the device data to the earlier click record and returns the referral code.
Pros: Doesn’t rely on user actions; works even if users rename the APK.
Cons: IP-based matching can be inaccurate (e.g., multiple devices on the same network); requires careful handling of device identifiers to avoid privacy issues.
Option 3: Embed Referral Code in APK Filename (Fallback)
Modify your server to generate unique APK filenames per referral (e.g., MyApp_v1.0_referral_12345678_john.apk). After installation:
- The app reads its own APK file name.
- Parses the filename to extract the referral code.
Sample code to get the APK filename:
val packageManager = packageManager val packageInfo = packageManager.getPackageInfo(packageName, 0) val apkPath = packageInfo.applicationInfo.sourceDir val apkFilename = File(apkPath).name // Extract referral code from filename val referralMatch = Regex("referral_(\\d{8}_\\w+)").find(apkFilename) referralMatch?.groups?.get(1)?.value?.let { referralCode -> validateAndRewardReferral(referralCode) }
Pros: No dependency on browsers or server tracking.
Cons: Users might rename the APK, breaking code extraction; requires generating unique APKs per referral.
Once you have the referral code, follow these steps to ensure security and avoid duplicates:
- Prevent reprocessing: Store a flag in
SharedPreferences(e.g.,isReferralProcessed) to ensure the code is only checked once per installation. - Validate with your backend: Send the referral code to your server to confirm
12345678_johnis a valid user. - Issue rewards: If valid, your server adds积分 to the referrer’s account and returns a success message to the app.
- Update app state: Mark the referral as processed in
SharedPreferencesso it doesn’t get checked again on subsequent launches.
- Sign referral codes: To prevent tampering, generate a signed version of the code on your server (e.g.,
referred_by_user=12345678_john&signature=abc123...). Your backend verifies the signature before issuing rewards. - Handle edge cases: Decide how to handle multiple referral links clicked by the same user (e.g., use the most recent one) and build that logic into your server.
- Permissions: For the clipboard method, Android 13+ requires the
READ_CLIPBOARD_IN_BACKGROUNDpermission (declare it in yourAndroidManifest.xml). - User experience: Add a first-launch popup to inform users: "We found a referral from John! You’ll get X points once you complete setup."
内容的提问来源于stack exchange,提问作者Mayank Kumar

