for #321: only send activity page counts for tabs that were activated

This commit is contained in:
groovecoder 2017-04-11 11:36:35 -05:00
parent f2a82cb6be
commit 6282201b2c

View file

@ -283,10 +283,10 @@ const messageHandler = {
browser.idle.onStateChanged.addListener((newState) => { browser.idle.onStateChanged.addListener((newState) => {
browser.tabs.query({}).then(tabs => { browser.tabs.query({}).then(tabs => {
for (let tab of tabs) { // eslint-disable-line prefer-const for (let tab of tabs) { // eslint-disable-line prefer-const
if (newState === "idle" || newState === "locked") { if (newState === "idle") {
tabPageCounter.sendTabCountAndDelete(tab.id, "user-went-idle"); tabPageCounter.sendTabCountAndDelete(tab.id, "user-went-idle");
} else if (newState === "active") { } else if (newState === "active" && tab.active) {
tabPageCounter.initTabCounter(tab, "user-became-active"); tabPageCounter.initTabCounter(tab);
} }
} }
}).catch(e => { }).catch(e => {
@ -349,32 +349,38 @@ const themeManager = {
const tabPageCounter = { const tabPageCounter = {
counters: {}, counters: {},
initTabCounter(tab, why = "user-activated-tab") { initTabCounter(tab) {
// When the user becomes active, if (tab.id in this.counters) {
// we ONLY need to initialize the activity counter if (!("activity" in this.counters[tab.id])) {
if (tab.id in this.counters && why === "user-became-active") { this.counters[tab.id].activity = {
"cookieStoreId": tab.cookieStoreId,
"pageRequests": 0
};
}
if (!("tab" in this.counters[tab.id])) {
this.counters[tab.id].tab = {
"cookieStoreId": tab.cookieStoreId,
"pageRequests": 0
};
}
} else {
this.counters[tab.id] = {};
this.counters[tab.id].tab = {
"cookieStoreId": tab.cookieStoreId,
"pageRequests": 0
};
this.counters[tab.id].activity = { this.counters[tab.id].activity = {
"cookieStoreId": tab.cookieStoreId, "cookieStoreId": tab.cookieStoreId,
"pageRequests": 0 "pageRequests": 0
}; };
return;
} }
if (tab.id in this.counters) {
return;
}
this.counters[tab.id] = {};
this.counters[tab.id].tab = {
"cookieStoreId": tab.cookieStoreId,
"pageRequests": 0
};
this.counters[tab.id].activity = {
"cookieStoreId": tab.cookieStoreId,
"pageRequests": 0
};
}, },
sendTabCountAndDelete(tabId, why = "user-closed-tab") { sendTabCountAndDelete(tabId, why = "user-closed-tab") {
if (why === "user-closed-tab") { if (!(this.counters[tabId])) {
return;
}
if (why === "user-closed-tab" && this.counters[tabId].tab) {
browser.runtime.sendMessage({ browser.runtime.sendMessage({
method: "sendTelemetryPayload", method: "sendTelemetryPayload",
event: "page-requests-completed-per-tab", event: "page-requests-completed-per-tab",
@ -384,7 +390,7 @@ const tabPageCounter = {
// When we send the ping because the user closed the tab, // When we send the ping because the user closed the tab,
// delete both the 'tab' and 'activity' counters // delete both the 'tab' and 'activity' counters
delete this.counters[tabId]; delete this.counters[tabId];
} else if (why === "user-went-idle") { } else if (why === "user-went-idle" && this.counters[tabId].activity) {
browser.runtime.sendMessage({ browser.runtime.sendMessage({
method: "sendTelemetryPayload", method: "sendTelemetryPayload",
event: "page-requests-completed-per-activity", event: "page-requests-completed-per-activity",