VulpesOS/gaia/shared/js/metrics_helper.js
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

52 lines
1.5 KiB
JavaScript
Executable file

/**
* This module is a helper class for modules that want to report metrics for
* transmission to the telemetry server for collection. This module handles the
* details of creating an iac port for connection to the AppUsageMetrics
* module and handles the details of reporting a message to AppUsageMetrics.
*
* Usage:
* -To initialize the module: MetricsHelper.init();
* -To report metrics:
* MetricsHelper.report({action: 'your action', data: 'your data'});
* Note that this needs to be hooked up on AppUsageMetrics to deliver anything
* meaningful to the Telemetry Server.
*
*/
'use strict';
(function(exports) {
function MetricsHelper() {
this._portMetrics = null;
this._initialized = false;
}
MetricsHelper.prototype = {
init: function () {
var self = this;
// Initialize the port for connecting to the system app to report metrics.
navigator.mozApps.getSelf().onsuccess = function () {
var app = this.result;
app.connect('app-metrics').then(function onConnAccepted(ports) {
if (ports.length > 0) {
self._portMetrics = ports[0];
self._initialized = true;
}
}, function onConnectionRejected(reason) {
console.log('Error connecting: ' + reason + '\n');
});
};
},
report: function (action, data) {
if (this._initialized) {
this._portMetrics.postMessage({
'action': action,
'data': data
});
}
}
};
exports.MetricsHelper = MetricsHelper;
})(window);