I recently added Google Analytics tracking to my Ionic Framework app, ChkBook. I did this mostly out of curiosity, just wanting to know how much people were using my app. This is because other that seeing the number of downloads, there is no other real look at how or when people are using your app. While putting this in I thought that it might be a good idea to (since I was already gonna be doing the bulk of the work) add in error logging/tracking into the app.

Error Handler


So, first thing I did was make sure I had an error handler on my AngularJS app. To do this I scrounged some code together from some googleing around. Check out the documentation here for some more info on the $exceptionHandler in AngularJS. My exception handling code looks like the following and is in my main app.js.

In this code I use the $injector to get my loggingService which is a service I created to handle all the logging calls I needed to make to Google Analytics. The loggingService is where I use the ngCordova plugin called $cordovaGoogleAnalytics to push the data to Google Analytics (documentation for that plugin can be found here).


loggingService & $cordovaGoogleAnalytics




In my loggingService I have the function logError(errorData, message) where I just check if the window has cordova defined (which is used to check if I am running it in a browser or if its running on the device). If it’s running in the browser I just log it out to the console (because I am likely just developing) and if its running on a device I make the call to the $cordovaGoogleAnalytics plugin and pass the data that I want to capture. The way I have it now is fairly simple but it works. (code in this service is still a little bit of a work in progress so its not that great right now)


Hope someone finds this helpful, or at least amusing. Thanks for taking a look! :)