How to get referrer using google track in android?

I want to implement setting the link for the referrer and want to specify the referrer parameter and store it in the back end database, I saw many examples or questions, for example, how to Get the Google Analytics referrer tag or tracking the Google Analytics campaign in Google Analytics does not appear , but don’t get the way I created links with and tried the code

package SimpleDemo.ReferralTrack; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.HashMap; import java.util.Map; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; public class ReferralReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Workaround for Android security issue: http://code.google.com/p/android/issues/detail?id=16006 try { final Bundle extras = intent.getExtras(); if (extras != null) { extras.containsKey(null); } } catch (final Exception e) { return; } Map<String, String> referralParams = new HashMap<String, String>(); // Return if this is not the right intent. if (! intent.getAction().equals("com.android.vending.INSTALL_REFERRER")) { //$NON-NLS-1$ return; } String referrer = intent.getStringExtra("referrer"); //$NON-NLS-1$ if( referrer == null || referrer.length() == 0) { return; } try { // Remove any url encoding referrer = URLDecoder.decode(referrer, "x-www-form-urlencoded"); //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { return; } // Parse the query string, extracting the relevant data String[] params = referrer.split("&"); // $NON-NLS-1$ for (String param : params) { String[] pair = param.split("="); // $NON-NLS-1$ referralParams.put(pair[0], pair[1]); } ReferralReceiver.storeReferralParams(context, referralParams); } private final static String[] EXPECTED_PARAMETERS = { "utm_source", "utm_medium", "utm_term", "utm_content", "utm_campaign" }; private final static String PREFS_FILE_NAME = "ReferralParamsFile"; /* * Stores the referral parameters in the app sharedPreferences. * Rewrite this function and retrieveReferralParams() if a * different storage mechanism is preferred. */ public static void storeReferralParams(Context context, Map<String, String> params) { SharedPreferences storage = context.getSharedPreferences(ReferralReceiver.PREFS_FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = storage.edit(); for(String key : ReferralReceiver.EXPECTED_PARAMETERS) { String value = params.get(key); if(value != null) { editor.putString(key, value); } } editor.commit(); } /* * Returns a map with the Market Referral parameters pulled from the sharedPreferences. */ public static Map<String, String> retrieveReferralParams(Context context) { HashMap<String, String> params = new HashMap<String, String>(); SharedPreferences storage = context.getSharedPreferences(ReferralReceiver.PREFS_FILE_NAME, Context.MODE_PRIVATE); for(String key : ReferralReceiver.EXPECTED_PARAMETERS) { String value = storage.getString(key, null); if(value != null) { params.put(key, value); } } return params; } } 

After that I tried in my activity

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(DemoActivity.this); String referrers1 =preferences.getString("ga_campaign", "0"); Map<String, String> retrieveReferralParams=ReferralReceiver.retrieveReferralParams(DemoActivity.this); String referrers2= retrieveReferralParams.get("utm_source"); String referrers3= retrieveReferralParams.get("utm_medium"); String referrers4= retrieveReferralParams.get("utm_term"); String referrers5= retrieveReferralParams.get("utm_content"); String referrers6= retrieveReferralParams.get("utm_campaign"); tv.setText(referrers1+" "+referrers2+" "+referrers3+" "+referrers4+" "+referrers5+" "+referrers6+" "); 

when you press a button, but do not get the desired result

I need something like

 "https://play.google.com/store/apps/details?id=com.lifestreet.android.TestInstallationIntent&referrer=bb%3DAAAAAAAAAA&feature=search_result%22" Ans referrer=bb 

any help I really liked Thanks in advance.

+5
android tracking
May 03 '12 at
source share
3 answers

Not sure if Google allows you to send arbitrary information. Try using a generator to create a URL.

https://developers.google.com/analytics/devguides/collection/android/devguide#google-play-builder

+1
Jun 12 '12 at 7:22
source share
β€” -

I had a similar problem. This was found to be a life-cycle problem: the on_ceive receivers of install_referrer are called AFTER my onResume () application in the SAME main thread, so any attempt to read the referrer file during onResume () fails. Here is the logarithm to prove this, it was 100% reproducible over and over on two devices using Android 4.2.1 and 4.4.2:

Firstly, the play store passes the referrer to the package in a separate (storage) process:

 11-04 14:17:51.558: D/Finsky(1737): [1] ReferrerRebroadcaster.doBroadcastInstallReferrer: Delivered referrer for com.xxx.demo 

OnResume () application - there is still no activation of broadcast receivers! S

 11-04 14:17:51.888: D/XXX Main Activity(22730): onResume 

The application is trying to read the referrer (which the recipients should have saved using getSharedPreferences.putString):

 11-04 14:17:51.958: I/XXX(22730): Extracted install referrer: 

Only now, the recipients are called in the main thread and will soon try to write the referrer to a file:

 11-04 14:17:51.918: I/XXX(22730): Received install referrer: abcdefg 

As you can see, onResume () does not have the ability to actually read the file, so extraction does not give anything. However, if I close the application and open it again, onResume will now be able to find the file, and the referrer line will be processed, but not on the first start :)

Hope this helps!

0
Nov 04 '14 at 13:35
source share

Google Analytics uses arbitrary constants in its SDK. for campaing_source is & cs.

0
Feb 03 '15 at 17:09
source share



All Articles