Home
%3CLINGO-SUB%20id%3D%22lingo-sub-802091%22%20slang%3D%22en-US%22%3EAdd%20telemetry%20to%20your%20Electron%20application%20with%20Application%20Insights%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-802091%22%20slang%3D%22en-US%22%3E%3CP%3EEven%20if%20sometimes%20developers%20focus%20only%20on%20the%20CI%2FCD%20or%20automatic%20testing%20aspects%2C%20telemetry%20is%20one%20of%20the%20critical%20components%20to%20adopt%20a%20successful%20DevOps%20culture%20for%20your%20projects.%3C%2FP%3E%0A%3CP%3EBeing%20able%20to%20quickly%20build%20and%20deploy%20new%20versions%20of%20your%20application%2C%20in%20fact%2C%20is%20much%20less%20effective%20if%20you%20rely%20only%20on%20the%20willing%20of%20your%20users%20to%20report%20problems%20and%20issues.%20You%20must%20identify%20problems%20fast%2C%20before%20they%20start%20to%20affect%20all%20your%20users%2C%20and%20this%20is%20where%20telemetry%2C%20crash%20logging%20and%20analytics%20come%20into%20play.%3C%2FP%3E%0A%3CP%3ERecently%20I%20helped%20a%20customer%20in%20integrating%20telemetry%20inside%20an%20Electron%20application%20and%20I%20faced%20some%20challenges%20in%20identifying%20the%20best%20way%20to%20do%20it%20by%20leveraging%20one%20of%20the%20available%20Microsoft%20services.%3C%2FP%3E%0A%3CP%3EAs%20Microsoft%2C%20in%20fact%2C%20we%20offer%20two%20platforms%20for%20telemetry%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-10%22%3E%0A%3CLI%20id%3D%22pragma-line-10%22%3E%3CA%20href%3D%22https%3A%2F%2Fvisualstudio.microsoft.com%2Fapp-center%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EVisual%20Studio%20App%20Center%3C%2FA%3E%2C%20which%20is%20focused%20on%20client%20applications%20(mobile%20and%20desktop).%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-11%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Fapp%2Fapp-insights-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EApplication%20Insights%3C%2FA%3E%2C%20which%20is%20part%20of%20Azure%20and%20it's%20focused%20on%20web%20applications.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EElectron%20stands%20in%20the%20middle%20of%20these%20two%20worlds%2C%20being%20a%20platform%20to%20build%20desktop%20applications%20using%20web%20technologies.%20Under%20the%20hood%2C%20in%20fact%2C%20an%20Electron%20app%20is%20nothing%20more%20than%20a%20Node.js%20application%20with%20the%20UI%20rendered%20by%20an%20embedded%20instance%20of%20Chromium.%3C%2FP%3E%0A%3CP%3ESo%2C%20what%20is%20the%20right%20choice%3F%20The%20answer%20is...%20drum%20roll...%20Application%20Insights!%20App%20Center%2C%20in%20fact%2C%20being%20dedicated%20to%20client%20applications%20doesn't%20offer%20a%20real%20web-compliant%20SDK.%20The%20closer%20matches%20are%20the%20Apache%20Cordova%20and%20React%20Native%20SDKs%2C%20but%20we%20aren't%20talking%20about%20traditional%20web%20development%20here.%20Application%20Insights%2C%20instead%2C%20offers%20many%20web%20based%20SDKs%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FApplicationInsights-node.js%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eincluding%20one%20for%20Node.js%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3ELet's%20see%20how%20to%20integrate%20it%2C%20even%20if%20you'll%20see%20how%20the%20process%20is%20really%20straightforward!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22create-an-application-insight-resource-on-azure%22%20id%3D%22toc-hId-1619902414%22%20id%3D%22toc-hId-1619902414%22%3ECreate%20an%20Application%20Insight%20resource%20on%20Azure%3C%2FH3%3E%0A%3CP%3EThe%20first%20step%20is%20to%20login%20to%20your%20Azure%20account%20and%20create%20a%20new%20Application%20Insight%20project.%20Just%20click%20on%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3ECreate%20a%20resource%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20look%20for%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EApplication%20Insights%3C%2FSTRONG%3E.%20You%20can%20search%20for%20the%20it%20using%20the%20built-in%20search%20tool%2C%20otherwise%20you%20will%20find%20it%20under%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EDeveloper%20Tools%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eor%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EDevOps%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ecategories.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20867px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126717i679F94001ACCF204%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22azureportal.png%22%20title%3D%22azureportal.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThe%20required%20information%20are%20very%20minimal.%20You%20just%20need%20to%20choose%20the%20Azure%20subscription%20where%20you%20want%20to%20create%20the%20resource%2C%20a%20resource%20group%2C%20a%20name%20and%20the%20region.%3C%2FP%3E%0A%3CP%3EOnce%20the%20deployment%20has%20been%20completed%2C%20go%20to%20the%20resource.%20The%20most%20important%20information%20you%20will%20see%20is%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EInstrumentation%20key%3C%2FSTRONG%3E%2C%20which%20we're%20going%20to%20specify%20in%20our%20Electron%20application%20to%20connect%20it%20to%20this%20resource.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126724i9E4ED8E1E66E221D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22instrumentation.png%22%20title%3D%22instrumentation.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--932254547%22%20id%3D%22toc-hId--932254547%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22integrate-the-sdk-in-the-application%22%20id%3D%22toc-hId-810555788%22%20id%3D%22toc-hId-810555788%22%3EIntegrate%20the%20SDK%20in%20the%20application%3C%2FH3%3E%0A%3CP%3ENow%20that%20we%20have%20a%20resource%2C%20it's%20time%20to%20integrate%20it%20into%20our%20Electron%20application.%20The%20starting%20point%20is%20to%20install%20inside%20your%20project%20the%20SDK%2C%20which%20is%20available%20as%20a%20NPM%20package.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%20id%3D%22pragma-line-34%22%3E%0A%3CP%3ENote%3A%20In%20this%20blog%20post%20I%20will%20give%20for%20granted%20that%20you%20already%20have%20an%20Electron%20application%20and%20that%20your%20computer%20is%20already%20setup%20with%20all%20the%20required%20tools%20(Node.js%2C%20NPM%2C%20etc.).%20If%20it's%20your%20first%20time%20working%20with%20Electron%2C%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Felectronjs.org%2Fdocs%2Ftutorial%2Fdevelopment-environment%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eofficial%20documentation%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewill%20help%20you%20setting%20up%20your%20development%20environment.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EJust%20run%20the%20following%20command%20in%20a%20console%20(like%20the%20new%20shiny%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fstore%2Fapps%2F9n0dx20hk701%3Fcid%3Dstorebadge%26amp%3Bocid%3Dbadge%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWindows%20Terminal%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3D))%20on%20the%20folder%20which%20hosts%20your%20Electron%20app%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-38%22%20class%3D%22language-powershell%20hljs%22%3Enpm%20install%20--save%20applicationinsights%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ENow%20you%20can%20initialize%20the%20SDK%20when%20the%20application%20starts.%20A%20good%20place%20where%20to%20do%20it%20is%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Emain.js%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efile%2C%20inside%20the%20function%20hooked%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Eready%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eevent%20that%20is%20triggered%20when%20Electron%20has%20finished%20the%20initialization%20process%20and%20it's%20ready%20to%20create%20browser%20windows.%20If%20you're%20using%20the%20standard%20Electron%20template%20as%20a%20starting%20point%2C%20the%20function%20will%20be%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EcreateWindow()%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EJust%20add%20the%20following%20code%20inside%20this%20function%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-46%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20appInsights%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'applicationinsights'%3C%2FSPAN%3E)%3B%0AappInsights.setup(%3CSPAN%20class%3D%22hljs-string%22%3E%22your-instrumentation-key%22%3C%2FSPAN%3E).start()%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EYou%20will%20need%20to%20replace%20the%20parameter%20passed%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Esetup()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20(the%20one%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Eyour-instrumentation-key%3C%2FSTRONG%3E)%20with%20the%20Instrumentation%20Key%20you%20have%20retrieved%20from%20the%20Azure%20portal%20in%20the%20previous%20step.%3C%2FP%3E%0A%3CP%3EThat's%20it!%20Once%20you%20do%20this%2C%20the%20basic%20telemetry%20will%20be%20automatically%20sent%20to%20your%20Application%20Insights%20resource%20every%20time%20the%20application%20is%20connected%20to%20Internet.%20This%20includes%20information%20like%20the%20OS%20of%20the%20user%2C%20the%20geographical%20area%2C%20etc.%20Of%20course%2C%20all%20the%20information%20will%20be%20anonymous%20and%20there%20won't%20be%20any%20way%20to%20trace%20them%20back%20to%20the%20user%20who%20generated%20them.%3C%2FP%3E%0A%3CP%3EThese%20information%20will%20be%20displayed%20in%20the%20various%20sections%20under%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EUsage%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Earea%20of%20the%20Application%20Insights%20dashboard.%20For%20example%2C%20these%20are%20some%20of%20the%20information%20displayed%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EUsers%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Esection%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20757px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126719i74A42381EC875F1C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22users.png%22%20title%3D%22users.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CBLOCKQUOTE%20id%3D%22pragma-line-59%22%3E%0A%3CP%3EKeep%20in%20mind%20that%20Application%20Insights%2C%20even%20if%20it%20works%20great%20with%20our%20Electron%20application%2C%20it's%20born%20for%20traditional%20web%20application%2C%20served%20through%20a%20browser.%20As%20such%2C%20some%20of%20the%20collected%20information%20(like%20the%20browser%20of%20the%20user%20or%20the%20metrics%20around%20the%20response%20timings%20of%20the%20server)%20won't%20be%20available%20or%20will%20contain%20not%20useful%20data.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH3%20id%3D%22toc-hId--1741601173%22%20id%3D%22toc-hId--1741601173%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22tracking-custom-events%22%20id%3D%22toc-hId-1209162%22%20id%3D%22toc-hId-1209162%22%3ETracking%20custom%20events%3C%2FH3%3E%0A%3CP%3EOne%20of%20the%20most%20important%20features%20in%20the%20telemetry%20space%20is%20the%20ability%20to%20track%20events%20which%20are%20relevant%20to%20your%20application.%20It%20might%20be%20a%20page%20loaded%2C%20a%20button%20clicked%2C%20an%20option%20turned%20on%20or%20off%2C%20etc.%20Events%20are%20useful%20to%20understand%20how%20people%20are%20using%20the%20application%20and%20can%20give%20you%20meaningful%20insights%20to%20better%20understand%20how%20to%20move%20your%20application%20forward.%20For%20example%2C%20if%20you%20add%20a%20new%20option%20to%20your%20application%2C%20but%20the%20number%20of%20people%20who%20turn%20it%20on%20is%20really%20low%2C%20you%20may%20want%20to%20focus%20your%20efforts%20on%20something%20else%3B%20or%20if%20a%20section%20takes%20too%20much%20time%20to%20load%2C%20you%20may%20want%20to%20work%20on%20improving%20its%20performance.%3C%2FP%3E%0A%3CP%3EEvents%20must%20be%20tracked%20manually%2C%20since%20the%20SDK%20can't%20know%20how%20your%20application%20is%20designed%20and%20which%20kind%20of%20events%20you%20would%20like%20to%20track.%20To%20do%20this%2C%20you%20need%20to%20get%20a%20reference%20to%20the%20Application%20Insights%20client%20and%20then%20call%20one%20of%20the%20many%20available%20events%20which%20start%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Etrack%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eprefix.%20Here%20is%20a%20sample%20code%20to%20track%20a%20custom%20event%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-66%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20appInsights%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'applicationinsights'%3C%2FSPAN%3E)%3B%0AappInsights.setup(%3CSPAN%20class%3D%22hljs-string%22%3E%22your-instrumentation-key%22%3C%2FSPAN%3E).start()%3B%0A%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20client%20%3D%20appInsights.defaultClient%3B%0Aclient.trackEvent(%7B%3CSPAN%20class%3D%22hljs-attr%22%3Ename%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-string%22%3E'app%20loaded'%3C%2FSPAN%3E%7D)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EOnce%20you%20get%20a%20reference%20to%20the%20client%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EdefaultClient%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%2C%20you%20can%20leverage%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtrackEvent()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction.%20The%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Ename%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eparameter%20contains%20the%20name%20of%20the%20event%2C%20which%20will%20identify%20it%20unequivocally.%20If%20you%20reuse%20the%20same%20event%20name%20in%20multiple%20pages%2C%20it%20will%20be%20tracked%20under%20a%20single%20event.%20The%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtrackEvent()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction%20accepts%20also%20a%20second%20parameter%2C%20which%20is%20a%20collection%20of%20properties.%20It's%20helpful%20if%20you%20want%20to%20track%20not%20just%20the%20event%2C%20but%20also%20additional%20information%20to%20provide%20some%20context.%20For%20example%2C%20let's%20say%20you%20have%20a%20multi-language%20application%20and%20you%20want%20to%20track%20not%20only%20when%20the%20user%20has%20changed%20the%20language%2C%20but%20also%20which%20one%20he%20has%20selected.%20You%20could%20write%20some%20code%20like%20this%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-76%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20appInsights%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'applicationinsights'%3C%2FSPAN%3E)%3B%0AappInsights.setup(%3CSPAN%20class%3D%22hljs-string%22%3E%22your-instrumentation-key%22%3C%2FSPAN%3E).start()%3B%0A%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20client%20%3D%20appInsights.defaultClient%3B%0Aclient.trackEvent(%7B%3CSPAN%20class%3D%22hljs-attr%22%3Ename%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-string%22%3E%22Language%20changed%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-attr%22%3Eproperties%3C%2FSPAN%3E%3A%20%7B%20%3CSPAN%20class%3D%22hljs-attr%22%3Elanguage%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-string%22%3E%22Italian%22%3C%2FSPAN%3E%7D%7D)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAnother%20useful%20helper%20to%20track%20custom%20metrics%20is%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtrackMetric()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction.%20For%20example%2C%20let's%20say%20you%20want%20to%20track%20how%20many%20times%20the%20user%20has%20clicked%20on%20a%20specific%20option.%20This%20is%20the%20code%20you%20would%20use%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-86%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20appInsights%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'applicationinsights'%3C%2FSPAN%3E)%3B%0AappInsights.setup(%3CSPAN%20class%3D%22hljs-string%22%3E%22your-instrumentation-key%22%3C%2FSPAN%3E).start()%3B%0A%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20client%20%3D%20appInsights.defaultClient%3B%0Aclient.trackMetric(%7B%3CSPAN%20class%3D%22hljs-attr%22%3Ename%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-string%22%3E%22Button%20clicked%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-attr%22%3Evalue%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-number%22%3E3%3C%2FSPAN%3E%7D)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThese%20information%20will%20be%20reported%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EEvents%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Esection%20of%20the%20Application%20Insights%20dashboard%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20592px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126720iFE93EA32B5BDFE2B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22events.png%22%20title%3D%22events.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1744019497%22%20id%3D%22toc-hId-1744019497%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22tracking-errors%22%20id%3D%22toc-hId--808137464%22%20id%3D%22toc-hId--808137464%22%3ETracking%20errors%3C%2FH3%3E%0A%3CP%3EErrors%20can%20be%20tracked%20as%20well%20by%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtrackException()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%2C%20which%20accepts%20an%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EError%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%20as%20parameter.%20This%20method%20can%20be%20helpful%20to%20track%20custom%20errors%2C%20like%20with%20the%20following%20sample%20code%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-101%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20appInsights%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'applicationinsights'%3C%2FSPAN%3E)%3B%0AappInsights.setup(%3CSPAN%20class%3D%22hljs-string%22%3E%229a5f20b9-3d27-4ad2-842a-45f9c9ebad1c%22%3C%2FSPAN%3E).start()%3B%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20client%20%3D%20appInsights.defaultClient%3B%0Aclient.trackException(%7B%3CSPAN%20class%3D%22hljs-attr%22%3Eexception%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-built_in%22%3EError%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E%22This%20is%20a%20custom%20exception%22%3C%2FSPAN%3E)%7D)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EHowever%2C%20even%20more%20helpful%20would%20be%20the%20ability%20to%20track%20every%20unhandled%20exception%20in%20the%20application.%20The%20Application%20Insights%20SDK%20is%20able%20to%20do%20this%20automatically%20with%20standard%20web%20applications%2C%20but%20Electron%20%22hides%22%20every%20exception%20which%20doesn't%20lead%20to%20a%20critical%20failure%20of%20the%20app%2C%20so%20they%20aren't%20automatically%20tracked.%20We%20can%20workaround%20this%20problem%20by%20installing%20a%20NPM%20library%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fsindresorhus%2Felectron-unhandled%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EElectron%20Unhandled%3C%2FA%3E%2C%20which%20provides%20a%20convenient%20way%20to%20capture%20all%20the%20unhandled%20exceptions%20in%20an%20Electron%20application.%3C%2FP%3E%0A%3CP%3EFirst%20you%20have%20to%20install%20the%20package%20in%20your%20project%2C%20by%20running%20the%20following%20command%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-113%22%20class%3D%22language-powershell%20hljs%22%3Enpm%20install%20electron-unhandled%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20basic%20usage%20is%20quite%20simple.%20Just%20invoke%20the%20following%20code%20when%20your%20application%20starts.%20You%20can%20leverage%2C%20like%20we%20did%20for%20the%20SDK%20initialization%2C%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EwindowReady()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-119%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20unhandled%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'electron-unhandled'%3C%2FSPAN%3E)%3B%0Aunhandled()%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EHowever%2C%20this%20initialization%20isn't%20helpful%20for%20our%20scenario.%20With%20the%20default%20settings%2C%20in%20fact%2C%20exceptions%20will%20be%20logged%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-126%22%3E%0A%3CLI%20id%3D%22pragma-line-126%22%3EIn%20the%20Node.js%20debug%20console.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-127%22%3EWith%20an%20alert%20displayed%20to%20the%20user%2C%20if%20the%20Electron%20application%20has%20been%20compiled%20as%20a%20production%20build.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWe%20can%20customize%20the%20behavior%20of%20the%20library%20and%20use%20a%20custom%20logger%2C%20like%20our%20Application%20Insights%20SDK.%20Here%20is%20a%20full%20example%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-131%22%20class%3D%22language-javascript%20hljs%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20appInsights%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'applicationinsights'%3C%2FSPAN%3E)%3B%0AappInsights.setup(%3CSPAN%20class%3D%22hljs-string%22%3E%229a5f20b9-3d27-4ad2-842a-45f9c9ebad1c%22%3C%2FSPAN%3E).start()%3B%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Elet%3C%2FSPAN%3E%20client%20%3D%20appInsights.defaultClient%3B%0A%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Easync%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Efunction%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EcreateWindow%3C%2FSPAN%3E%20()%20%3C%2FSPAN%3E%7B%0A%20%20%0A%20%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20window%20initialization%3C%2FSPAN%3E%0A%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20unhandled%20%3D%20%3CSPAN%20class%3D%22hljs-built_in%22%3Erequire%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-string%22%3E'electron-unhandled'%3C%2FSPAN%3E)%3B%0A%20%20unhandled(%7B%3CSPAN%20class%3D%22hljs-attr%22%3EshowDialog%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-literal%22%3Etrue%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-attr%22%3Elogger%3C%2FSPAN%3E%3A%20%3CSPAN%20class%3D%22hljs-function%22%3E(%3CSPAN%20class%3D%22hljs-params%22%3Eerror%3C%2FSPAN%3E)%20%3D%26gt%3B%3C%2FSPAN%3E%20%7B%20client.trackException(%7B%3CSPAN%20class%3D%22hljs-attr%22%3Eexception%3C%2FSPAN%3E%3A%20error%7D)%7D%7D)%3B%0A%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20result%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20fetch(%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fwww.foo.foo%22%3C%2FSPAN%3E)%3B%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20json%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eawait%3C%2FSPAN%3E%20result.json()%3B%0A%20%20%3CSPAN%20class%3D%22hljs-built_in%22%3Econsole%3C%2FSPAN%3E.log(json)%3B%0A%7D%0A%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Efunction%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3ElogError%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3Eerror%3C%2FSPAN%3E)%20%3C%2FSPAN%3E%7B%0A%20%20%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWe%20invoke%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Eunhandled()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction%20passing%20two%20parameters.%20With%20the%20first%20one%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EshowDialog%3C%2FSTRONG%3E%2C%20we%20force%20the%20display%20of%20the%20alert%2C%20even%20if%20we're%20running%20in%20development%20mode.%20The%20second%20one%20is%20the%20custom%20logger%2C%20which%20we%20specify%20with%20a%20lambda%20expression%20that%20holds%2C%20inside%20the%20argument%2C%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EError%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%20with%20the%20full%20stack.%20In%20our%20scenario%20the%20custom%20logger%20invokes%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtrackException()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20of%20the%20Application%20Insights%20SDK%2C%20passing%20as%20exception%20the%20error%20caught%20by%20the%20library.%3C%2FP%3E%0A%3CP%3EThat's%20it!%20The%20next%20lines%20simulate%20an%20error.%20We're%20using%20the%20fetch%20APIs%2C%20which%20however%20we%20haven't%20referenced%20in%20the%20application.%20If%20we%20launch%20the%20application%2C%20we%20will%20see%20the%20error%20popping%20out%2C%20since%20we%20have%20set%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EshowDialog%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Etrue%3C%2FSTRONG%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126721iD869811ED8461237%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22promise.png%22%20title%3D%22promise.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAdditionally%2C%20thanks%20to%20the%20custom%20logger%2C%20the%20error%20will%20be%20logged%20also%20on%20the%20Application%20Insights%20dashboard.%20Move%20to%20the%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EFailures%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3Bsection%20and%20click%20on%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EExceptions%3C%2FSTRONG%3E%3CSPAN%3E.%20You%20will%20see%20the%20error%20there%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20885px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126722iAE4123609B151DE9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22exceptiondetail.png%22%20title%3D%22exceptiondetail.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH3%20id%3D%22wrapping-up%22%20id%3D%22toc-hId-934672871%22%20id%3D%22toc-hId-934672871%22%3EWrapping%20up%3C%2FH3%3E%0A%3CP%3EAdding%20telemetry%20to%20your%20application%2C%20no%20matter%20if%20it's%20a%20web%20or%20desktop%20one%2C%20is%20critical%20to%20address%20issues%20quickly%20and%20to%20understand%20usage%20patterns%2C%20so%20that%20you%20can%20focus%20on%20moving%20your%20application%20forwards%20in%20the%20right%20way.%20Electron%20applications%20aren't%20an%20exception%20to%20this%20best%20practice.%20Application%20Insights%2C%20the%20telemetry%20platform%20offered%20by%20Azure%2C%20is%20a%20great%20tool%20to%20achieve%20this%20goal%20and%2C%20thanks%20to%20the%20Node.js%20SDK%2C%20we%20can%20easily%20integrate%20it%20in%20our%20application%2C%20even%20if%20the%20SDK%20is%20originally%20born%20for%20traditional%20web%20apps.%3C%2FP%3E%0A%3CP%3EHappy%20coding!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-802091%22%20slang%3D%22en-US%22%3E%3CP%3ELet's%20learn%20how%20to%20leverage%20Application%20Insights%2C%20the%20telemetry%20platform%20offered%20by%20Azure%2C%20inside%20a%20desktop%20application%20built%20with%20Electron%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

Even if sometimes developers focus only on the CI/CD or automatic testing aspects, telemetry is one of the critical components to adopt a successful DevOps culture for your projects.

Being able to quickly build and deploy new versions of your application, in fact, is much less effective if you rely only on the willing of your users to report problems and issues. You must identify problems fast, before they start to affect all your users, and this is where telemetry, crash logging and analytics come into play.

Recently I helped a customer in integrating telemetry inside an Electron application and I faced some challenges in identifying the best way to do it by leveraging one of the available Microsoft services.

As Microsoft, in fact, we offer two platforms for telemetry:

 

Electron stands in the middle of these two worlds, being a platform to build desktop applications using web technologies. Under the hood, in fact, an Electron app is nothing more than a Node.js application with the UI rendered by an embedded instance of Chromium.

So, what is the right choice? The answer is... drum roll... Application Insights! App Center, in fact, being dedicated to client applications doesn't offer a real web-compliant SDK. The closer matches are the Apache Cordova and React Native SDKs, but we aren't talking about traditional web development here. Application Insights, instead, offers many web based SDKs, including one for Node.js.

Let's see how to integrate it, even if you'll see how the process is really straightforward!

 

Create an Application Insight resource on Azure

The first step is to login to your Azure account and create a new Application Insight project. Just click on Create a resource and look for Application Insights. You can search for the it using the built-in search tool, otherwise you will find it under the Developer Tools or DevOps categories.

 

azureportal.png

The required information are very minimal. You just need to choose the Azure subscription where you want to create the resource, a resource group, a name and the region.

Once the deployment has been completed, go to the resource. The most important information you will see is the Instrumentation key, which we're going to specify in our Electron application to connect it to this resource.

 

instrumentation.png

 

Integrate the SDK in the application

Now that we have a resource, it's time to integrate it into our Electron application. The starting point is to install inside your project the SDK, which is available as a NPM package.

 

Note: In this blog post I will give for granted that you already have an Electron application and that your computer is already setup with all the required tools (Node.js, NPM, etc.). If it's your first time working with Electron, the official documentation will help you setting up your development environment.

Just run the following command in a console (like the new shiny Windows Terminal =) ) on the folder which hosts your Electron app:

npm install --save applicationinsights

Now you can initialize the SDK when the application starts. A good place where to do it is in the main.js file, inside the function hooked to the ready event that is triggered when Electron has finished the initialization process and it's ready to create browser windows. If you're using the standard Electron template as a starting point, the function will be called createWindow().

Just add the following code inside this function:

let appInsights = require('applicationinsights');
appInsights.setup("your-instrumentation-key").start();

You will need to replace the parameter passed to the setup() method (the one called your-instrumentation-key) with the Instrumentation Key you have retrieved from the Azure portal in the previous step.

That's it! Once you do this, the basic telemetry will be automatically sent to your Application Insights resource every time the application is connected to Internet. This includes information like the OS of the user, the geographical area, etc. Of course, all the information will be anonymous and there won't be any way to trace them back to the user who generated them.

These information will be displayed in the various sections under the Usage area of the Application Insights dashboard. For example, these are some of the information displayed in the Users section:

users.png

Keep in mind that Application Insights, even if it works great with our Electron application, it's born for traditional web application, served through a browser. As such, some of the collected information (like the browser of the user or the metrics around the response timings of the server) won't be available or will contain not useful data.

 

Tracking custom events

One of the most important features in the telemetry space is the ability to track events which are relevant to your application. It might be a page loaded, a button clicked, an option turned on or off, etc. Events are useful to understand how people are using the application and can give you meaningful insights to better understand how to move your application forward. For example, if you add a new option to your application, but the number of people who turn it on is really low, you may want to focus your efforts on something else; or if a section takes too much time to load, you may want to work on improving its performance.

Events must be tracked manually, since the SDK can't know how your application is designed and which kind of events you would like to track. To do this, you need to get a reference to the Application Insights client and then call one of the many available events which start with the track prefix. Here is a sample code to track a custom event:

 

let appInsights = require('applicationinsights');
appInsights.setup("your-instrumentation-key").start();

let client = appInsights.defaultClient;
client.trackEvent({name: 'app loaded'});

Once you get a reference to the client using the defaultClient object, you can leverage the trackEvent() function. The name parameter contains the name of the event, which will identify it unequivocally. If you reuse the same event name in multiple pages, it will be tracked under a single event. The trackEvent() function accepts also a second parameter, which is a collection of properties. It's helpful if you want to track not just the event, but also additional information to provide some context. For example, let's say you have a multi-language application and you want to track not only when the user has changed the language, but also which one he has selected. You could write some code like this:

let appInsights = require('applicationinsights');
appInsights.setup("your-instrumentation-key").start();

let client = appInsights.defaultClient;
client.trackEvent({name: "Language changed", properties: { language: "Italian"}});

Another useful helper to track custom metrics is the trackMetric() function. For example, let's say you want to track how many times the user has clicked on a specific option. This is the code you would use:

let appInsights = require('applicationinsights');
appInsights.setup("your-instrumentation-key").start();

let client = appInsights.defaultClient;
client.trackMetric({name: "Button clicked", value: 3});

These information will be reported in the Events section of the Application Insights dashboard:

events.png

 

Tracking errors

Errors can be tracked as well by using the trackException() method, which accepts an Error object as parameter. This method can be helpful to track custom errors, like with the following sample code:

let appInsights = require('applicationinsights');
appInsights.setup("9a5f20b9-3d27-4ad2-842a-45f9c9ebad1c").start();
let client = appInsights.defaultClient;
client.trackException({exception: new Error("This is a custom exception")});

However, even more helpful would be the ability to track every unhandled exception in the application. The Application Insights SDK is able to do this automatically with standard web applications, but Electron "hides" every exception which doesn't lead to a critical failure of the app, so they aren't automatically tracked. We can workaround this problem by installing a NPM library called Electron Unhandled, which provides a convenient way to capture all the unhandled exceptions in an Electron application.

First you have to install the package in your project, by running the following command:

npm install electron-unhandled

The basic usage is quite simple. Just invoke the following code when your application starts. You can leverage, like we did for the SDK initialization, the windowReady() function.

const unhandled = require('electron-unhandled');
unhandled();

However, this initialization isn't helpful for our scenario. With the default settings, in fact, exceptions will be logged:

  • In the Node.js debug console.
  • With an alert displayed to the user, if the Electron application has been compiled as a production build.

We can customize the behavior of the library and use a custom logger, like our Application Insights SDK. Here is a full example:

let appInsights = require('applicationinsights');
appInsights.setup("9a5f20b9-3d27-4ad2-842a-45f9c9ebad1c").start();
let client = appInsights.defaultClient;

async function createWindow () {
  
  // window initialization

  const unhandled = require('electron-unhandled');
  unhandled({showDialog: true, logger: (error) => { client.trackException({exception: error})}});

  var result = await fetch("http://www.foo.foo");
  var json = await result.json();
  console.log(json);
}

function logError(error) {
  
}

We invoke the unhandled() function passing two parameters. With the first one, showDialog, we force the display of the alert, even if we're running in development mode. The second one is the custom logger, which we specify with a lambda expression that holds, inside the argument, the Error object with the full stack. In our scenario the custom logger invokes the trackException() method of the Application Insights SDK, passing as exception the error caught by the library.

That's it! The next lines simulate an error. We're using the fetch APIs, which however we haven't referenced in the application. If we launch the application, we will see the error popping out, since we have set the showDialog property to true:

 

promise.png

Additionally, thanks to the custom logger, the error will be logged also on the Application Insights dashboard. Move to the Failures section and click on Exceptions. You will see the error there:

 

exceptiondetail.png

Wrapping up

Adding telemetry to your application, no matter if it's a web or desktop one, is critical to address issues quickly and to understand usage patterns, so that you can focus on moving your application forwards in the right way. Electron applications aren't an exception to this best practice. Application Insights, the telemetry platform offered by Azure, is a great tool to achieve this goal and, thanks to the Node.js SDK, we can easily integrate it in our application, even if the SDK is originally born for traditional web apps.

Happy coding!