0

How to use mixpanel analytics for referral tracking in android. I googled it , im not able to get any proper answer.

andinrajesh
  • 575
  • 2
  • 9
  • 24

1 Answers1

3

First u have to write a broadcast receiver for receiving the referral intent

public class ReferalIntentReciever extends BroadcastReceiver {
    public static MixpanelAPI mixpanel;

    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        mixpanel = MixpanelAPI.getInstance(context, "YOUR MIXPANEL TOKEN");

        // TODO Auto-generated method stub

        String referrerString = intent.getStringExtra("referrer");
       //sending to mixpanel
        try {
            JSONObject props = new JSONObject();
            props.put("utm_source", splitQuery(referrerString)
                    .get("utm_source"));
            props.put("utm_medium", splitQuery(referrerString)
                    .get("utm_medium"));
            if (splitQuery(referrerString).get("utm_campaign") != null) {
                props.put("utm_campaign",
                        splitQuery(referrerString).get("utm_campaign"));
            }
            mixpanel.track("Referral Campaign", props);
            mixpanel.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //getting each parameter 

    public static Map<String, String> splitQuery(String url)
            throws UnsupportedEncodingException {
        Map<String, String> query_pairs = new LinkedHashMap<String, String>();
        String[] pairs = url.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"),
                    URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }
        return query_pairs;
    }
}

and set the receiver in the manifest

<receiver
            android:name=".ReferalIntentReciever"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver> 

also check this Google play campaign tracking without google analytics implementation android

Community
  • 1
  • 1
Sishin
  • 2,001
  • 1
  • 17
  • 22
  • how to attach the referral code in invite message. – andinrajesh Sep 25 '14 at 08:25
  • add the referral parameter to the referral URL https://play.google.com/store/apps/details?id=com.example.app &referrer=utm_source%3Dgoogle %26utm_medium%3Dcpc %26utm_term%3Drunning%252Bshoes %26utm_content%3DdisplayAd1 %26utm_campaign%3Dshoe%252Bcampaign – Sishin Sep 25 '14 at 10:51
  • utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name" – Sishin Sep 25 '14 at 10:56