forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendToAnalytics.js
More file actions
1 lines (1 loc) · 5.36 KB
/
Copy pathSendToAnalytics.js
File metadata and controls
1 lines (1 loc) · 5.36 KB
1
define(function(require,exports,module){const Metrics=brackets.getModule("utils/Metrics"),PreferencesManager=brackets.getModule("preferences/PreferencesManager"),PerfUtils=brackets.getModule("utils/PerfUtils"),NodeUtils=brackets.getModule("utils/NodeUtils"),themesPref=PreferencesManager.getExtensionPrefs("themes"),BugsnagPerformance=window.BugsnagPerformance,PLATFORM=Metrics.EVENT_TYPE.PLATFORM,PERFORMANCE=Metrics.EVENT_TYPE.PERFORMANCE,STORAGE=Metrics.EVENT_TYPE.STORAGE;function _emitDeviceTypeMetrics(){brackets.browser.isDeskTop&&Metrics.countEvent(PLATFORM,"device","desktop"),brackets.browser.isMobile&&Metrics.countEvent(PLATFORM,"device","mobile"),brackets.browser.isTablet&&Metrics.countEvent(PLATFORM,"device","tablet")}function _emitMobileMetricsIfPresent(){let platform="none";if(brackets.browser.mobile.isIos)platform="ios";else if(brackets.browser.mobile.isWindows)platform="windows";else{if(!brackets.browser.mobile.isAndroid)return;platform="android"}Metrics.countEvent(PLATFORM,"mobile",platform)}function _emitBrowserMetrics(){brackets.browser.desktop.isChrome&&Metrics.countEvent(PLATFORM,"browser","chrome"),brackets.browser.desktop.isChromeBased&&Metrics.countEvent(PLATFORM,"browser","chromeBased"),brackets.browser.desktop.isEdgeChromium&&Metrics.countEvent(PLATFORM,"browser","EdgeChromium"),brackets.browser.desktop.isFirefox&&Metrics.countEvent(PLATFORM,"browser","firefox"),brackets.browser.desktop.isOpera&&Metrics.countEvent(PLATFORM,"browser","opera"),brackets.browser.desktop.isOperaChromium&&Metrics.countEvent(PLATFORM,"browser","operaChromium")}async function _sendStorageMetrics(){if(navigator.storage&&navigator.storage.estimate){const quota=await navigator.storage.estimate(),percentageUsed=Math.round(quota.usage/quota.quota*100),usedMB=Math.round(quota.usage/1024/1024);console.log(`Web Storage quota used: ${percentageUsed}%, ${usedMB}MB`),Metrics.valueEvent(STORAGE,"browserQuota","percentUsed",percentageUsed),Metrics.valueEvent(STORAGE,"browserQuota","usedMB",usedMB)}}function _getPlatformInfo(){let OS="";return/Windows|Win32|WOW64|Win64/.test(window.navigator.userAgent)?OS="WIN":/Mac/.test(window.navigator.userAgent)?OS="OSX":/Linux|X11/.test(window.navigator.userAgent)&&(OS="LINUX32",/x86_64/.test(window.navigator.appVersion+window.navigator.userAgent)&&(OS="LINUX64")),OS}function sendPlatformMetrics(){Metrics.countEvent(PLATFORM,"os",brackets.platform),Metrics.countEvent(PLATFORM,"userAgent",window.navigator.userAgent),Metrics.countEvent(PLATFORM,"languageOS",brackets.app.language),Metrics.countEvent(PLATFORM,"languageBrackets",brackets.getLocale()),Metrics.countEvent(PLATFORM,"bracketsVersion",brackets.metadata.version),"linux"===Phoenix.platform&&Phoenix.isNativeApp?NodeUtils.getLinuxOSFlavorName().then(flavor=>{flavor?Metrics.countEvent(PLATFORM,"os.flavor",flavor):Metrics.countEvent(PLATFORM,"os.flavor",_getPlatformInfo())}):Metrics.countEvent(PLATFORM,"os.flavor",_getPlatformInfo()),_emitDeviceTypeMetrics(),_emitBrowserMetrics(),_emitMobileMetricsIfPresent(),_sendStorageMetrics()}const envName=window.__TAURI__?"tauri-":window.__ELECTRON__?"electron-":"";let bugsnagPerformanceInited=!1;function _initBugsnagPerformance(){bugsnagPerformanceInited=!0,BugsnagPerformance.start({apiKey:"94ef94f4daf871ca0f2fc912c6d4764d",appVersion:AppConfig.version,releaseStage:window.__IS_NATIVE_SHELL__?`${envName}${AppConfig.config.bugsnagEnv}-${Phoenix.platform}`:AppConfig.config.bugsnagEnv,autoInstrumentRouteChanges:!1,autoInstrumentNetworkRequests:!1,autoInstrumentFullPageLoads:!1})}function _bugsnagPerformance(key,valueMs){if(Metrics.isDisabled()||!BugsnagPerformance||Phoenix.isTestWindow)return;bugsnagPerformanceInited||_initBugsnagPerformance();let activityStartTime=new Date,activityEndTime=new Date(activityStartTime.getTime()+valueMs);BugsnagPerformance.startSpan(key,{startTime:activityStartTime}).end(activityEndTime)}function sendStartupPerformanceMetrics(){const healthReport=PerfUtils.getHealthReport();let labelAppStart="AppStartupTime";Phoenix.firstBoot&&(labelAppStart="FirstBootTime"),Metrics.valueEvent(PERFORMANCE,"startup",labelAppStart,Number(healthReport.AppStartupTime)),_bugsnagPerformance(labelAppStart,Number(healthReport.AppStartupTime)),Metrics.valueEvent(PERFORMANCE,"startup","ModuleDepsResolved",Number(healthReport.ModuleDepsResolved)),_bugsnagPerformance("ModuleDepsResolved",Number(healthReport.ModuleDepsResolved)),Metrics.valueEvent(PERFORMANCE,"startup","PhStore",PhStore._storageBootstrapTime),_bugsnagPerformance("PhStore",PhStore._storageBootstrapTime),Phoenix.isNativeApp&&(Metrics.valueEvent(PERFORMANCE,"startup","tauriBoot",window._tauriBootVars.bootstrapTime),_bugsnagPerformance("tauriBootVars",window._tauriBootVars.bootstrapTime)),window.nodeSetupDonePromise&&window.nodeSetupDonePromise.then(()=>{window.PhNodeEngine&&window.PhNodeEngine._nodeLoadTime&&(Metrics.valueEvent(PERFORMANCE,"startup","nodeBoot",window.PhNodeEngine._nodeLoadTime),_bugsnagPerformance("nodeBoot",window.PhNodeEngine._nodeLoadTime)),Metrics.countEvent(PERFORMANCE,"nodeBoot","success",1)}).catch(_err=>{Metrics.countEvent(PERFORMANCE,"nodeBoot","fail",1)})}function _getCurrentTheme(){return themesPref.get("theme")||"default"}function sendThemesMetrics(){Metrics.countEvent(Metrics.EVENT_TYPE.THEMES,"currentTheme",_getCurrentTheme())}exports.sendPlatformMetrics=sendPlatformMetrics,exports.sendStartupPerformanceMetrics=sendStartupPerformanceMetrics,exports.sendThemesMetrics=sendThemesMetrics});