Building Better Ionic Apps with Ionic Pro, Part 3

Muhammad Ahsan Ayaz
Modus Create: Front End Development
5 min readJan 16, 2018
Part1: Ionic Creator
Part2: Ionic DevApp
Part3: Ionic Monitor
Part4: Ionic Deploy

In our previous post, we demonstrated how to easily run an Ionic app on devices without installing the native SDKs using Ionic DevApp. For Part 3 in our series, we will use Ionic Monitor to find and fix errors before they are caught by users.

Ionic Monitor

Ionic Monitor is a great tool as it enables monitoring runtime errors automatically. It is perhaps the only solution that provides error tracking to the typescript source files. Compared to other error tracking mechanisms, the most appealing thing about Ionic Monitor is the way it tracks the error from the web layer to the typescript source maps.

Now what caused it to fail?

Combining Ionic Monitor with Ionic Deploy allows for rolling out hot updates with fixes quickly. In essence, with Ionic Monitor you can:

  • Find out the root cause of runtime errors with tracing to the typescript layer.
  • Get real time notifications and alerts so you are notified as soon as something goes bad in the app.
  • Fix the errors quickly, even in apps already live in app stores.

Getting started with Ionic Monitor

If you do not have the latest version of the Ionic CLI installed, run the command:

npm install -g ionic

In terminal, from the project’s root folder, install the Ionic Pro client from NPM:

npm install -S @ionic/pro

Make sure that you’re running @ionic/app-scripts version 3.1.0 or later, and @ionic/proversion 1.0.12 or later. You can verify the versions in the package.json file.

To setup and integrate Ionic Monitor in our notes-app , we will create an Ionic Provider named AppErrorHandlerProvider . In terminal, from the project’s root folder, run the command below to create the provider:

ionic g provider AppErrorHandler

Open the file providers/app-error-handler/app-error-handler.ts and replace the contents with the code below:

Replace the APP_ID and APP_VERSION with your application’s values for each of them. Go to Ionic Dashboard and copy your Ionic Pro App ID as shown in the image below. This APP_ID makes sure that Ionic Monitor is targeting the appropriate app for error tracking.

Copying APP_ID from Ionic Dashboard

You will also need the value for the APP_VERSION . This is useful when shipping newer versions of the app and identifying which version the errors on Ionic Monitor are referring to. The value of APP_VERSION should be similar to the version of your package.json or config.xml (highly recommended).

Copying APP_VERSION from package.json or config.xml

Once that is taken care of, add AppErrorHandlerProvider to the providers in the app.module.tsfile as shown below:

Ionic Monitor is now set up. As an example, I created a page in the application named Monitorcontaining several buttons, each triggering a different implementation of the monitoring service. See the page in action and the results below:

Triggering errors manually
Event Log: Errors List
Event Log: Error Details

Notice that the Exception Detail contains references to main.js, and on the right the information shows Source Mapped value as NO . This means that the source maps have not been configured/synced with the Ionic Pro services. To do that, run the command below (using Ionic CLI 3.9.0 or greater) in your terminal:

ionic monitoring syncmaps

Using SourceMaps

When using source maps, make sure to do the following:

  • Sync the source maps before pushing a release to Ionic Pro.
  • Do not sync more than once per release as mentioned in Ionic Monitor docs:

Before doing a release, ensure you’ve synced source maps and make sure to not send new source maps for that version in the future.

  • The version number of the running app should match the version of the source maps.
  • Utilizing semver for versioning helps in detecting regressions by comparing the version of the app in which the error occurred versus future releases of the app.

You can also upload the source maps manually. To do that, go to Ionic Dashboard. Click the Monitoring tab, then click the source maps button on the top right. You need to do a dev or prodbuild to generate source maps in YOUR_APP/.sourcemaps/. Upload main.js.map along with other .map files related to your code.
After following the steps above, you should see your synced source maps in the Ionic Dashboard as follows:

When you open the app again, tap the Call wrapped function button to trigger the same error as in the prior images. You will now see the .ts stack trace as well.

Besides having manual triggers, our app also catches any errors automatically since we’re using AppErrorHandlerProvider as the ErrorHandler. Below is an example of an error that occurred when the app property is set to null:

Summary

Ionic Monitor is a great tool that helps you find errors before they become painful for end users. With email alerts, you are notified as soon as something goes bad in your application. Since Monitor can pinpoint the errors to their exact .ts sources, there is less chance of panic attacks for you in the process of fixing the issues.

Part 4

In Part 4, we will go through Ionic Deploy, a service that makes it super easy to deploy app updates in real time.

--

--

Muhammad Ahsan Ayaz
Modus Create: Front End Development

Educator | Angular GDE | Speaker | Author of Angular Cookbook | Left medium and now on https://dev.to/codewithahsan