Properly tracking install referrals on Play Store
Asked Answered
D

3

15

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.

Demott answered 28/6, 2016 at 11:32 Comment(12)
Have you consider Firebase Dynamic Link? It is better and more reliable.Lie
@Lie I haven't looked into that yet.Demott
Do you interesting? If so, I can post some brief explanation as an answer.Lie
@Lie yeah, sure. I would love that.Demott
Please see my answer below. Please also post the link that you use and not working.Lie
How you are testing, the referral ?Mall
@Lie link for invite.Demott
@Mall Actually, I am not able to test the referrals directly, but backend logs is showing the value of referrer field that I am trying to send from front-end and it is not what I had expected.Demott
Okay I am posting the procedure of testing and mine working code. It might help you :)Mall
@AmitTiwari I have posted mine working implementation. Let me know if you need any more. We are running out of time :)Mall
@AmitTiwari Can you give me the full link that you redirect user to play storeLie
Hi, can you guide me on the javascript part since now i am stuck on how to auto open the application after done installing the apps in play storeDoubletalk
M
8

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 :)

Mall answered 7/7, 2016 at 8:0 Comment(9)
Creating the invite url and extracting data from referrer field is clear. The issue is, when I open my invite url from mobile browser, I need to open the app if it is installed or open the play store, if not installed. For this, I use the intent as given in question. What I want to know is, what changes should I do in the intent so as to pass the referral value to play store app so that I can get it back when the app is installed?Demott
Posted additional clarifications, Let me know if still you have any doubt :)Mall
I have tried this. It works. Tell me what will happen if the link that you posted is put in a href for a button in a promotional email or send to other users via say, Whatsapp? Will this link alone, on opening in mobile, open my app if it is installed or open the Play Store app(not Play Store on Chrome)?Demott
So whatever data you put in your data(e.g. - play.google.com/store/apps/… data>) you will get in onReceive after first launch. So assume your link is play.google.com/store/apps/…. So if user clicks the link, go to play store and downloads it. On the first launch you will get my_test_data in onReceive. And again for testing just pass "my_test_data" after "referrer" tag.Mall
Let us continue this discussion in chat.Mall
done but if we are still getting utm source %20set and same for utm meduim.how can i fix the %20set issue .i am able to get the url params specified in cmd while testing manually. but %20set on playstore ..Please helpCauseway
Can u please explain your problem with example, I would love to help you.Mall
Hi, like @atishnaik, I am also facing the same issue when passing a properly generated link which I got using the tool at developers.google.com/analytics/devguides/collection/android/v4/…. The link I generated was play.google.com/store/apps/… But I am still getting "not_set" in the campaign parameters in the broadcast receiver on installThekla
while checking with add its working and not working from play store.Anhydrous
L
5

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/

Lie answered 6/7, 2016 at 17:8 Comment(0)
E
0

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>
Essary answered 7/7, 2016 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.