14

We're planning to use Google Analytics to track ad click-through referrals, through the Android Market, to our application.

According to the Google Documentation the referrer tag comes through via an intent, and is automatically recorded by the Google Analytics library.

That's great, but we need to extract that referral tag for our own internal analytics. The documentation is shy on details about how to grab it out of the initial launch intent, and instructions on how to simulate this before going live.

Does anyone have experience with this?

Dave Sims
  • 4,958
  • 3
  • 22
  • 26
DougW
  • 25,384
  • 18
  • 76
  • 106

3 Answers3

25

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.

Ed Marty
  • 39,011
  • 19
  • 96
  • 153
DougW
  • 25,384
  • 18
  • 76
  • 106
  • 4
    The reason more than one receiver doesn't work is that you can't register more than one receiver for the same intent filter. This is the point of the confusing documentation here -- http://code.google.com/apis/analytics/docs/mobile/android.html#google-play-tracking -- that appears to say you can't have more than one of *any* kind of BroadcastReceiver in an application, which of course is not true. – Dave Sims Mar 19 '12 at 21:53
  • @DaveSims - Yeah, they added that note long after I made this post. Not particularly clear though, I agree. – DougW Mar 20 '12 at 23:45
  • @DougW my que is how i can i get from this link "http://market.android.com/details?id=com.lifestreet.android.TestInstallationIntent&referrer=bb%3DAAAAAAAAAA&feature=search_result" referre=bb suppose i have add in menifest code four lines of default google of this com.google.android.apps.analytics.AnalyticsReceiver i need any extra in my code – Khan May 03 '12 at 05:05
  • @Khan - Hey Khan, I'd suggest you ask this as a new question. It's a bit to complicated to answer in the comments here. – DougW May 08 '12 at 22:05
  • @DougW i got refferer code if i open the link with play store rather than browser and my question is also at http://stackoverflow.com/questions/10431018/how-to-get-referrer-using-google-track-in-android – Khan May 09 '12 at 04:16
  • Not able to find any reference for [Utility.getHashMapFromQuery()] from code above. – AndyW May 30 '14 at 16:31
  • @DougW Hey Doug, I was gone through this but not getting callback in onreceive() method. Did exactly same process like you have done. Can you please tell me, if any changes there. – Pratik Dasa Apr 15 '15 at 13:37
8

@DougW 's answer updated for Analytics SDK 4

https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

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

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by dave on 15-05-05.
 */
public class ReferrerReceiver extends BroadcastReceiver {

    public static final String REFERRER = "REF";

    public static final String UTM_CAMPAIGN = "utm_campaign";
    public static final String UTM_SOURCE = "utm_source";
    public static final String UTM_MEDIUM = "utm_medium";
    public static final String UTM_TERM = "utm_term";
    public static final String UTM_CONTENT = "utm_content";

    private final String[] sources = {
            UTM_CAMPAIGN, UTM_SOURCE, UTM_MEDIUM, UTM_TERM, UTM_CONTENT
    };

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

        Bundle extras = intent.getExtras();

        String referrerString = extras.getString("referrer");

        try {
            Map<String, String> getParams = getHashMapFromQuery(referrerString);

            SharedPreferences preferences = context
                    .getSharedPreferences(REFERRER, Context.MODE_PRIVATE);

            SharedPreferences.Editor preferencesEditor = preferences.edit();

            for (String sourceType : sources) {
                String source = getParams.get(sourceType);

                if (source != null) {

                    preferencesEditor.putString(sourceType, source);

                }
            }

            preferencesEditor.commit();
        } catch (UnsupportedEncodingException e) {

            Log.e("Referrer Error", e.getMessage());
        } finally {

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



    }

    public static Map<String, String> getHashMapFromQuery(String query)
            throws UnsupportedEncodingException {

        Map<String, String> query_pairs = new LinkedHashMap<String, String>();

        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"),
                    URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }
        return query_pairs;
    }

}

In you manifest file:

        <service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
        <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver" />

        <!-- Make sure this points to the location of Referrer Receiver in your package -->
        <receiver android:name=".ReferrerReceiver" android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>
Dave Thomas
  • 2,918
  • 2
  • 27
  • 40
4

Check at:

Get referrer after installing app from Android Market

for the solutions.

Tobia

Community
  • 1
  • 1
Tobia
  • 647
  • 4
  • 3