Skip to content

Commit

Permalink
Send reload reason and determinive re-enable event
Browse files Browse the repository at this point in the history
  • Loading branch information
everdimension committed Feb 25, 2025
1 parent df4fdea commit cebc51d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ initialize().then((values) => {
});

browser.runtime.onInstalled.addListener(({ reason }) => {
runtimeStore.handleInstalledEvent({ reason });
if (reason === 'install') {
userLifecycleStore.handleRuntimeInstalledEvent();
openOnboarding();
Expand Down
11 changes: 10 additions & 1 deletion src/shared/analytics/analytics.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,16 @@ function trackAppEvents({ account }: { account: Account }) {
// We want to check whether background script got restarted in a way
// that has led to an unexpected logout.
// The browser restart is considered an expected logout.
const { startupEvent } = runtimeStore.getState();
const { startupEvent, installedEvent } = runtimeStore.getState();

const isIntentionalBrowserRestart = Boolean(startupEvent);

const likelyReason = installedEvent
? installedEvent.reason
: isIntentionalBrowserRestart
? 'browser restart'
: 're-enabled by user';

if (isIntentionalBrowserRestart) {
return;
}
Expand All @@ -362,6 +370,7 @@ function trackAppEvents({ account }: { account: Account }) {
request_name: 'background_script_reloaded',
time_to_expiry: sessionExpiry.timeToExpiry,
is_update_from_version: previousVersion,
likely_reason: likelyReason,
})
);
});
Expand Down
13 changes: 13 additions & 0 deletions src/shared/core/runtime-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Store } from 'store-unit';
import type Browser from 'webextension-polyfill';

type State = {
connected: boolean;
Expand All @@ -9,15 +10,27 @@ type State = {
* https://developer.chrome.com/docs/extensions/reference/api/runtime#event-onStartup
*/
startupEvent: null | number;
installedEvent: null | {
reason: Browser.Runtime.OnInstalledReason;
timestamp: number;
};
};

class RuntimeStore extends Store<State> {
handleStartupEvent() {
this.setState((state) => ({ ...state, startupEvent: Date.now() }));
}

handleInstalledEvent(details: { reason: Browser.Runtime.OnInstalledReason }) {
this.setState((state) => ({
...state,
installedEvent: { reason: details.reason, timestamp: Date.now() },
}));
}
}

export const runtimeStore = new RuntimeStore({
connected: true,
startupEvent: null,
installedEvent: null,
});

0 comments on commit cebc51d

Please sign in to comment.