I have implemented Google Analytics Campaign Measurement according to this guide. Then I want to test if everything works good by following this guide.
I have added these on AndroidManifest.xml:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.gms.analytics.globalConfigResource"
android:resource="@xml/global_tracker" />
<!-- Used for Google Play Store Campaign Measurement-->;
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
And putting these on MainActivity.java:
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);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleAnalytics.getInstance(this).getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
// Get tracker.
Tracker t = getTracker(TrackerName.APP_TRACKER);
// Set screen name.
t.setScreenName("Test Track");
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
}
When I run below command:
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.example.gatestapp/com.google.android.gms.analytics.CampaignTrackingReceiver --es "referrer" "utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=testContent&utm_campaign=testCampaign"
I got this response:
Broadcast completed: result=0
and this on my Logcat:
I/GAV4﹕ Thread[GAThread,5,main]: Campaign found: utm_source=testSource
But I don't have any idea how to collect the utm_source into a String, so I can have the "testSource" value.
So, I tried using Custom Receiver:
public class CustomReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
handleIntent(context, intent);
Log.d("YES", "IT WORKS!!");
new CampaignTrackingReceiver().onReceive(context, intent);
}
// Handle the intent data
public void handleIntent(Context context, Intent intent) {
String referrer = intent.getStringExtra("referrer");
Log.d("YEES", "IT WORKS!!!");
}
}
changed my the receiver tag on AndroidManifest.xml to this:
<receiver android:name=".app.service.CustomReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
and change the adb shell command with this:
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.example.gatestapp/.app.service.CustomReceiver --es "referrer" "utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=testContent&utm_campaign=testCampaign"
The broadcast was success but my CustomReceiver catches nothing! None of "YES" or "YEES" on Logcat. Also there is no hit detected on my Google Analytics. Is there anything wrong with my method? How do I collect the referrer extras?
EDIT:
I have successfully collect the referrer extras by using SharedPreferences
. However, when I publish my app on Play Store, the referrer extras seems to be missing. I'm sure that I did everything correctly, as I was using URL Builder. Also, I have tried some app demonstration like the one in this tutorial, but it is not working. My device is not receiving any extras.
Am I doing a wrong approach? Or, did I miss something?