Home » Android » Can we have INSTALL_REFERRER two times in a single Android app?

Can we have INSTALL_REFERRER two times in a single Android app?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on an Android application where I am using INSTALL_REFERRER to know source of installation of my app. For this, I am using first Google Analytics Install_Referrer intent filter and one of my own custom ones.

Is it possible to have two broadcast receiver entries with an intent filter com.android.vending.INSTALL_REFERRER in Android?

Google Analytics Receiver

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

My Custom Broadcast Receiver.

<receiver
    android:name="com.xyz.broadcastreceiver.ReferrerCatcher"
    android:exported="true" >
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>
How to&Answers:

No we can’t have two Install Referrer broadcast receiver. For this I created my own custom broadcast receiver to achieve Google Analytics Source tracking and my own stuff.

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

In the ReferrerCatcher broadcast receiver

public class ReferrerCatcher extends BroadcastReceiver {
    public static final String TAG = InstallReceiver.class.getCanonicalName();

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

        // Google Analytics
        new CampaignTrackingReceiver().onReceive(context, intent);

        Log.d(TAG, intent.getExtras().getString("referrer");
        // my stuff here 
    }
}

Answer:

I’ve been working in a similar application, and after doing some research I found that we could just remove Google Analytics Receiver (as it does nothing), and use your own custom receiver instead. And of course you could still have your app send data to Google Analytics by implementing below code:

public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.
    GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
    ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker("UA-xxxxxxxx-1")
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
                : analytics.newTracker("UA-xxxxxxxx-1");
        mTrackers.put(trackerId, t);
    } return mTrackers.get(trackerId);
}

// Get tracker.
Tracker t = getTracker(TrackerName.APP_TRACKER);

// Set screen name.
t.setScreenName("some string");

// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());

EDIT:

Below code shows how I declare the broadcast receiver in my AndroidManifest.xml:

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