From c43f83fd46bf91ca18756f1cd00a37d2cf266606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Luba=C5=84ski?= Date: Wed, 11 Dec 2024 10:38:27 +0100 Subject: [PATCH 1/2] fix(telemetry): send events on trailing edge of the frequency --- src/background/telemetry/metrics.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/background/telemetry/metrics.js b/src/background/telemetry/metrics.js index 7b4c9ffa6..4af929502 100644 --- a/src/background/telemetry/metrics.js +++ b/src/background/telemetry/metrics.js @@ -352,7 +352,16 @@ export default class Metrics { _timeToExpired(type, frequency) { if (frequency === 'all') return 0; - const last = this.storage[`${type}_${frequency}`] || 0; + const key = `${type}_${frequency}`; + + // Protect against calling events immediately after install for all frequencies + // They should trigger on the trailing edge of the frequency + if (!this.storage[key]) { + this.storage[key] = Date.now(); + this.saveStorage(this.storage); + } + + const last = this.storage[key]; const frequency_ago = Date.now() - FREQUENCIES[frequency]; return last ? last - frequency_ago : 0; From cd78163c48a7a76db8493702b5839e1468f7dd38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Luba=C5=84ski?= Date: Wed, 11 Dec 2024 11:17:57 +0100 Subject: [PATCH 2/2] Extract Date.now() --- src/background/telemetry/metrics.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/background/telemetry/metrics.js b/src/background/telemetry/metrics.js index 4af929502..184269485 100644 --- a/src/background/telemetry/metrics.js +++ b/src/background/telemetry/metrics.js @@ -353,16 +353,17 @@ export default class Metrics { if (frequency === 'all') return 0; const key = `${type}_${frequency}`; + const now = Date.now(); // Protect against calling events immediately after install for all frequencies // They should trigger on the trailing edge of the frequency if (!this.storage[key]) { - this.storage[key] = Date.now(); + this.storage[key] = now; this.saveStorage(this.storage); } const last = this.storage[key]; - const frequency_ago = Date.now() - FREQUENCIES[frequency]; + const frequency_ago = now - FREQUENCIES[frequency]; return last ? last - frequency_ago : 0; }