Search code examples
androidgoogle-analyticsbroadcastreceiverreferrals

Android referral removed in analytics v3?


I've spent the last two days trying to find a workaround for this.

I need to pre-config my app depending on the referral and since google play is broadcasting an Install Referrer intent when an app is installed, I created my own receiver for this task. The code for the manifest declaration and the Receiver is:

Manifest declaration:

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

And the simplified code of the CustomReceiver:

public class CustomReceiver extends BroadcastReceiver {

private static final String CAMPAIGN_SOURCE_PARAM = "utm_source";

@Override
public void onReceive(Context context, Intent intent) {

    Log.d("debug", "waking receiver");
    Uri uri = intent.getData();
    getReferrerMapFromUri(uri);

    new CampaignTrackingReceiver().onReceive(context, intent);
}

void getReferrerMapFromUri(Uri uri) {

    MapBuilder paramMap = new MapBuilder();

    // If no URI, return an empty Map.
    if (uri == null) {
        Log.d("debug", "intent null");

        return;
    }

    if (uri.getQueryParameter(CAMPAIGN_SOURCE_PARAM) != null) {

        // MapBuilder.setCampaignParamsFromUrl parses Google Analytics
        // campaign
        // ("UTM") parameters from a string URL into a Map that can be set
        // on
        // the Tracker.
        paramMap.setCampaignParamsFromUrl(uri.toString());

        Log.d("debug", paramMap.get(Fields.CAMPAIGN_SOURCE));

        // If no source parameter, set authority to source and medium to
        // "referral".
    } else if (uri.getAuthority() != null) {

        paramMap.set(Fields.CAMPAIGN_MEDIUM, "referral");
        paramMap.set(Fields.CAMPAIGN_SOURCE, uri.getAuthority());

    }
}

}

That's all. I am sending broadcast install intent with the adb shell command but it's not getting activated at all. I am using google analytics v3.

Thank you in advance!


Solution

  • I've tried this way. And it does work.( I am also using Google Analytics v3)

    First, in manifest :

    <!-- Used for Google Play Store Campaign Measurement-->;
    <service android:name="com.google.analytics.tracking.android.CampaignTrackingService" />
    <receiver android:name="com.google.analytics.tracking.android.CampaignTrackingReceiver" android:exported="true" >
       <intent-filter>
           <action android:name="com.android.vending.INSTALL_REFERRER" />
       </intent-filter>
    </receiver>
    

    Second, add an CustomReceiver extends BroadcastReceiver ( In my case, I just copy and paste all the codes from developers )

    package com.example.testanalytics;
    
    import com.google.analytics.tracking.android.CampaignTrackingReceiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class CustomReceiver extends BroadcastReceiver
    {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // Pass the intent to other receivers.
    
        // When you're done, pass the intent to the Google Analytics receiver.
        new CampaignTrackingReceiver().onReceive(context, intent);
    }
    }
    

    last step: in my activity( which I want to send messages to Google Analytics ) create an Intent and call sendBroadcast( Intent ) as follow:

    Intent it = new Intent("com.android.vending.INSTALL_REFERRER");
    
    it.setPackage("com.example.testanalytics");
    it.putExtra("referrer", "utm_source%3Dyahoo%26utm_medium%3Dbanner+cpc%26utm_term%3Debook%26utm_content%3Dmy_content%26utm_campaign%3Dmy_campaign_name_2");
    
    sendBroadcast(it);
    

    And just get it. I hope this may help you.