mediawiki-extensions-Popups/resources/ext.popups.logger.js
Prateek Saxena f4dbd455c6 logger: Add a sampling rate of 10
This rate was decided in T88173

Bug: T88167
Change-Id: I22776582c271fa83ba5b98d38dd01775ebbfcef1
2015-03-27 04:35:12 +05:30

63 lines
1.3 KiB
JavaScript

( function ( $, mw ) {
/**
* @class mw.popups.logger
* @singleton
*/
var logger = {};
/**
* Sampling rate at which events are logged
* @property samplingRate
*/
logger.samplingRate = 10;
/**
* Get action based on click event
*
* @method getAction
* @param {Object} event
*/
logger.getAction = function ( event ) {
if ( event.which === 2 ) { // middle click
return 'opened in new tab';
} else if ( event.which === 1 ) {
if ( event.ctrlKey || event.metaKey ) {
return 'opened in new tab';
} else if ( event.shiftKey ) {
return 'opened in new window';
} else {
return 'opened in same tab';
}
}
};
/**
* Logs the popup event as defined in the following schema-
* https://meta.wikimedia.org/wiki/Schema:Popups
*
* @method log
* @param {Object} event
* @return {jQuery.Promise}
*/
logger.log = function ( event ) {
if (
mw.eventLog === undefined ||
Math.floor( Math.random() * logger.samplingRate ) !== 0
) {
return $.Deferred().resolve();
}
// Get duration from time
if ( $.isNumeric( event.time ) ) {
event.duration = Math.floor( mw.now() - event.time );
delete event.time;
}
return mw.eventLog.logEvent( 'Popups', event );
};
mw.popups.logger = logger;
} ) ( jQuery, mediaWiki );