Get Android Google Analytics referrer tag

后端 未结 3 1351
别跟我提以往
别跟我提以往 2020-11-29 02:52

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 th

3条回答
  •  生来不讨喜
    2020-11-29 03:54

    @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 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 getHashMapFromQuery(String query)
                throws UnsupportedEncodingException {
    
            Map query_pairs = new LinkedHashMap();
    
            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:

            
            
    
            
            
                
                    
                
            
    

提交回复
热议问题