Bug 1417473 - Add the docs for hybrid content telemetry. r?chutten,gfritzsche draft
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Wed, 29 Nov 2017 16:37:30 +0100
changeset 712992 8e434595082bf1a54669771d43d07dd5a0fda4b1
parent 712991 8a5a6d1e58cfbd04f5f89d0aa1fae9600bd21fc5
child 744218 9931d1bd2845aeed2893b37f084bc628680a3e5b
push id93518
push useralessio.placitelli@gmail.com
push dateTue, 19 Dec 2017 11:20:25 +0000
reviewerschutten, gfritzsche
bugs1417473
milestone59.0a1
Bug 1417473 - Add the docs for hybrid content telemetry. r?chutten,gfritzsche MozReview-Commit-ID: 2qEiZcNU7SB
toolkit/components/telemetry/docs/collection/hybrid-content.rst
toolkit/components/telemetry/docs/collection/index.rst
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/docs/collection/hybrid-content.rst
@@ -0,0 +1,212 @@
+========================
+Hybrid Content Telemetry
+========================
+
+Hybrid content is web content that is designed to follow the look and feel of the
+Firefox chrome. This can be either a page that ships with Firefox or that can be
+loaded dynamically from our hosted services. Hybrid content telemetry allows these
+pages to submit Telemetry data.
+
+.. important::
+
+    Every new data collection in Firefox (including hybrid content) needs a `data collection review <https://wiki.mozilla.org/Firefox/Data_Collection#Requesting_Approval>`_ from a data collection peer. Just set the feedback? flag for one of the data peers. We try to reply within a business day.
+
+The recorded data will be sent to Mozilla servers by Firefox, if the collection is enabled, with the :doc:`main-ping <../data/main-ping>`.
+
+The API
+=======
+The hybrid content API is available to the web content through the inclusion of the `HybridContentTelemetry-lib.js <https://dxr.mozilla.org/mozilla-central/source/toolkit/components/telemetry/hybrid-content/HybridContentTelemetry-lib.js>`_ library.
+
+The initial implementation of the API allows the registration and the recording of events.
+
+JS API
+------
+Authorized content can use the following functions:
+
+.. code-block:: js
+
+  Mozilla.ContentTelemetry.canUpload();
+  Mozilla.ContentTelemetry.registerEvents(category, eventData);
+  Mozilla.ContentTelemetry.recordEvent(category, method, object, value, extra);
+
+These functions will not throw. If an unsupported operation is performed (e.g. writing to an unkown event) an error will be logged to the browser console if the Telemetry logging :doc:`preferences <../internals/preferences>` are set.
+
+.. note::
+
+    Data collected using this API will always respect the user Telemetry preferences: if a user has chosen to not send Telemetry data to Mozilla servers, Telemetry from hybrid content pages will not be sent as well.
+    Like other Telemetry data, it will still be recorded locally and available through ``about:telemetry``.
+
+``Mozilla.ContentTelemetry.canUpload()``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. code-block:: js
+
+  Mozilla.ContentTelemetry.canUpload();
+
+This function returns true if the browser is allowed to send collected data to Mozilla servers (i.e. ``datareporting.healthreport.uploadEnabled`` is ``true``), false otherwise. See :doc:`preferences <../internals/preferences>`.
+
+.. note::
+
+    The page should use this function to check if it is allowed to collect data. This is only needed in case the Telemetry system is not be being used for collection. If Telemetry is used, then this is taken care of internally by the Telemetry API. The page should not cache the returned value: user can opt in or out from the Data Collection at any time and so the returned value may change.
+
+Example:
+
+.. code-block:: js
+
+  if (Mozilla.ContentTelemetry.canUpload()) {
+    // ... perform the data collection here using another measurement system.
+  }
+
+``Mozilla.ContentTelemetry.registerEvents()``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. code-block:: js
+
+  Mozilla.ContentTelemetry.registerEvents(category, eventData);
+
+Register new dynamic events from the content. This accepts the same parameters and is subject to the same limitation as ``Services.telemetry.registerEvents()``. See the `events` documentation for the definitive reference.
+
+.. note::
+
+    While is possible to call this function anywhere in content and at any time, doing that as soon as the library is loaded (e.g. `window load event <https://developer.mozilla.org/en-US/docs/Web/Events/load>`_) will make sure that the definition will be ready when recording.
+
+The data recorded into events registered with this function will end up in the ``dynamic`` process section of the main ping.
+
+Example:
+
+.. code-block:: js
+
+  Mozilla.ContentTelemetry.registerEvents("page.interaction", {
+    "click": {
+      methods: ["click"],
+      objects: ["red_button", "blue_button"],
+    }
+  });
+  // Now events can be recorded.
+  Mozilla.ContentTelemetry.recordEvent("page.interaction", "click", "red_button");
+
+``Mozilla.ContentTelemetry.recordEvent()``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. code-block:: js
+
+  Mozilla.ContentTelemetry.recordEvent(category, method, object, value, extra);
+
+Record a registered event. This accepts the same parameters and is subject to the same limitation as ``Services.telemetry.recordEvent()``. See the `events` documentation for the definitive reference.
+
+Example:
+
+.. code-block:: js
+
+  Mozilla.ContentTelemetry.recordEvent("ui", "click", "reload-btn");
+  // event: [543345, "ui", "click", "reload-btn"]
+  Mozilla.ContentTelemetry.recordEvent("ui", "search", "search-bar", "google");
+  // event: [89438, "ui", "search", "search-bar", "google"]
+  Mozilla.ContentTelemetry.recordEvent("ui", "completion", "search-bar", "yahoo",
+                                       {"querylen": "7", "results": "23"});
+  // event: [982134, "ui", "completion", "search-bar", "yahoo",
+  //           {"qerylen": "7", "results": "23"}]
+
+Adding content data collection
+==============================
+Telemetry can be sent from web content by:
+
+1. granting the web content's host privileges in the Firefox codebase;
+2. including the ``HybridContentTelemetry-lib.js`` file in the page;
+3. registering the probes after the library is loaded;
+4. using the API to send Telemetry.
+
+Granting the privileges
+-----------------------
+For security/privacy reasons `Mozilla.ContentTelemetry` will only work on a list of allowed secure origins. The list of allowed origins can be found in `browser/app/permissions <https://dxr.mozilla.org/mozilla-central/source/browser/app/permissions>`_ . A host needs to be given the ``hc_telemetry`` permission in order to be whitelisted.
+
+Example:
+
+.. code-block:: csv
+
+  origin  hc_telemetry  1 https://discovery.addons.mozilla.org
+
+Including the library
+---------------------
+To use hybrid content telemetry the relative content JS library needs to be included in the page. We don't have a CDN hosted version that can be readily included in the page. For this reason, each consumer will need to fetch the latest version of the library from `here <https://hg.mozilla.org/mozilla-central/file/tip/toolkit/components/telemetry/hybrid-content/HybridContentTelemetry-lib.js>`_ and add it to the page repository. Then this file can be deployed along with the page.
+
+Example:
+
+.. code-block:: html
+
+  <!DOCTYPE html>
+  <html>
+    <head>
+      <!-- Other head stuff -->
+      <script type="application/javascript" src="HybridContentTelemetry-lib.js"></script>
+    </head>
+    <body> <!-- Other body stuff --> </body>
+  </html>
+
+Registering the probes
+----------------------
+Probe registration can happen at any time after the library is loaded in the page, but registering early enough ensures that the definition is available once a recording attempt is made.
+
+Example:
+
+.. code-block:: html
+
+  <!DOCTYPE html>
+  <html>
+    <head>
+      <!-- Other head stuff -->
+      <script type="application/javascript">
+        window.onload = function() {
+          if (!Mozilla || !Mozilla.ContentTelemetry) {
+            // .. uh-oh, was library loaded? Report the error.
+            return;
+          }
+          // Register the probe.
+          Mozilla.ContentTelemetry.registerEvents("page.interaction", {
+            "click": {
+              methods: ["click"],
+              objects: ["red_button", "blue_button"],
+            }
+          });
+        };
+      </script>
+    </head>
+    <body> <!-- Other body stuff --> </body>
+  </html>
+
+Recording the data
+------------------
+Data recording can happen at any time after a probe has been registrated. The data will be recorded and sent by Firefox if permitted by the Telemetry :doc:`preferences <../internals/preferences>`.
+
+Example:
+
+.. code-block:: html
+
+  <!DOCTYPE html>
+  <html>
+    <head>
+      <!-- Other head stuff -->
+      <script type="application/javascript">
+        function triggerEvent() {
+          if (!Mozilla || !Mozilla.ContentTelemetry) {
+            // .. uh-oh, was library loaded? Report the error.
+            return;
+          }
+          Mozilla.ContentTelemetry.recordEvent("page.interaction", "click", "red_button");
+        };
+      </script>
+    </head>
+    <body>
+      <!-- Other body stuff -->
+      <div id="content">
+        <button id='event-recording' onclick="triggerEvent();">
+          Trigger Recording
+        </button>
+      </div>
+    </body>
+  </html>
+
+Version History
+===============
+
+- Firefox 59: Initial hybrid content telemetry support (`bug 1417473 <https://bugzilla.mozilla.org/show_bug.cgi?id=1417473>`_).
--- a/toolkit/components/telemetry/docs/collection/index.rst
+++ b/toolkit/components/telemetry/docs/collection/index.rst
@@ -18,28 +18,30 @@ The current data collection possibilitie
 * :doc:`events` can record richer data on individual occurrences of specific actions
 * ``TelemetryLog`` allows collecting ordered event entries up to a limit of 1000 entries (note: this does not have supporting analysis tools)
 * :doc:`measuring elapsed time <measuring-time>`
 * :doc:`custom pings <custom-pings>`
 * :doc:`stack capture <stack-capture>` allow recording application call stacks
 * :doc:`Use counters <use-counters>` measure the usage of web platform features
 * :doc:`Experiment annotations <experiments>`
 * :doc:`Remote content uptake <uptake>`
+* :doc:`hybrid content telemety <hybrid-content>` allows recording telemetry from semi-privileged hosted content
 
 .. toctree::
    :maxdepth: 2
    :titlesonly:
    :hidden:
    :glob:
 
    scalars
    histograms
    events
    measuring-time
    custom-pings
    stack-capture
    experiments
    uptake
+   hybrid-content
    *
 
 Browser Usage Telemetry
 ~~~~~~~~~~~~~~~~~~~~~~~
 For more information, see :ref:`browserusagetelemetry`.