Properly tracking install referrals on Play Store

后端 未结 3 1581
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 05:59

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

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-04 06:58

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

提交回复
热议问题