Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Commit

Permalink
Fix #157: Add Firefox UI telemetry probes
Browse files Browse the repository at this point in the history
* Add `visit_supported_site` and `hide_toolbar_button` probes.
  * `hide_toolbar_button` required adding a new experimental API, `customizeUI`, which allows the extension to be notified when the Firefox CustomizeUI module detects the `onWidgetRemoved` event. This event fires any time a widget is removed from the chrome, including browserAction buttons. The widget is identified by a widgetId.
* Update METRICS.md to move `uninstall` probe to Appendix, since it is handled by the Addons Manager's event telemetry already.
  • Loading branch information
biancadanforth committed Oct 16, 2018
1 parent 502c6c2 commit 121b8d1
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 12 deletions.
56 changes: 46 additions & 10 deletions docs/METRICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,7 @@ Fired when the user clicks an undo button in a Product Card in the browserAction

### `uninstall`

Fired when the user uninstalls the extension.

#### Payload properties

- `methods`: String
- `'uninstall'`
- `objects`: String
- `'uninstall'`
- `extra_keys`: Object
- `'tracked_prods'`
See Appendix A.

### `hide_toolbar_button`

Expand Down Expand Up @@ -381,3 +372,48 @@ No telemetry will be sent from the extension in the following additional cases:
- The user is in a [Private Browsing](https://support.mozilla.org/en-US/kb/private-browsing-use-firefox-without-history?redirectlocale=en-US&redirectslug=Private+Browsing) window
- Preference: `browser.privatebrowsing.autostart`
- [`windows.Window`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows/Window) property: `window.incognito`


## Appendices

### Appendix A: `uninstall`

Fired when the user uninstalls the extension.

This event, along with all other add-on lifecycle events, is recorded by the Addons Manager's event telemetry in Firefox. It will exist as part of the `main` ping under `payload.processes.parent.events` as an array in the `events` array. This event will be fired under the `addonsManager` telemetry category.

#### Sample Ping

Note: This is a sample ping. The exact value for the extension ID may differ, though the other values are correct.

```javascript
{
"type": "main",
// ...
"payload": {
// ...
"processes": {
// ...
"parent": {
// ...
"events": [
[
9792,
"addonsManager",
"uninstall",
"extension",
"[email protected]", // the extension ID
{
"source": "testpilot"
// ...
}
]
]
}
// ...
}
// ...
}
// ...
}
```
25 changes: 23 additions & 2 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,42 @@ import {handleWebRequest, updatePrices} from 'commerce/background/price_updates'
import store from 'commerce/state';
import {checkMigrations} from 'commerce/state/migrations';
import {loadStateFromStorage} from 'commerce/state/sync';
import {registerEvents} from 'commerce/background/telemetry';
import {registerEvents, recordEvent, getBadgeType} from 'commerce/background/telemetry';

(async function main() {
registerEvents();

// Record `hide_toolbar_button` event
browser.customizableUI.onWidgetRemoved.addListener(async (widgetId) => {
// Get tab id for current tab in current window
const windowId = (await browser.windows.getCurrent()).id;
const tabId = (await browser.tabs.query({
windowId,
active: true,
}))[0].id;
const addonId = (await browser.management.getSelf()).id;
// widgetId replaces '@' and '.' in the addonId with _
const modifiedAddonId = addonId.replace(/[@.+]/g, '_');
if (`${modifiedAddonId}-browser-action` === widgetId) {
recordEvent('hide_toolbar_button', 'toolbar_button', null, {
badge_type: await getBadgeType(tabId),
});
}
});

// Set browser action default badge color, which can't be set via manifest
browser.browserAction.setBadgeBackgroundColor({
color: await config.get('badgeAlertBackground'),
});

// Setup product extraction listener
// Setup product extraction and `visit_supported_site` probe listener
browser.runtime.onMessage.addListener((message, sender) => {
if (message.from === 'content' && message.subject === 'ready') {
handleExtractedProductData(message.extractedProduct, sender);
}
if (message.from === 'content' && message.subject === 'visit_supported_site') {
recordEvent('visit_supported_site', 'supported_site');
}
});

// Setup config listener; returns for onMessage listeners can't be consistent
Expand Down
31 changes: 31 additions & 0 deletions src/experiment_apis/customizableUI/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* 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/. */
/* global ChromeUtils ExtensionAPI ExtensionCommon */

this.customizableUI = class extends ExtensionAPI {
getAPI(context) {
ChromeUtils.import('resource://gre/modules/ExtensionCommon.jsm');
const {EventManager} = ExtensionCommon;
const {CustomizableUI} = ChromeUtils.import('resource:///modules/CustomizableUI.jsm', {});
return {
customizableUI: {
onWidgetRemoved: new EventManager(
context,
'customizableUI.onWidgetRemoved',
(fire) => {
const toolbarButton = {
onWidgetRemoved(widgetId) {
fire.async(widgetId);
},
};
CustomizableUI.addListener(toolbarButton);
return () => {
CustomizableUI.removeListener(toolbarButton);
};
},
).api(),
},
};
}
};
19 changes: 19 additions & 0 deletions src/experiment_apis/customizableUI/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"namespace": "customizableUI",
"events": [
{
"name": "onWidgetRemoved",
"type": "function",
"description": "Description of the event",
"parameters": [
{
"name": "widgetId",
"description": "Description of the first callback parameter",
"type": "string"
}
]
}
]
}
]
9 changes: 9 additions & 0 deletions src/extraction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ async function attemptExtraction() {
return;
}

// Let background script know the user visited a supported site, so that it
// can record the event.
if (!isBackgroundUpdate) {
await browser.runtime.sendMessage({
from: 'content',
subject: 'visit_supported_site',
});
}

// Extract immediately, and again if the readyState changes.
attemptExtraction();
document.addEventListener('readystatechange', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
"script": "experiment_apis/shoppingPrefs/api.js",
"paths": [["shoppingPrefs"]]
}
},
"customizableUI": {
"schema": "experiment_apis/customizableUI/schema.json",
"parent": {
"scopes": ["addon_parent"],
"script": "experiment_apis/customizableUI/api.js",
"paths": [["customizableUI"]]
}
}
}
}

0 comments on commit 121b8d1

Please sign in to comment.