44 */
55import { pathToFileURL , URL } from 'url' ;
66import path from 'path' ;
7- import createDebug from 'debug' ;
87import { ipcMain } from 'hadron-ipc' ;
98import { once } from 'events' ;
109import type {
1110 BrowserWindowConstructorOptions ,
1211 FindInPageOptions ,
12+ App ,
1313} from 'electron' ;
1414import { app as electronApp , shell , dialog , BrowserWindow } from 'electron' ;
1515import { enable } from '@electron/remote/main' ;
1616
17+ import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging' ;
1718import COMPASS_ICON from './icon' ;
1819import type { CompassApplication } from './application' ;
1920import preferences from 'compass-preferences-model' ;
@@ -23,7 +24,7 @@ import {
2324 registerMongoDbUrlForBrowserWindow ,
2425} from './auto-connect' ;
2526
26- const debug = createDebug ( 'mongodb-compass:electron:window-manager ') ;
27+ const { track , debug } = createLoggerAndTelemetry ( 'COMPASS-WINDOW-MANAGER ') ;
2728
2829export const EXCLUDED_MONGODB_HOSTS = [
2930 'compass-maps.mongodb.com' ,
@@ -259,6 +260,67 @@ async function onAppReady() {
259260 }
260261}
261262
263+ function trackWindowEvents ( electronApp : App ) {
264+ // Map of [Window id] -> [opening timestamp in milliseconds]
265+ const windowFocusedAt = new Map < number , number > ( ) ;
266+ let sessionStartedAt : number | undefined = undefined ;
267+ let openWindows = 0 ;
268+
269+ electronApp . on ( 'browser-window-created' , ( event , win ) => {
270+ openWindows ++ ;
271+ track ( 'Window Open' , {
272+ number_of_windows_open : openWindows ,
273+ } ) ;
274+
275+ win . once ( 'closed' , ( ) => {
276+ openWindows -- ;
277+ track ( 'Window Closed' , {
278+ number_of_windows_open : openWindows ,
279+ } ) ;
280+ } ) ;
281+ } ) ;
282+
283+ electronApp . on ( 'browser-window-focus' , ( event , win ) => {
284+ const now = Date . now ( ) ;
285+ sessionStartedAt ??= now ;
286+ windowFocusedAt . set ( win . webContents . id , now ) ;
287+ track ( 'Window Focused' , {
288+ number_of_windows_open : openWindows ,
289+ } ) ;
290+ } ) ;
291+
292+ electronApp . on ( 'browser-window-blur' , ( event , win ) => {
293+ if ( win . webContents . isDevToolsFocused ( ) ) {
294+ return ;
295+ }
296+
297+ // causes focus to be emitted after blur, allowing us to track
298+ // when the focus moves from a Compass window to the other
299+ setImmediate ( ( ) => {
300+ const focusAt = windowFocusedAt . get ( win . webContents . id ) ;
301+ const movedToOtherCompassWin = windowFocusedAt . size === 2 ;
302+ windowFocusedAt . delete ( win . webContents . id ) ;
303+
304+ if ( focusAt ) {
305+ const now = Date . now ( ) ;
306+ track ( 'Window Blurred' , {
307+ session_duration_secs : Math . round ( ( now - focusAt ) / 1000 ) ,
308+ focus_to_other_compass_window : movedToOtherCompassWin ,
309+ number_of_windows_open : openWindows ,
310+ } ) ;
311+
312+ if ( sessionStartedAt && ! movedToOtherCompassWin ) {
313+ track ( 'Application Blurred' , {
314+ session_duration_secs : Math . round ( ( now - sessionStartedAt ) / 1000 ) ,
315+ number_of_windows_open : openWindows ,
316+ } ) ;
317+ sessionStartedAt = undefined ;
318+ }
319+ }
320+ } ) ;
321+ } ) ;
322+ }
323+
262324class CompassWindowManager {
263325 private static initPromise : Promise < void > | null = null ;
264326
@@ -277,6 +339,8 @@ class CompassWindowManager {
277339 }
278340 } ) ;
279341
342+ trackWindowEvents ( electronApp ) ;
343+
280344 compassApp . on ( 'show-connect-window' , ( ) => {
281345 showConnectWindow ( compassApp ) ;
282346 } ) ;
0 commit comments