Telerik Analytics Plugin for NativeScript
Getting started
-
Obtain an AppId
Create a new application in Telerik Platform by choosing the Native application type.
-
Enable Analytics
Select Analytics from the left navigation menu and click Enable Analytics.
-
Create a new NativeScript application
tns create MyApp
or use an existing one.
-
Add the Analytics plugin (from npm). This will install the nativescript-telerik-analytics plugin in node_modules in the root of the project. When adding a new platform (or using an existing one) the plugin will be added there as well. Go to the application folder and add the Analytics plugin:
tns plugin add nativescript-telerik-analytics
-
Go to the application folder and add the Android (or iOS) platform to the application:
tns platform add android
-
Initialize the plugin and start a new session in the
onLaunch
event (app.js):var application = ;applicationmainModule = 'main-page';applicationcssFile = './app.css';application;applicationstart; -
Track some events in your application:
var Analytics =timer = ;Analytics;Analytics;var timingScope = Analytics;timer;Analytics; // track timing of 1300 mstrythrow 'error message';catch eAnalytics; -
Attach your phone to the PC, ensure
adb devices
command lists it and run the app on the phone:tns run android
API
To use the Analytics plugin you need to require the nativescript-telerik-analytics
module:
var Analytics = ;
And then call any of the available methods on it:
-
init(settings) - used to initialize the plugin with different configuration options. This method must be called before starting a new session or tracking events. It is the first method that needs to be called.
var settings =appId: 'oamq6lixk0rak4dl' // Required identifier of the application obtained in Telerik PlatformproductVersion: '1.2.3.4' // Optional - the version of the monitored applicationlocation: // optionally associate some geo location coordinates with the userlatitude: 40719618longitude: -74010282clientIP: '193.42.34.123' // optionally override the IP of the userisInternalData: false // Optional flag allowing to enable test mode for this session. This will mark all events tracked in this particular session as "Internal"autoTrackUnhandledExceptions: false // Optionally turn off automatic exception handling. The default value is true. The plugin subscribes to the "application.uncaughtErrorEvent" and automatically tracks the exceptionlogger: // Optionally specify a custom logger. This should be an instance of a class with info(message, obj) and error(message, obj) functions.;Analytics; -
start() - starts a new Analytics session. The SDK needs to be initialized with the init method prior to calling this method.
Analyticsstart; -
trackEvent(name) - registers a feature usage. It is recommended that related features are grouped by using simple dot-notation in the name such as e.g. relating print to pdf and print to file by naming the features "print.pdf" and "print.file" respectively
Analytics; -
trackValue(name, value) - register a value on a specific feature. While calls to
trackEvent
increments the use of a feature in the session a call to this methods will associate a given value with a named feature. Use this method to e.g. track the distribution of file sizes imported or the number of results registered. Tracking this distribution across all your application usage will give insights to what scenarios your applications are handling. The value parameter must be a valid integer.Analytics; -
trackException(e, context) - Call to track an exception that occurred in the application. An optional context string can be associated with the exception.
trythrow 'error message';catch eAnalytics; -
trackTimingStart(name) - Starts a named timer for measuring elapsed time on operation and returns a scope that can be used to stop or cancel the timing operation.
var timer =timingScope = Analytics;timer; -
trackTimingRaw(name, durationInMilliseconds) - Registers elapsed time measured by some other means.
Analytics;
Troubleshooting
In case the application doesn't work as expected, here are some things you can verify:
-
For Android ensure that the AndroindManifest.xml located at
platforms\android
contains the following permission: -
Enable logging to see if there are some information or error messages logged. You could enable logging by writing the following module (
mylogger.js
):{var {};exports = moduleexports = MyLogger;MyLoggerprototype {console;};MyLoggerprototype {if obj instanceof Errorconsole;elseconsole;};}this || global;and then set this logger when initializing the plugin:
var Analytics =MyLogger = ;Analytics;