Skip to content

Instantly share code, notes, and snippets.

@gerocha
Created June 22, 2018 19:10
Show Gist options
  • Save gerocha/27ef05e48238d1dcc3cc9b9463d01fbb to your computer and use it in GitHub Desktop.
Save gerocha/27ef05e48238d1dcc3cc9b9463d01fbb to your computer and use it in GitHub Desktop.
package com.<YOUR-APP>;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.util.HashMap;
import com.google.android.gms.analytics.CampaignTrackingReceiver;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import android.util.Log;
public class CustomReceiver extends BroadcastReceiver {
HashMap<String, Tracker> mTrackers = new HashMap<String, Tracker>();
synchronized Tracker getTracker(String trackerId, Context context) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);
analytics.setLocalDispatchPeriod(20);
Tracker t = analytics.newTracker(trackerId);
t.enableExceptionReporting(true);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
@Override
public void onReceive(Context context, Intent intent) {
handleIntent(context, intent);
new CampaignTrackingReceiver().onReceive(context, intent);
}
// Handle the intent data
public void handleIntent(Context context, Intent intent) {
String referrer = intent.getStringExtra("referrer");
Tracker t = getTracker("UA-XXXXX-XXXX", context);
t.setScreenName("Init With Campaign");
t.send(new HitBuilders.ScreenViewBuilder()
.setCampaignParamsFromUrl(referrer)
.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment