Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Created February 20, 2019 12:51
Show Gist options
  • Save miquelbeltran/578c7e982277d084a305fb0f0c984a0a to your computer and use it in GitHub Desktop.
Save miquelbeltran/578c7e982277d084a305fb0f0c984a0a to your computer and use it in GitHub Desktop.
install referrer data on Android
<!-- Used for Google Play Store Campaign Measurement -->
<receiver
android:name=“.broadcastreceivers.CampaignTrackingReceiver”
android:enabled=“true”
android:exported=“true”>
<intent-filter>
<action android:name=“com.android.vending.INSTALL_REFERRER” />
</intent-filter>
</receiver>
public void onReceive(Context context, Intent intent) {
if (intent != null) {
try {
BaseApp app = ((BaseApp) context.getApplicationContext());
FirebaseAnalytics firebaseAnalytics = app.getFirebaseAnalytics();
mSharedPreferencesApp = context.getSharedPreferences(
context.getPackageName() + “.APP”, Context.MODE_PRIVATE);
int currentVersion = app.getPackageManager()
.getPackageInfo(app.getPackageName(), 0).versionCode;
String referrer = intent.getStringExtra(“referrer”);
String action = intent.getAction();
if (“com.android.vending.INSTALL_REFERRER”.equals(action) && !TextUtils.isEmpty(referrer)) {
mSharedPreferencesApp.edit().putString(“referrer”, referrer).apply();
app.gaTrackEvent(“AppInstall”,
“AppInstall”,
“Playstore install v” + currentVersion + “. Referrer: ” + referrer);
firebaseAnalytics.setUserProperty(“referrer”, referrer);
} else {
mSharedPreferencesApp.edit().putString(“referrer”, “NONE”).apply();
app.gaTrackEvent(“AppInstall”,
“AppInstall”,
“Playstore install v” + currentVersion + “. No referrer.“);
}
} catch (ClassCastException e) {
Log.e(BaseApp.LOGTAG,
“CampaignTrackingReceiver::onReceive: Could not get App from context”, e);
} catch (PackageManager.NameNotFoundException e) {
Log.e(BaseApp.LOGTAG,
“CampaignTrackingReceiver::onReceive: Could not get App version”, e);
} catch (NullPointerException e) {
Log.e(BaseApp.LOGTAG,
“CampaignTrackingReceiver::onReceive: Something was null”, e);
}
}
new CampaignTrackingReceiver().onReceive(context, intent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment