Android Open Source - callmeter Tracking Utils






From Project

Back to project page callmeter.

License

The source code is released under:

GNU General Public License

If you think the Android project callmeter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.ub0r.android.callmeter;
//from   ww  w .j av  a2s. c  om
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;

import android.app.Activity;
import android.content.Context;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;

import de.ub0r.android.logg0r.Log;

/**
 * @author flx
 */
public class TrackingUtils {

    private static final String TAG = "TrackingUtils";

    private static int sEnableAnalytics = -1;
    private static String sTrackingId;

    private TrackingUtils() {
        // hide constructor
    }

    public static void startTracking(final Activity context) {
        if (context == null) {
            return;
        }
        if (sEnableAnalytics < 0) {
            sEnableAnalytics = PreferenceManager.getDefaultSharedPreferences(context)
                    .getBoolean("enable_analytics", true) ? 1 : 0;
        }
        if (sEnableAnalytics == 1) {
            EasyTracker tr = EasyTracker.getInstance(context);
            tr.activityStart(context);
        }
    }

    public static void stopTracking(final Activity context) {
        if (context == null) {
            return;
        }
        if (sEnableAnalytics == 1) {
            EasyTracker.getInstance(context).activityStop(context);
        }
    }


    public static void setDimension(final Context context, final int id, final int value) {
        setDimension(context, id, String.valueOf(value));
    }

    public static void setDimension(final Context context, final int id, final String value) {
        if (sEnableAnalytics != 1) {
            return;
        }
        Tracker tr = getTracker(context);
        if (tr == null) {
            return;
        }
        tr.set(Fields.customDimension(id), value);
    }

    public static void sendView(final Fragment fragment) {
        if (fragment == null) {
            Log.w(TAG, "fragment=null");
            return;
        }
        sendView(fragment, fragment.getClass().getName());
    }

    public static void sendView(final Object context, final String view) {
        Tracker tr = getTracker(context);
        if (tr == null) {
            return;
        }
        tr.send(MapBuilder
                .createAppView()
                .set(Fields.SCREEN_NAME, view)
                .build()
        );
    }

    public static void clearView(final Object context) {
        Tracker tr = getTracker(context);
        if (tr == null) {
            return;
        }
        tr.set(Fields.SCREEN_NAME, null);
    }

    public static void sendEvent(final Object context, final String category, final String action,
            final String label, final Long value) {
        Tracker tr = getTracker(context);
        if (tr == null) {
            return;
        }
        tr.send(MapBuilder.createEvent(category, action, getView(context, label), value).build());
    }

    public static void sendMenu(final Object context, final String label) {
        sendEvent(context, "UX", "menu", label, null);
    }

    public static void sendClick(final Object context, final String label, final Long id) {
        sendEvent(context, "UX", "click", label, id);
    }

    public static void sendLongClick(final Object context, final String label, final Long id) {
        sendEvent(context, "UX", "longClick", label, id);
    }

    private static Tracker getTracker(final Object context) {
        if (context == null) {
            Log.w(TAG, "context=null");
            return null;
        }
        if (context instanceof Tracker) {
            return (Tracker) context;
        } else if (context instanceof Context) {
            return getTrackerFromContext((Context) context);
        } else if (context instanceof Fragment) {
            Activity a = ((Fragment) context).getActivity();
            if (a == null) {
                Log.w(TAG, "fragment's activity=null");
                return null;
            } else {
                return getTrackerFromContext(a);
            }
        } else {
            Log.w(TAG, "invalid context: " + context);
            return null;
        }
    }

    private static String getView(final Object context, final String view) {
        if (context == null) {
            Log.w(TAG, "context=null");
            return view;
        }
        if (context instanceof Tracker) {
            return view;
        } else if (context instanceof Context) {
            return getViewFromContext((Context) context, view);
        } else if (context instanceof Fragment) {
            Activity a = ((Fragment) context).getActivity();
            if (a == null) {
                Log.w(TAG, "fragment.activity=null");
                return view;
            } else {
                return getViewFromContext(a, view);
            }
        } else {
            Log.w(TAG, "invalid context: " + context);
            return view;
        }
    }

    private static Tracker getTrackerFromContext(final Context context) {
        if (sEnableAnalytics < 0) {
            sEnableAnalytics = PreferenceManager.getDefaultSharedPreferences(context)
                    .getBoolean("enable_analytics", true) ? 1 : 0;
        }
        if (sEnableAnalytics == 1) {
            GoogleAnalytics ga = GoogleAnalytics.getInstance(context);
            Tracker tr = ga.getDefaultTracker();
            if (tr == null) {
                if (sTrackingId == null) {
                    sTrackingId = context.getString(R.string.ga_trackingId);
                }
                tr = ga.getTracker(sTrackingId);
            }
            return tr;
        } else {
            return null;
        }
    }

    private static String getViewFromContext(final Context context, final String view) {
        if (view.contains("/")) {
            return view;
        } else {
            return context.getClass().getName() + "/" + view;
        }
    }
}




Java Source Code List

android.preference.DatePreference.java
com.actionbarsherlock.BuildConfig.java
de.ub0r.android.callmeter.Ads.java
de.ub0r.android.callmeter.CallMeter.java
de.ub0r.android.callmeter.TrackingUtils.java
de.ub0r.android.callmeter.data.DataProvider.java
de.ub0r.android.callmeter.data.Device.java
de.ub0r.android.callmeter.data.ExportProvider.java
de.ub0r.android.callmeter.data.LogRunnerReceiver.java
de.ub0r.android.callmeter.data.LogRunnerService.java
de.ub0r.android.callmeter.data.NameCache.java
de.ub0r.android.callmeter.data.NameLoader.java
de.ub0r.android.callmeter.data.RuleMatcher.java
de.ub0r.android.callmeter.data.SysClassNet.java
de.ub0r.android.callmeter.ui.AboutActivity.java
de.ub0r.android.callmeter.ui.AddLogActivity.java
de.ub0r.android.callmeter.ui.AskForPlan.java
de.ub0r.android.callmeter.ui.Common.java
de.ub0r.android.callmeter.ui.HelpActivity.java
de.ub0r.android.callmeter.ui.IntroActivity.java
de.ub0r.android.callmeter.ui.LogsFragment.java
de.ub0r.android.callmeter.ui.PlansFragment.java
de.ub0r.android.callmeter.ui.Plans.java
de.ub0r.android.callmeter.ui.TrackingActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockFragmentActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockPreferenceActivity.java
de.ub0r.android.callmeter.ui.prefs.BillModeListPreference.java
de.ub0r.android.callmeter.ui.prefs.CV2EditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVBillModePreference.java
de.ub0r.android.callmeter.ui.prefs.CVCheckBoxPreference.java
de.ub0r.android.callmeter.ui.prefs.CVDatePreference.java
de.ub0r.android.callmeter.ui.prefs.CVEditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVListPreference.java
de.ub0r.android.callmeter.ui.prefs.HourGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.HourGroups.java
de.ub0r.android.callmeter.ui.prefs.NumberGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.NumberGroups.java
de.ub0r.android.callmeter.ui.prefs.PlanEdit.java
de.ub0r.android.callmeter.ui.prefs.Plans.java
de.ub0r.android.callmeter.ui.prefs.PreferencesImport.java
de.ub0r.android.callmeter.ui.prefs.PreferencesPlain.java
de.ub0r.android.callmeter.ui.prefs.PreferencesRules.java
de.ub0r.android.callmeter.ui.prefs.Preferences.java
de.ub0r.android.callmeter.ui.prefs.RuleEdit.java
de.ub0r.android.callmeter.ui.prefs.Rules.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferencesChild.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferences.java
de.ub0r.android.callmeter.ui.prefs.UpDownPreference.java
de.ub0r.android.callmeter.ui.prefs.UpdateListener.java
de.ub0r.android.callmeter.widget.LogsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.LogsAppWidgetProvider.java
de.ub0r.android.callmeter.widget.StatsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.StatsAppWidgetProvider.java
yuku.ambilwarna.AmbilWarnaDialog.java
yuku.ambilwarna.AmbilWarnaKotak.java