Monitor Integration in NativeScript

This article will describe how to integrate Telerik Analytics in your NativeScript application.

The integration process consists of 3 steps:

  1. Unzip the provided package in your application folder
  2. Create and start the monitor
  3. Start tracking interesting statistics

Before getting started you must create an application and obtain a product id. You will need this id in the integration section. Read more about how to Add Analytics to Your Application. It is important to select the javascript project type when creating the new project.

The NativeScript module requires the following permissions in Android to work correctly (those permissions are automatically added to the corresponding manifests when adding the android platform using the tns platform add android command):

android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.INTERNET

Step 1: Add the library to your application

Download the NativeScript zip package which contains the following structure:

AnalyticsMonitorNativeScript_major.minor.build.zip
├── NativeScriptMonitor
│   ├── package.json
│   ├── DeviceInfo.js
│   ├── EqatecMonitor.js
│   └── NativeScriptMonitor.js
└── tns_modules
    └── platform-ext
        ├── platform-ext.android.js
        ├── platform-ext.ios.js  
        └── package.json

Create a new NativeScript application:

tns create hello-world

Add some platforms to it:

cd hello-world
tns platform add android

Unzip the contents of the downloaded package inside the app folder of your nativescript application so that the target directory structure looks like this:

.
├── app
│   ├── app.css
│   ├── app.js
│   ├── main-page.js
│   ├── main-view-model.js
│   ├── main-page.xml
│   ├── NativeScriptMonitor
│   │   ├── package.json
│   │   ├── DeviceInfo.js
│   │   ├── EqatecMonitor.js
│   │   └── NativeScriptMonitor.js
│   └── tns_modules
│       ├── platform-ext
│       │   ├── platform-ext.android.js
│       │   ├── platform-ext.ios.js  
│       │   └── package.json
│       └── ...
├── lib
│   ├── android
│   └── ios
└── platforms
    ├── android
    └── ios

Step 2: Create and start the monitor

The Nativescript module can be included in your app.js file and an instance of the monitor created at application startup. This instance can be later reused in the application:

var application = require('application');
application.mainModule = 'main-page';
application.cssFile = './app.css';

application.onLaunch = function(context) {
  // including the Telerik Analytics module
  var NativeScriptMonitor = require('./NativeScriptMonitor').Monitor;

  // Create an instance of the Telerik Analytics monitor
  var analytics = new NativeScriptMonitor({
    productId: 'PUT-YOUR-PRODUCTID-HERE', // required GUID obtained when creating the application in the Analytics module
    version: '1.2.3', // optional version of the monitored application
    useHttps: true // optional flag specifying the protocol used for calling the Analytics endpoint
  });

  // Starting a new analytics session
  analytics.start();

  // Store the analytics instance so that we can track some features in our view models later
  application.analytics = analytics;
};

application.onExit = function() {
  // Stoping the analytics session at application exit
  application.analytics.stop();
};

application.start();

That's it, really. If you run your application now it will start reporting basic analytics data:

tns run android

Step 3: Start tracking interesting statistics

The next natural step is to track interesting events and exceptions occurring in your application.

The NativeScript monitor exposes the same SDK methods as its javascript counterpart.

var application = require('application');

// Tracking features
application.analytics.trackFeature('SomeFeature');

// Tracking exceptions
try {
  someOperationThatThrowsAnException();
}
catch (e) {
  application.analytics.trackException(e, e.message);
}

// Tracking timing
application.analytics.trackFeatureStart('SomeLongOperation');
someLongOperation();
application.analytics.trackFeatureStop('SomeLongOperation');

// Tracking numeric values
application.analytics.trackFeatureValue('SomeFeatureValue', 1234);

Conclusion

After completing the steps in this guide, Telerik Analytics should be integrated into your application. When you run it, you should see data within 5 minutes on the Analytics client dashboard. If not then please check the article on Your First Data.

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.