Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Using Google Analytics v4 Campaign Measurement

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?

like image 939
Harry Avatar asked Dec 02 '14 06:12

Harry


2 Answers

First, did your package name is "com.example.gatestapp" ? because that what you wrote in the adb command. Second, did you put your "app.service.CustomReceiver" before the "CampaignTrackingReceiver" in the manifest? maybe the intent is intercept by CampaignTrackingReceiver and not by your receiver.

I secceeded to get the refferels in custom broadcast by:

Bundle extras = intent.getExtras();
String referrerString = extras.getString("referrer");
like image 180
yshahak Avatar answered Oct 15 '22 02:10

yshahak


i find you question very useful for replicating that feature.

The issue looks like to be on the name of the package, I followed exactly the same procedure.

android_manifest.xml

<receiver
        android:name="co.tpaga.poc.referer.poc_installreferer.CustomInstallRefererReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>

The adb command is:

echo 'am broadcast \
-a com.android.vending.INSTALL_REFERRER \
-n "co.tpaga.poc.referer.poc_installreferer/co.tpaga.poc.referer.poc_installreferer.CustomInstallRefererReceiver" \
--es "referrer" \
  "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name&tappsi_uuid=04442-123123-213213-2323"; \                                     
exit' | ./adb shell

And the logcat exit is:

03-22 21:58:04.469 22340-22340/co.tpaga.poc.referer.poc_installreferer D/YEES: IT WORKS!!!utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name
03-22 21:58:04.469 22340-22340/co.tpaga.poc.referer.poc_installreferer D/YES: IT WORKS!!
03-22 21:58:04.477 22340-22394/co.tpaga.poc.referer.poc_installreferer D/GAv4: Received installation campaign: content=test_content, keyword=test_term, medium=test_medium, name=test_name, source=test_source
like image 21
Neoecos Avatar answered Oct 15 '22 01:10

Neoecos