androidgoogle-analyticsgoogle-play-servicesinstall-referrer

Custom INSTALL_REFERRER receiver with multiple tracking services


After update google play services to 7.0.3 my custom INSTALL_REFERRER braodcast receiver got broken. I used CampaignTrackingService (which got the update also) to forward the intent to google analytics.

in AndroidManifest.xml

    <receiver
        android:name="myapp.analytics.installReferrer.InstallReferrerBroadcastReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>

And my Custom BroadCastReceiver (and here is the problem):

    public class InstallReferrerBroadcastReceiver extends BroadcastReceiver{

private final static String TAG = InstallReferrerBroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
    // Send to Different Analytics services

    // Send to Google Analytics
    CampaignTrackingService campaignTrackingService = new CampaignTrackingService();
    // I used onHandleIntent() in google-play services 7.0.0, in 7.3.0 its gone
    campaignTrackingService.onHandleIntent(intent);
    }
}

Solution

  • Try extending CampaignTrackingReceiver instead of BroadcastReceiver. Then in your onReceive method call super.onReceive(context, intent); Also you are calling the Service from the Receiver. Analytics provides both the CampaignTrackingService and CampaignTrackingReceiver. When you are providing your own receiver implementation you should pass the call to Analytics CampaignTrackingReceiver.onReceive instead of direct call to the service. Android guarantees that your app process will not be killed before you return from onReceive but the app process might be killed before the service had started and you will loose the campaign. Analytics provided receiver will correctly do the handoff to the service to avoid this problem (it requires partial wake lock permission).