Get Android Google Analytics Referrer Tag

Get Android Google Analytics referrer tag

I went ahead and published a dead pixel finder app to play with snooping on the intent. For some reason, when I registered two different broadcast receivers (ie com.google.android.apps.analytics.AnalyticsReceiver and my own), I never received it on my own.

So instead, I registered only my own receiver, process the information, and pass it along to Google Analytics. Don't know how kosher this is, but it works. Code follows.

public class ZSGoogleInterceptor extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();

String referrerString = extras.getString("referrer");
// Next line uses my helper function to parse a query (eg "a=b&c=d") into key-value pairs
HashMap<String, String> getParams = Utility.getHashMapFromQuery(referrerString);
String source = getParams.get("utm_campaign");

if (source != null) {
SharedPreferences preferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
Editor preferencesEditor = preferences.edit();
preferencesEditor.putString("ga_campaign", source);
preferencesEditor.commit();
}

// Pass along to google
AnalyticsReceiver receiver = new AnalyticsReceiver();
receiver.onReceive(context, intent);
}

}

Then, when your application is actually launched, you can pull the value back out of the shared preferences and pass it along with user signup or whatever. I used the campaign tag for my purposes, but you can grab any parameters you want out of the referrer string created with this tool.

Google analytics Referrer Campaign

You only need to add the service and the receiver definition to your application manifest. The implementation for both is provided by the Google Play Services library.

Android Google Analytics Getting Referrer

You can implement your own receiver that gets the referral string extra from the INSTALL_REFERRER intent, does some work, and then passes the intent on to the Google Analytics receiver.

Here's an example:

package com.example.app;

import com.google.analytics.tracking.android.CampaignTrackingReceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NotePadReceiver extends BroadcastReceiver {

// The name of the referrer string broadcast by Google Play Store.
private static final String PLAY_STORE_REFERRER_KEY = "referrer";

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

String referrer = intent.getStringExtra(PLAY_STORE_REFERRER_KEY);

// Do something with the referrer.

// When you're done, pass the intent to the Google Analytics Campaign Tracking Receiver.
new CampaignTrackingReceiver().onReceive(context, intent);

}
}

AndroidApplicationRecord and Google Analytics

The process behind the AAR is completely automatic. The AAR contains the package name of the app and nothing else. Android will then search for that package name in the Play Store app. So no way to add anything to that, as far as I can tell.

What you could try instead of sharing an AAR, is sharing an URI record containing a Play store URI with your Google Analytics referrer in it. I don't know much about Google Analytics, but it looks to me like you can catch the referrer upon installation of your app, see e.g Get Android Google Analytics referrer tag and Get referrer after installing app from Android Market.

Yet another approach could be to track the sharing events using Google Analytics in the already installed app when it shares the AAR to another device.

Google Analytics Referrer issue in android

You need to add to your Activity:

    GoogleAnalyticsTracker GATracker = GoogleAnalyticsTracker.getInstance();
GATracker.start("UA-XXXXXX-XX",20, this);

GATracker.trackPageView("Page-to-track");

Refer this for complete setting: Google Analytics SDK for Android



Related Topics



Leave a reply



Submit