Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fix #2250, make sendEvent conditional on whether the user has opted-o…
Browse files Browse the repository at this point in the history
…ut of Telemetry generally
  • Loading branch information
ianb committed Mar 11, 2017
1 parent 837195e commit b07a29e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
13 changes: 11 additions & 2 deletions addon/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* exported startup */
/* globals Components */
/* eslint-disable no-unused-vars */

const prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);

function startup(data, reason) {
data.webExtension.startup().then((api) => {
Expand All @@ -11,4 +15,9 @@ function shutdown(data, reason) {}
function install(data, reason) {}
function uninstall(data, reason) {}

function handleMessage() {}
function handleMessage(msg, sender, sendReply) {
if (msg && msg.funcName === "getTelemetryPref") {
let enableTelemetry = prefs.getBoolPref("toolkit.telemetry.enabled");
sendReply({type: "success", value: enableTelemetry});
}
}
27 changes: 25 additions & 2 deletions addon/webextension/background/analytics.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
/* globals main, auth, catcher, deviceInfo */
/* globals main, auth, catcher, deviceInfo, browser */

window.analytics = (function () {
let exports = {};

let telemetryPrefKnown = false;
let telemetryPref;

exports.sendEvent = function (action, label, options) {
let eventCategory = "addon";
if (! telemetryPrefKnown) {
console.warn("sendEvent called before we were able to refresh");
return Promise.resolve();
}
if (! telemetryPref) {
console.info(`Cancelled sendEvent ${eventCategory}/${action}/${label || 'none'} ${JSON.stringify(options)}`);
return Promise.resolve();
}
if (typeof label == "object" && (! options)) {
options = label;
label = undefined;
}
options = options || {};
let di = deviceInfo();
return new Promise((resolve, reject) => {
let eventCategory = "addon";
let url = main.getBackend() + "/event";
let req = new XMLHttpRequest();
req.open("POST", url);
Expand Down Expand Up @@ -43,5 +54,17 @@ window.analytics = (function () {
});
};

exports.refreshTelemetryPref = function () {
return browser.runtime.sendMessage({funcName: "getTelemetryPref"}).then((result) => {
telemetryPrefKnown = true;
telemetryPref = result.value;
}, (error) => {
// If there's an error reading the pref, we should assume that we shouldn't send data
telemetryPrefKnown = true;
telemetryPref = false;
throw error;
});
};

return exports;
})();
8 changes: 6 additions & 2 deletions addon/webextension/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ window.main = (function () {

chrome.browserAction.onClicked.addListener(function(tab) {
if(tab.url.match(/about:(newtab|blank)/i)) {
sendEvent("goto-myshots", "about-newtab");
catcher.watchPromise(analytics.refreshTelemetryPref().then(() => {
sendEvent("goto-myshots", "about-newtab");
}));
chrome.tabs.update({url: backend + "/shots"});
} else {
sendEvent("start-shot", "toolbar-pageshot-button");
catcher.watchPromise(analytics.refreshTelemetryPref().then(() => {
sendEvent("start-shot", "toolbar-pageshot-button");
}));
catcher.watchPromise(loadSelector());
}
});
Expand Down
4 changes: 4 additions & 0 deletions docs/METRICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

A summary of the metrics Page Shot will record, and what we're looking for in those metrics.

### Opt-out

The add-on reads the Telemetry opt-out preference (`toolkit.telemetry.enabled`) each time the user presses the Page Shot button. If this preference is false, or if there is any issue trying to fetch the preference, then no data is sent to the Page Shot server.

### Key Metrics

Key metrics of Page Shot are fairly simple:
Expand Down

0 comments on commit b07a29e

Please # to comment.