2013-07-30 22:52:19 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor tracking methods.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function () {
|
|
|
|
var callbacks = $.Callbacks( 'memory' ), queue = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Track an analytic event.
|
|
|
|
*
|
|
|
|
* VisualEditor uses this method internally to track internal changes of state that are of analytic
|
|
|
|
* interest, either because they provide data about how users interact with the editor, or because
|
|
|
|
* they contain exception info, latency measurements, or other metrics that help gauge performance
|
|
|
|
* and reliability. VisualEditor does not transmit these events by default, but it provides a
|
|
|
|
* generic interface for routing these events to an analytics framework.
|
|
|
|
*
|
|
|
|
* @member ve
|
2013-10-11 20:49:41 +00:00
|
|
|
* @param {string} topic Event name
|
|
|
|
* @param {Object} [data] Additional data describing the event, encoded as an object
|
2013-07-30 22:52:19 +00:00
|
|
|
*/
|
2013-10-11 20:49:41 +00:00
|
|
|
ve.track = function ( topic, data ) {
|
|
|
|
queue.push( { topic: topic, timeStamp: ve.now(), data: data } );
|
2013-07-30 22:52:19 +00:00
|
|
|
callbacks.fire( queue );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-10-11 20:49:41 +00:00
|
|
|
* Register a handler for subset of analytic events, specified by topic
|
2013-07-30 22:52:19 +00:00
|
|
|
*
|
|
|
|
* Handlers will be called once for each tracked event, including any events that fired before the
|
|
|
|
* handler was registered; 'this' is set to a plain object with a 'timeStamp' property indicating
|
2013-10-11 20:49:41 +00:00
|
|
|
* the exact time at which the event fired, a string 'topic' property naming the event, and a
|
|
|
|
* 'data' property which is an object of event-specific data. The event topic and event data are
|
|
|
|
* also passed to the callback as the first and second arguments, respectively.
|
2013-07-30 22:52:19 +00:00
|
|
|
*
|
|
|
|
* @member ve
|
2013-10-11 20:49:41 +00:00
|
|
|
* @param {string} topic Handle events whose name starts with this string prefix
|
|
|
|
* @param {Function} callback Handler to call for each matching tracked event
|
2013-07-30 22:52:19 +00:00
|
|
|
*/
|
2013-10-11 20:49:41 +00:00
|
|
|
ve.trackSubscribe = function ( topic, callback ) {
|
|
|
|
var seen = 0;
|
|
|
|
|
2013-07-30 22:52:19 +00:00
|
|
|
callbacks.add( function ( queue ) {
|
2013-10-11 20:49:41 +00:00
|
|
|
var event;
|
2013-07-30 22:52:19 +00:00
|
|
|
for ( ; seen < queue.length; seen++ ) {
|
2013-10-11 20:49:41 +00:00
|
|
|
event = queue[ seen ];
|
|
|
|
if ( event.topic.indexOf( topic ) === 0 ) {
|
|
|
|
callback.call( event, event.topic, event.data );
|
|
|
|
}
|
2013-07-30 22:52:19 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
2013-10-11 20:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a handler for all analytic events
|
|
|
|
*
|
|
|
|
* Like ve#trackSubscribe, but binds the callback to all events, regardless of topic.
|
|
|
|
*
|
|
|
|
* @member ve
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
ve.trackSubscribeAll = function ( callback ) {
|
|
|
|
ve.trackSubscribe( '', callback );
|
|
|
|
};
|
2013-07-30 22:52:19 +00:00
|
|
|
}() );
|