App Center Analytics (Apache Cordova)

Important

Visual Studio App Center is scheduled for retirement on March 31, 2025. While you can continue to use Visual Studio App Center until it is fully retired, there are several recommended alternatives that you may consider migrating to.

Learn more about support timelines and alternatives.

Note

Support for Cordova Apps has ended in April 2022. Find more information in the App Center blog.

App Center Analytics helps you understand user behavior and customer engagement to improve your app. The SDK automatically captures session count and device properties like model, OS version, etc. You can define your own custom events to measure things that matter to you. All the information captured is available in the App Center portal for you to analyze the data.

Follow the Get Started section if you haven't set up the SDK in your application yet.

Session and device information

Once you add App Center Analytics to your app and the SDK is started, it will automatically track sessions and device properties like OS Version, model, etc.

Custom events

You can track your own custom events with up to 20 properties to understand the interaction between your users and the app.

Once you've started the SDK, use the trackEvent method to track your events with properties. You can send up to 200 distinct event names. Also, there's a maximum limit of 256 characters per event name and 125 characters per event property name and event property value.

var success = function() {
    console.log("Event tracked");
}

var error = function(error) {
    console.error(error);
}
AppCenter.Analytics.trackEvent('Video clicked', { Category: 'Music', FileName: 'favorite.avi' }, success, error);

This function uses third parameter as success callback which returns an empty string and fourth parameter as error callback which returns an error.

Properties for events are entirely optional. If you just want to track an event, use this sample instead:

var success = function() {
    console.log("Event tracked");
}

var error = function(error) {
    console.error(error);
}

AppCenter.Analytics.trackEvent('Video clicked', success, error);

Enable or disable App Center Analytics at runtime

You can enable and disable App Center Analytics at runtime. If you disable it, the SDK won't collect analytics information for the app anymore.

var success = function() {
    console.log("Analytics disabled");
}

var error = function(error) {
    console.error(error);
}

AppCenter.Analytics.setEnabled(false, success, error);

This function uses second parameter as success callback which returns an empty string and third parameter as error callback which returns an error. To enable App Center Analytics again, use the same API but pass true as a parameter.

var success = function() {
    console.log("Analytics enabled");
}

var error = function(error) {
    console.error(error);
}

AppCenter.Analytics.setEnabled(true, success, error);

The state is persisted in the device's storage across application launches.

Check if App Center Analytics is enabled

You can also check whether App Center Analytics is enabled.

var success = function(result) {
    console.log("analytics " + (result) ? "enabled" : "disabled");
}

var error = function(error) {
    console.error(error);
}

AppCenter.Analytics.isEnabled(success, error);

This function uses first parameter as success callback which returns a boolean and second parameter as error callback which returns an error.

Wait for JS to enable App Center Analytics

In some cases, an application may want to ask users whether they want to share analytics information. In that case, you should change preference APPCENTER_ANALYTICS_ENABLE_IN_JS to true in your config.xml

<preference name="APPCENTER_ANALYTICS_ENABLE_IN_JS" value="true" />

This means that for any information to be sent to App Center (even basic session information), the developer must first enable App Center Analytics inside the app by adding the following line to his code.

var success = function() {
    console.log("analytics disabled");
}

var error = function(error) {
    console.error(error);
}

AppCenter.Analytics.setEnabled(true, success, error);

Local storage size

By default, the SDK stores up to 10 MB of logs in the storage.

No internet access

When there isn't any network connectivity, the SDK saves up to 10 MB of logs in the local storage. Once the storage is full, the SDK will start discarding old logs to make room for the new logs. Once the device gets internet access back, the SDK will send logs in the batch of 50 or after every 3 seconds.

Batching event logs

The App Center SDK uploads logs in a batch of 50 and if the SDK doesn't have 50 logs to send, it will still send logs after 3 seconds. There can be a maximum of three batches sent in parallel.

Retry and back-off logic

App Center SDK supports back-off retries on recoverable network errors. Below is the retry logic:

  • 3 tries maximum per request.
  • Each request has its own retry state machine.
  • All the transmission channels are disabled (until next app process) after one request exhausts all its retries.

Back-off logic

  • 50% randomization, first retry between 5s and 10s, second retry between 2.5 and 5 minutes, last try between 10 and 20 minutes.
  • If network switches from off to on (or from wi-fi to mobile), retry states are reset and requests are retried immediately.