问题
I have a simple task: I want to track the referral id of an app install and pass it to backend.
What I did: I created a link with an extra parameter referrer
and appended it to the invite link. When it is opened, the javascript
detects if the browser is an Android mobile browser and then prepares an intent
and issues a redirect to that intent. While preparing the intent, referrer
field is extracted from the url and appended to the intent
like this:
intent://scan/#Intent;scheme=com.example.android;package=com.example.android&referrer=4;end
And here is my code for BroadCastReceiver
:
public class InstallReferrerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TinyDB tinyDB = new TinyDB(context);
String referrer = intent.getStringExtra("referrer");
tinyDB.putString(AppConstants.REFERRAL_ID, referrer);
tinyDB.putBoolean(AppConstants.REFERRAL_SENT, false);
}
}
So, what I expect to get here as the value of referrer
is 4
based on the above intent
. But the value that I am getting is this String utm_source=google-play&utm_medium=organic
What am I doing wrong and how can I fix it to get the correct value for referrer
field?
Edit
I don't have any issues in creating the url or extracting values from referrer
field once the app is installed.
Once the invite link is clicked through any button click or opened directly in the mobile browser, I use the above to "either open the app if it is already installed or open the app's page on Play Store app for users to install it".
The issue is, how should I pass the value of referrer field from the invite link to the Play Store app through the above intent so that the Play Store receives this value and passes it to the app when it is installed.
回答1:
You need to test it properly, I am posting mine use case, hope it will solve your problem :)
Refferal URL -
https://play.google.com/store/apps/details?id=com.mypackage&referrer=utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1
Code to receive refferal -
public static final String KEY_UTM_SOURCE = "utm_source";
public static final String KEY_UTM_CONTENT = "utm_content";
public static final String KEY_UTM_CAMPAIGN = "utm_campaign";
public void onReceive(Context context, Intent intent) {
Utils.log("Referral Received");
try {
String referrer = intent.getStringExtra("referrer");
if (referrer != null && !referrer.equals("")) {
Utils.log("Referral Received - " + referrer);
String[] referrerParts = referrer.split("&");
String utmSource = getData(KEY_UTM_SOURCE, referrerParts);
String utmContent = getData(KEY_UTM_CONTENT, referrerParts);
String utmCampaign = getData(KEY_UTM_CAMPAIGN, referrerParts);
if (utmSource != null && utmSource.equals("mobisoc")) {
sendLogToMobisocServer(context, utmContent);
} else if (utmSource != null && utmSource.equals("app_share")) {
RawStorageProvider.getInstance(context).dumpDataToStorage(RaghuKakaConstants.REFFERAL_FOR, utmContent);
}
updateRKServerForReferral(context, utmSource, utmCampaign, utmContent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String getData(String key, String[] allData) {
for (String selected : allData)
if (selected.contains(key)) {
return selected.split("=")[1];
}
return "";
}
Now the most important part testing. You can test the referral locally. Just you need to attach your phone, open the shell prompt by using adb shell
. And broadcast the referral data.
Here are the command sequence example -
C:\Users\Neo\Desktop>adb shell
$ am broadcast -a com.android.vending.INSTALL_REFERRER -n com.mypackage/<className of your ReferralReceiver with package> --es "referrer" "utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1"
Additional -
https://play.google.com/store/apps/details?id=com.mypackage&referrer=utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1
Just see my link. If user will go to the playstore via that link, and install the app. Then first time when the app will launch, your onReceive method will be fired automatically, and you will get all the data after referrer=.
Broadcast -
$ am broadcast -a com.android.vending.INSTALL_REFERRER -n com.mypackage/<className of your ReferralReceiver with package> --es "referrer" "utm_source%3Dmobisoc%26utm_content%3D{transaction_id}%26utm_campaign%3D1"
For testing it you no need to publish your app on playstore, Just put a debug point on first point of onReceive, launch in debug mode, and fire the command sequences I have posted, you will get all the data after "referrer" tag. So by this you can decide what data you need to add while creating the referrer link.
Let me know in case of more clarification you need :)
回答2:
It is better and more reliable to track referrer via Firebase Dynamic Link.
Below this how it work.
https://domain/?link=your_deep_link&apn=package_name[&amv=minimum_version][&ad=1][&al=android_link][&afl=fallback_link]
Here's the example of link after fill in the parameters.
https://example.app.goo.gl/?link=https://www.example.com/someresource&apn=com.example.android&amv=3&al=exampleapp://someresource&ibi=com.example.ios&isi=1234567&ius=exampleapp
Of course, you can shorten the link to something like https://example.app.goo.gl/abcde
directly at Firebase console. It will take only few minutes to setup the Dynamic Link.
Then in the Android app on your main Activity
you can call AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, false)
to retrieve link information.
More information can be found here https://firebase.google.com/docs/dynamic-links/
回答3:
I have used utm tagging
you can see full source at https://github.com/dheeraj9198/Utm-Test
I am providing the basic code
public class CustomCampaignTrackingReceiver extends BroadcastReceiver {
private static final String TAG = CustomCampaignTrackingReceiver.class.getSimpleName();
private static final Logger LOGGER = LoggerFactory.getLogger(TAG);
private static final Marker MARKER = MarkerFactory.getMarker(TAG);
@Override
public void onReceive(Context context,final Intent intentx) {
LOGGER.info(MARKER, "on Receive called");
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
try {
for (String key : intentx.getExtras().keySet()) {
try {
LOGGER.info(MARKER, key + " => " + String.valueOf(intentx.getExtras().get(key)));
} catch (Exception e) {
LOGGER.error(MARKER, "caught exception in on key retrieval ", e);
}
}
} catch (Exception e) {
LOGGER.error(MARKER, "caught exception in key loop ", e);
}
}
});
executorService.shutdown();
}
}
--------------------------Manifest---------------------------------------
<receiver
android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<receiver
android:name=".receivers.CustomCampaignTrackingReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
来源:https://stackoverflow.com/questions/38075072/properly-tracking-install-referrals-on-play-store