| 1 | /*! |
| 2 | * VisualEditor tracking methods. |
| 3 | * |
| 4 | * @copyright See AUTHORS.txt |
| 5 | */ |
| 6 | |
| 7 | ( function () { |
| 8 | const callbacks = $.Callbacks( 'memory' ), |
| 9 | queue = []; |
| 10 | |
| 11 | /** |
| 12 | * Track an analytic event. |
| 13 | * |
| 14 | * VisualEditor uses this method internally to track internal changes of state that are of analytic |
| 15 | * interest, either because they provide data about how users interact with the editor, or because |
| 16 | * they contain exception info, latency measurements, or other metrics that help gauge performance |
| 17 | * and reliability. VisualEditor does not transmit these events by default, but it provides a |
| 18 | * generic interface for routing these events to an analytics framework. |
| 19 | * |
| 20 | * @memberof ve |
| 21 | * @param {string} topic Event name |
| 22 | * @param {Object} [data] Additional data describing the event, encoded as an object |
| 23 | */ |
| 24 | ve.track = function ( topic, data ) { |
| 25 | queue.push( { topic: topic, timeStamp: ve.now(), data: data } ); |
| 26 | callbacks.fire( queue ); |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * Register a handler for subset of analytic events, specified by topic |
| 31 | * |
| 32 | * Handlers will be called once for each tracked event, including any events that fired before the |
| 33 | * handler was registered, with the topic, event data payload, and event timestamp as the first, |
| 34 | * second, and third arguments, respectively. |
| 35 | * |
| 36 | * @memberof ve |
| 37 | * @param {string} topic Handle events whose name starts with this string prefix |
| 38 | * @param {Function} callback Handler to call for each matching tracked event |
| 39 | */ |
| 40 | ve.trackSubscribe = function ( topic, callback ) { |
| 41 | let seen = 0; |
| 42 | |
| 43 | callbacks.add( ( q ) => { |
| 44 | let event; |
| 45 | for ( ; seen < q.length; seen++ ) { |
| 46 | event = q[ seen ]; |
| 47 | if ( event.topic.indexOf( topic ) === 0 ) { |
| 48 | callback( event.topic, event.data, event.timeStamp ); |
| 49 | } |
| 50 | } |
| 51 | } ); |
| 52 | }; |
| 53 | }() ); |