Bug 1401404 - Add telemetry for AS content prefs. r?mcomella,francois draft
authorChenxia Liu <liuche@mozilla.com>
Thu, 21 Sep 2017 17:29:27 -0700
changeset 668799 8b8e646c229fabda7e2a4c83af212c8503ad3237
parent 668548 ca7d18dbacbf103d74a3213d8d08a7c3e4def9a2
child 732775 e17f9b80a4efe766d70133ed640f0277b2e88b38
push id81121
push usercliu@mozilla.com
push dateFri, 22 Sep 2017 00:44:45 +0000
reviewersmcomella, francois
bugs1401404
milestone58.0a1
Bug 1401404 - Add telemetry for AS content prefs. r?mcomella,francois MozReview-Commit-ID: 3IiDeghnhgX
mobile/android/base/java/org/mozilla/gecko/activitystream/ActivityStreamTelemetry.java
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamHomeFragment.java
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
mobile/android/docs/activitystreamtelemetry.rst
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/ActivityStreamTelemetry.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/ActivityStreamTelemetry.java
@@ -1,21 +1,24 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.activitystream;
 
+import android.content.SharedPreferences;
+import android.content.res.Resources;
 import android.support.annotation.NonNull;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.R;
+import org.mozilla.gecko.activitystream.homepanel.ActivityStreamPanel;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.activitystream.homepanel.model.TopSite;
 
 import java.util.HashMap;
 
 /**
  * Telemetry constants and an 'extras' builder specific to Activity Stream.
  */
@@ -25,16 +28,17 @@ public class ActivityStreamTelemetry {
         public final static String FX_ACCOUNT_PRESENT = "fx_account_present";
         public final static String ITEM = "item";
         public final static String SOURCE_TYPE = "source_type";
         public final static String SOURCE_SUBTYPE = "source_subtype";
         public final static String ACTION_POSITION = "action_position";
         public final static String COUNT = "count";
         public final static String PAGE_NUMBER = "page_number";
         public final static String INTERACTION = "interaction";
+        public final static String AS_USER_PREFERENCES = "as-user-preferences";
 
         // Values
         public final static String TYPE_TOPSITES = "topsites";
         public final static String TYPE_HIGHLIGHTS = "highlights";
         public final static String TYPE_POCKET = "pocket";
         public final static String SUBTYPE_PINNED = "pinned";
         public final static String SUBTYPE_SUGGESTED = "suggested";
         public final static String SUBTYPE_TOP = "top";
@@ -50,16 +54,50 @@ public class ActivityStreamTelemetry {
         public final static String ITEM_NEW_TAB = "newtab";
         public final static String ITEM_DISMISS = "dismiss";
         public final static String ITEM_DELETE_HISTORY = "delete";
         public final static String INTERACTION_MENU_BUTTON = "menu_button";
         public final static String INTERACTION_LONG_CLICK = "long_click";
     }
 
     /**
+     * AS_USER_PREFERENCES
+     *
+     * NB: Additional pref values should be (unique) powers of 2
+     * We skip 1 and 2 to be consistent with Desktop, because those prefs are not used in Firefox for Android.
+     **/
+    private final static int POCKET_ENABLED_VALUE = 4;
+    private final static int VISITED_ENABLED_VALUE = 8;
+    private final static int BOOKMARKED_ENABLED_VALUE = 16;
+
+    /**
+     * Calculates the bit-packed value of the user's Activity Stream preferences (e.g. enabled/disabled sections).
+     *
+     * @param sharedPreferences SharedPreferences of this profile
+     * @return bit-packed value of the user's AS preferences, which is the sum of the values of the enabled preferences.
+     */
+    public static int getASUserPreferencesValue(final SharedPreferences sharedPreferences, final Resources res) {
+        int bitPackedPrefValue = 0;
+
+        if (sharedPreferences.getBoolean(ActivityStreamPanel.PREF_POCKET_ENABLED, res.getBoolean(R.bool.pref_activitystream_pocket_enabled_default))) {
+            bitPackedPrefValue += POCKET_ENABLED_VALUE;
+        }
+
+        if (sharedPreferences.getBoolean(ActivityStreamPanel.PREF_VISITED_ENABLED, res.getBoolean(R.bool.pref_activitystream_visited_enabled_default))) {
+            bitPackedPrefValue += VISITED_ENABLED_VALUE;
+        }
+
+        if (sharedPreferences.getBoolean(ActivityStreamPanel.PREF_BOOKMARKS_ENABLED, res.getBoolean(R.bool.pref_activitystream_recentbookmarks_enabled_default))) {
+            bitPackedPrefValue += BOOKMARKED_ENABLED_VALUE;
+        }
+
+        return bitPackedPrefValue;
+    }
+
+    /**
      * A helper class used for composing an 'extras' field. It encapsulates a holder of "global"
      * key/value pairs which will be present in every 'extras' constructed by this class, and a
      * static builder which is aware of Activity Stream telemetry needs.
      */
     public final static class Extras {
         private static final HashMap<String, Object> globals = new HashMap<>();
 
         public static void setGlobal(String key, Object value) {
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamHomeFragment.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamHomeFragment.java
@@ -1,29 +1,27 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 package org.mozilla.gecko.activitystream.homepanel;
 
 import android.app.Activity;
-import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
 import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
-import org.mozilla.gecko.activitystream.ActivityStream;
 import org.mozilla.gecko.home.HomeFragment;
 
 /**
  * Simple wrapper around the ActivityStream view that allows embedding as a HomePager panel.
  */
 public class ActivityStreamHomeFragment
         extends HomeFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
     private ActivityStreamPanel activityStreamPanel;
@@ -46,17 +44,17 @@ public class ActivityStreamHomeFragment
 
     @Override
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
         final boolean shouldReload = TextUtils.equals(s, ActivityStreamPanel.PREF_BOOKMARKS_ENABLED)
                 || TextUtils.equals(s, ActivityStreamPanel.PREF_VISITED_ENABLED)
                 || TextUtils.equals(s, ActivityStreamPanel.PREF_POCKET_ENABLED);
 
         if (shouldReload) {
-            activityStreamPanel.reload(getLoaderManager());
+            activityStreamPanel.reload(getLoaderManager(), sharedPreferences, this.getResources());
         }
 
     }
 
     @Override
     protected void load() {
         activityStreamPanel.load(getLoaderManager());
     }
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
@@ -84,16 +84,25 @@ public class ActivityStreamPanel extends
         final Resources resources = getResources();
         desiredTileWidth = resources.getDimensionPixelSize(R.dimen.activity_stream_desired_tile_width);
         tileMargin = resources.getDimensionPixelSize(R.dimen.activity_stream_base_margin);
 
         ActivityStreamTelemetry.Extras.setGlobal(
                 ActivityStreamTelemetry.Contract.FX_ACCOUNT_PRESENT,
                 FirefoxAccounts.firefoxAccountsExist(context)
         );
+
+        updateSharedPreferencesGlobalExtras(sharedPreferences, context.getResources());
+    }
+
+    private void updateSharedPreferencesGlobalExtras(final SharedPreferences sharedPreferences, final Resources res) {
+        ActivityStreamTelemetry.Extras.setGlobal(
+                ActivityStreamTelemetry.Contract.AS_USER_PREFERENCES,
+                ActivityStreamTelemetry.getASUserPreferencesValue(sharedPreferences, res)
+        );
     }
 
     void setOnUrlOpenListeners(HomePager.OnUrlOpenListener onUrlOpenListener, HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
         adapter.setOnUrlOpenListeners(onUrlOpenListener, onUrlOpenInBackgroundListener);
     }
 
     public void load(LoaderManager lm) {
         lm.initLoader(LOADER_ID_TOPSITES, null, new TopSitesCallback());
@@ -108,19 +117,21 @@ public class ActivityStreamPanel extends
     }
 
     public void unload() {
         adapter.swapHighlights(Collections.<Highlight>emptyList());
 
         adapter.swapTopSitesCursor(null);
     }
 
-    public void reload(LoaderManager lm) {
+    public void reload(LoaderManager lm, SharedPreferences sharedPreferences, Resources res) {
         adapter.clearAndInit();
 
+        updateSharedPreferencesGlobalExtras(sharedPreferences, res);
+
         // Destroy loaders so they don't restart loading when returning.
         lm.destroyLoader(LOADER_ID_HIGHLIGHTS);
         lm.destroyLoader(LOADER_ID_POCKET);
 
         load(lm);
     }
 
     @Override
--- a/mobile/android/docs/activitystreamtelemetry.rst
+++ b/mobile/android/docs/activitystreamtelemetry.rst
@@ -13,16 +13,24 @@ Activity Stream events are recorded as p
 
 Global extras
 =============
 A concept of a "global" extra is meant to support recording certain context information with every event that is being sent, regardless of its type.
 
 ``fx_account_present``, values: true, false
 Indicates if Firefox Account is currently enabled.
 
+``as_user_preferences``, values: (bit-packed) value of preferences enabled
+Each preference is assigned a value that is a unique power of 2, and value of as_user_preferences is the sum of all enabled preferences values.
+
+The SharedPreferences preferences measured (with their value) are:
+pref_activitystream_pocket_enabled: 4
+pref_activitystream_visited_enabled: 8
+pref_activitystream_recentbookmarks_enabled: 16
+
 Extra information available for various event types
 ===================================================
 Action position
 ---------------
 Common to most recorded events is the 0-based ``action_position`` extra. For non-menu interactions it
 indicates position of an item being interacted with. For example, click on a third top site of a
 second page will have ``action_position = 6``, given that the first page had 4 top sites.
 
@@ -116,62 +124,66 @@ Possible values for "item" key (names of
 - "newtab" (private tab actions are collapsed into "newtab" telemetry due to our privacy guidelines)
 - "dismiss"
 - "delete"
 
 Full Examples
 =============
 Following examples of events are here to provide a better feel for the overall shape of telemetry data being recorded.
 
-1) User with an active Firefox Account clicked on a menu item for a third highlight ("visited"):
+1) User with an active Firefox Account clicked on a menu item for a third highlight ("visited") [prefs enabled: top-stories, bookmarks, visited] :
     ::
 
         session="activitystream.1"
         event="show.1"
         method="contextmenu"
         extras="{
             'fx_account_present': true,
+            'as_user_preferences': 28,
             'source_type': 'highlights',
             'source_subtype': 'visited',
             'action_position': 2
         }"
 
-2) User with no active Firefox Account clicked on a second highlight (recent bookmark), with total of 7 highlights being displayed:
+2) User with no active Firefox Account clicked on a second highlight (recent bookmark), with total of 7 highlights being displayed [prefs enabled: bookmarks] :
     ::
 
         session="activitystream.1"
         event="loadurl.1"
         method="listitem"
         extras="{
             'fx_account_present': false,
+            'as_user_preferences': 16,
             'source_type': 'highlights',
             'source_subtype': 'bookmarked'
             'action_position': 1,
             'count': 7
         }"
 
-3) User with an active Firefox Account clicked on a third pinned top site:
+3) User with an active Firefox Account clicked on a third pinned top site [prefs enabled: (none)] :
     ::
 
         session="activitystream.1"
         event="loadurl.1"
         method="listitem"
         extras="{
             'fx_account_present': true,
+            'as_user_preferences': 0,
             'source_type': 'topsites',
             'source_subtype': 'pinned',
             'action_position': 2,
             'page_number': 0
         }"
 
-4) User with an active Firefox Account clicked on a "share" context menu item, which was displayed for a regular top site number 6:
+4) User with an active Firefox Account clicked on a "share" context menu item, which was displayed for a regular top site number 6 [prefs enabled: visited, bookmarks] :
     ::
 
         session="activitystream.1"
         event="action.1"
         method="contextmenu"
         extras="{
             'fx_account_present': true,
+            'as_user_preferences': 24,
             'source_type': 'topsites',
             'source_subtype': 'top',
             'item': 'share',
             'action_position': 5
         }"