2014-10-29 01:19:52 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki event subscriber.
|
|
|
|
*
|
|
|
|
* Subscribes to ve.track() events and routes them to mw.track().
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2014-10-29 01:19:52 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function () {
|
2021-10-13 12:57:45 +00:00
|
|
|
var actionPrefixMap = {
|
2019-09-10 00:32:50 +00:00
|
|
|
firstChange: 'first_change',
|
2018-10-26 20:46:36 +00:00
|
|
|
saveIntent: 'save_intent',
|
|
|
|
saveAttempt: 'save_attempt',
|
|
|
|
saveSuccess: 'save_success',
|
|
|
|
saveFailure: 'save_failure'
|
2018-11-27 17:50:01 +00:00
|
|
|
},
|
2019-11-13 19:47:36 +00:00
|
|
|
trackdebug = !!mw.Uri().query.trackdebug,
|
|
|
|
firstInitDone = false;
|
|
|
|
|
|
|
|
function getEditingSessionIdFromRequest() {
|
|
|
|
return mw.config.get( 'wgWMESchemaEditAttemptStepSessionId' ) ||
|
|
|
|
mw.Uri().query.editingStatsId;
|
|
|
|
}
|
2015-08-19 18:05:01 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var timing = {};
|
|
|
|
var editingSessionId = getEditingSessionIdFromRequest() || mw.user.generateRandomSessionId();
|
2021-10-18 23:55:52 +00:00
|
|
|
ve.init.editingSessionId = editingSessionId;
|
2014-10-29 01:19:52 +00:00
|
|
|
|
2018-12-11 16:13:06 +00:00
|
|
|
function log() {
|
|
|
|
// mw.log is a no-op unless resource loader is in debug mode, so
|
|
|
|
// this allows trackdebug to work independently (T211698)
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log.apply( console, arguments );
|
|
|
|
}
|
|
|
|
|
2022-08-02 07:52:20 +00:00
|
|
|
function inEASSample() {
|
2018-10-15 22:33:32 +00:00
|
|
|
// Not using mw.eventLog.inSample() because we need to be able to pass our own editingSessionId
|
|
|
|
return mw.eventLog.randomTokenMatch(
|
2018-10-26 20:46:36 +00:00
|
|
|
1 / mw.config.get( 'wgWMESchemaEditAttemptStepSamplingRate' ),
|
2018-10-15 22:33:32 +00:00
|
|
|
editingSessionId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-02 07:52:20 +00:00
|
|
|
function inVEFUSample() {
|
|
|
|
return mw.eventLog.randomTokenMatch(
|
|
|
|
1 / mw.config.get( 'wgWMESchemaVisualEditorFeatureUseSamplingRate' ),
|
|
|
|
editingSessionId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-19 06:07:57 +00:00
|
|
|
function addABTestData( data ) {
|
2022-02-11 21:42:17 +00:00
|
|
|
// DiscussionTools New Topic A/B test for logged out users
|
|
|
|
if ( !mw.config.get( 'wgDiscussionToolsABTest' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-19 06:07:57 +00:00
|
|
|
if ( mw.config.get( 'wgDiscussionToolsABTestBucket' ) ) {
|
|
|
|
data.bucket = mw.config.get( 'wgDiscussionToolsABTestBucket' );
|
2022-02-11 21:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:11:14 +00:00
|
|
|
function computeDuration( action, event, timeStamp ) {
|
|
|
|
if ( event.timing !== undefined ) {
|
|
|
|
return event.timing;
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:19:52 +00:00
|
|
|
switch ( action ) {
|
|
|
|
case 'ready':
|
2015-02-20 20:11:14 +00:00
|
|
|
return timeStamp - timing.init;
|
2017-12-13 19:20:09 +00:00
|
|
|
case 'loaded':
|
|
|
|
return timeStamp - timing.init;
|
2019-09-10 00:32:50 +00:00
|
|
|
case 'firstChange':
|
|
|
|
return timeStamp - timing.ready;
|
2014-10-29 01:19:52 +00:00
|
|
|
case 'saveIntent':
|
2015-02-20 20:11:14 +00:00
|
|
|
return timeStamp - timing.ready;
|
2014-10-29 01:19:52 +00:00
|
|
|
case 'saveAttempt':
|
2015-02-20 20:11:14 +00:00
|
|
|
return timeStamp - timing.saveIntent;
|
2014-10-29 01:19:52 +00:00
|
|
|
case 'saveSuccess':
|
|
|
|
case 'saveFailure':
|
|
|
|
// HERE BE DRAGONS: the caller must compute these themselves
|
|
|
|
// for sensible results. Deliberately sabotage any attempts to
|
|
|
|
// use the default by returning -1
|
|
|
|
mw.log.warn( 've.init.mw.trackSubscriber: Do not rely on default timing value for saveSuccess/saveFailure' );
|
|
|
|
return -1;
|
|
|
|
case 'abort':
|
2015-02-20 20:11:14 +00:00
|
|
|
switch ( event.type ) {
|
2014-10-29 01:19:52 +00:00
|
|
|
case 'preinit':
|
2015-02-20 20:11:14 +00:00
|
|
|
return timeStamp - timing.init;
|
2014-10-29 01:19:52 +00:00
|
|
|
case 'nochange':
|
|
|
|
case 'switchwith':
|
|
|
|
case 'switchwithout':
|
2015-09-04 00:42:05 +00:00
|
|
|
case 'switchnochange':
|
2014-10-29 01:19:52 +00:00
|
|
|
case 'abandon':
|
2015-02-20 20:11:14 +00:00
|
|
|
return timeStamp - timing.ready;
|
2014-10-29 01:19:52 +00:00
|
|
|
case 'abandonMidsave':
|
2015-02-20 20:11:14 +00:00
|
|
|
return timeStamp - timing.saveAttempt;
|
2014-10-29 01:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mw.log.warn( 've.init.mw.trackSubscriber: Unrecognized action', action );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:04:32 +00:00
|
|
|
/**
|
|
|
|
* Log the equivalent of an EditAttemptStep event via
|
|
|
|
* [the Metrics Platform](https://wikitech.wikimedia.org/wiki/Metrics_Platform).
|
|
|
|
*
|
|
|
|
* See https://phabricator.wikimedia.org/T309013.
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {string} actionPrefix
|
|
|
|
*/
|
|
|
|
function logEditViaMetricsPlatform( data, actionPrefix ) {
|
|
|
|
var eventName = 'eas.ve.' + actionPrefix;
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
var customData = ve.extendObject(
|
|
|
|
{
|
|
|
|
editor_interface: 'visualeditor',
|
|
|
|
integration: ve.init && ve.init.target && ve.init.target.constructor.static.integrationType || 'page',
|
|
|
|
editing_session_id: editingSessionId
|
|
|
|
},
|
|
|
|
data
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !mw.config.get( 'wgRevisionId' ) ) {
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
customData.revision_id = +$( 'input[name=parentRevId]' ).val() || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
|
|
|
|
delete customData.action;
|
|
|
|
|
|
|
|
// Sampling rate (and therefore whether a stream should oversample) is captured in the
|
|
|
|
// stream config ($wgEventStreams).
|
|
|
|
delete customData.is_oversample;
|
|
|
|
|
|
|
|
// Platform can be derived from the agent_client_platform_family context attribute mixed in
|
|
|
|
// by the JavaScript Metrics Platform Client. The context attribute will be
|
|
|
|
// "desktop_browser" or "mobile_browser" depending on whether the MobileFrontend extension
|
|
|
|
// has signalled that it is enabled.
|
|
|
|
delete customData.platform;
|
|
|
|
|
|
|
|
mw.eventLog.dispatch( eventName, customData );
|
|
|
|
}
|
|
|
|
|
2018-10-11 16:52:14 +00:00
|
|
|
function mwEditHandler( topic, data, timeStamp ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
var action = topic.split( '.' )[ 1 ],
|
2018-10-26 20:46:36 +00:00
|
|
|
actionPrefix = actionPrefixMap[ action ] || action,
|
2021-10-13 12:57:45 +00:00
|
|
|
duration = 0;
|
2015-02-20 20:11:14 +00:00
|
|
|
|
|
|
|
if ( action === 'init' ) {
|
2019-11-13 19:47:36 +00:00
|
|
|
if ( firstInitDone ) {
|
|
|
|
// Regenerate editingSessionId
|
|
|
|
editingSessionId = mw.user.generateRandomSessionId();
|
2021-10-18 23:55:52 +00:00
|
|
|
ve.init.editingSessionId = editingSessionId;
|
2019-11-13 19:47:36 +00:00
|
|
|
}
|
|
|
|
firstInitDone = true;
|
2018-10-15 22:01:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 07:52:20 +00:00
|
|
|
if ( !inEASSample() && !mw.config.get( 'wgWMESchemaEditAttemptStepOversample' ) && !trackdebug ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-15 22:01:26 +00:00
|
|
|
if (
|
2015-02-12 02:35:52 +00:00
|
|
|
action === 'abort' &&
|
|
|
|
( data.type === 'unknown' || data.type === 'unknown-edited' )
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
timing.saveAttempt &&
|
|
|
|
timing.saveSuccess === undefined &&
|
|
|
|
timing.saveFailure === undefined
|
|
|
|
) {
|
|
|
|
data.type = 'abandonMidsave';
|
|
|
|
} else if (
|
|
|
|
timing.init &&
|
|
|
|
timing.ready === undefined
|
|
|
|
) {
|
|
|
|
data.type = 'preinit';
|
|
|
|
} else if ( data.type === 'unknown' ) {
|
|
|
|
data.type = 'nochange';
|
|
|
|
} else {
|
|
|
|
data.type = 'abandon';
|
|
|
|
}
|
2015-02-20 20:11:14 +00:00
|
|
|
}
|
2014-12-06 19:11:02 +00:00
|
|
|
|
2018-10-26 20:46:36 +00:00
|
|
|
// Convert mode=source/visual to interface name
|
2017-12-13 17:56:35 +00:00
|
|
|
if ( data && data.mode ) {
|
2018-10-26 20:46:36 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
data.editor_interface = data.mode === 'source' ? 'wikitext-2017' : 'visualeditor';
|
2017-12-07 11:14:00 +00:00
|
|
|
delete data.mode;
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:50:11 +00:00
|
|
|
if ( !data.platform ) {
|
|
|
|
if ( ve.init && ve.init.target && ve.init.target.constructor.static.platformType ) {
|
|
|
|
data.platform = ve.init.target.constructor.static.platformType;
|
|
|
|
} else {
|
|
|
|
data.platform = 'other';
|
|
|
|
// TODO: outright abort in this case, once we think we've caught everything
|
|
|
|
mw.log.warn( 've.init.mw.trackSubscriber: no target available and no platform specified', action );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:04:32 +00:00
|
|
|
// Schema's kind of a mess of special properties
|
|
|
|
if ( action === 'init' || action === 'abort' || action === 'saveFailure' ) {
|
|
|
|
data[ actionPrefix + '_type' ] = data.type;
|
|
|
|
}
|
|
|
|
if ( action === 'init' || action === 'abort' ) {
|
|
|
|
data[ actionPrefix + '_mechanism' ] = data.mechanism;
|
|
|
|
}
|
|
|
|
if ( action !== 'init' ) {
|
|
|
|
// Schema actually does have an init_timing field, but we don't want to
|
|
|
|
// store it because it's not meaningful.
|
|
|
|
duration = Math.round( computeDuration( action, data, timeStamp ) );
|
|
|
|
data[ actionPrefix + '_timing' ] = duration;
|
|
|
|
}
|
|
|
|
if ( action === 'saveFailure' ) {
|
|
|
|
data[ actionPrefix + '_message' ] = data.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove renamed properties
|
|
|
|
delete data.type;
|
|
|
|
delete data.mechanism;
|
|
|
|
delete data.timing;
|
|
|
|
delete data.message;
|
|
|
|
|
|
|
|
if ( action === 'abort' ) {
|
|
|
|
timing = {};
|
|
|
|
} else {
|
|
|
|
timing[ action ] = timeStamp;
|
|
|
|
}
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
|
|
|
|
addABTestData( data );
|
|
|
|
|
|
|
|
logEditViaMetricsPlatform( data, actionPrefix );
|
|
|
|
|
2018-10-26 20:46:36 +00:00
|
|
|
/* eslint-disable camelcase */
|
2021-11-15 17:25:51 +00:00
|
|
|
var event = ve.extendObject( {
|
2015-02-21 21:02:08 +00:00
|
|
|
version: 1,
|
|
|
|
action: action,
|
2022-08-02 07:52:20 +00:00
|
|
|
is_oversample: !inEASSample(),
|
2018-10-26 20:46:36 +00:00
|
|
|
editor_interface: 'visualeditor',
|
2015-02-21 21:02:08 +00:00
|
|
|
integration: ve.init && ve.init.target && ve.init.target.constructor.static.integrationType || 'page',
|
2018-10-26 20:46:36 +00:00
|
|
|
page_id: mw.config.get( 'wgArticleId' ),
|
|
|
|
page_title: mw.config.get( 'wgPageName' ),
|
|
|
|
page_ns: mw.config.get( 'wgNamespaceNumber' ),
|
2020-02-11 18:36:33 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2020-11-30 17:17:23 +00:00
|
|
|
revision_id: mw.config.get( 'wgRevisionId' ) || +$( 'input[name=parentRevId]' ).val() || 0,
|
2018-10-26 20:46:36 +00:00
|
|
|
editing_session_id: editingSessionId,
|
|
|
|
page_token: mw.user.getPageviewToken(),
|
|
|
|
session_token: mw.user.sessionId(),
|
|
|
|
user_id: mw.user.getId(),
|
|
|
|
user_editcount: mw.config.get( 'wgUserEditCount', 0 ),
|
|
|
|
mw_version: mw.config.get( 'wgVersion' )
|
2015-02-20 20:11:14 +00:00
|
|
|
}, data );
|
2014-10-29 01:19:52 +00:00
|
|
|
|
2015-02-20 20:11:14 +00:00
|
|
|
if ( mw.user.isAnon() ) {
|
2018-10-26 20:46:36 +00:00
|
|
|
event.user_class = 'IP';
|
2015-02-20 20:11:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 17:50:01 +00:00
|
|
|
if ( trackdebug ) {
|
2018-12-11 16:13:06 +00:00
|
|
|
log( topic, duration + 'ms', event );
|
2018-11-27 17:50:01 +00:00
|
|
|
} else {
|
|
|
|
mw.track( 'event.EditAttemptStep', event );
|
|
|
|
}
|
2018-10-11 16:52:14 +00:00
|
|
|
}
|
2014-10-29 01:19:52 +00:00
|
|
|
|
2018-10-11 16:52:14 +00:00
|
|
|
function mwTimingHandler( topic, data ) {
|
2015-02-20 20:11:14 +00:00
|
|
|
// Add type for save errors; not in the topic for stupid historical reasons
|
|
|
|
if ( topic === 'mwtiming.performance.user.saveError' ) {
|
|
|
|
topic = topic + '.' + data.type;
|
2014-10-29 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 20:11:14 +00:00
|
|
|
// Map mwtiming.foo --> timing.ve.foo.mobile
|
2015-04-09 12:48:16 +00:00
|
|
|
topic = topic.replace( /^mwtiming/, 'timing.ve.' + data.targetName );
|
2018-11-27 17:50:01 +00:00
|
|
|
if ( trackdebug ) {
|
2018-12-11 16:13:06 +00:00
|
|
|
log( topic, Math.round( data.duration ) + 'ms' );
|
2018-11-27 17:50:01 +00:00
|
|
|
} else {
|
|
|
|
mw.track( topic, data.duration );
|
|
|
|
}
|
2018-10-11 16:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function activityHandler( topic, data ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var feature = topic.split( '.' )[ 1 ];
|
2018-10-11 16:52:14 +00:00
|
|
|
|
2022-08-02 07:52:20 +00:00
|
|
|
if ( !inVEFUSample() && !mw.config.get( 'wgWMESchemaEditAttemptStepOversample' ) && !trackdebug ) {
|
2018-10-15 22:01:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-06 22:08:26 +00:00
|
|
|
if ( ve.init.target && (
|
|
|
|
ve.init.target.constructor.static.platformType !== 'desktop'
|
|
|
|
) ) {
|
|
|
|
// We want to log activity events when we're also logging to
|
|
|
|
// EditAttemptStep. The EAS events are only fired from DesktopArticleTarget
|
|
|
|
// in this repo. As such, we suppress this unless the current target is at
|
|
|
|
// least inheriting that. (Other tools may fire their own instances of
|
|
|
|
// those events, but probably need to reimplement this anyway for
|
|
|
|
// session-identification reasons.)
|
2019-01-31 00:53:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:13:01 +00:00
|
|
|
/* eslint-disable camelcase */
|
2021-10-13 12:57:45 +00:00
|
|
|
var event = {
|
2018-10-11 16:52:14 +00:00
|
|
|
feature: feature,
|
|
|
|
action: data.action,
|
2020-06-08 17:13:01 +00:00
|
|
|
editingSessionId: editingSessionId,
|
2022-08-02 07:52:20 +00:00
|
|
|
is_oversample: !inVEFUSample(),
|
2020-06-08 17:13:01 +00:00
|
|
|
user_id: mw.user.getId(),
|
|
|
|
user_editcount: mw.config.get( 'wgUserEditCount', 0 ),
|
|
|
|
editor_interface: ve.getProp( ve, 'init', 'target', 'surface', 'mode' ) === 'source' ? 'wikitext-2017' : 'visualeditor',
|
|
|
|
integration: ve.getProp( ve, 'init', 'target', 'constructor', 'static', 'integrationType' ) || 'page',
|
|
|
|
platform: ve.getProp( ve, 'init', 'target', 'constructor', 'static', 'platformType' ) || 'other'
|
2018-10-11 16:52:14 +00:00
|
|
|
};
|
2022-02-02 21:11:56 +00:00
|
|
|
/* eslint-enable camelcase */
|
2021-01-29 23:24:34 +00:00
|
|
|
|
2022-02-11 21:42:17 +00:00
|
|
|
addABTestData( data );
|
|
|
|
|
2018-11-27 17:50:01 +00:00
|
|
|
if ( trackdebug ) {
|
2018-12-11 16:13:06 +00:00
|
|
|
log( topic, event );
|
2018-11-27 17:50:01 +00:00
|
|
|
} else {
|
|
|
|
mw.track( 'event.VisualEditorFeatureUse', event );
|
2022-10-03 13:05:01 +00:00
|
|
|
|
|
|
|
// T309602: Also log via the Metrics Platform:
|
|
|
|
var eventName = 'vefu.' + event.action;
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
var customData = {
|
|
|
|
feature: event.feature,
|
|
|
|
editing_session_id: event.editingSessionId,
|
|
|
|
editor_interface: event.editor_interface,
|
|
|
|
integration: event.integration
|
|
|
|
};
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
|
|
|
|
mw.eventLog.dispatch( eventName, customData );
|
2018-11-27 17:50:01 +00:00
|
|
|
}
|
2018-10-11 16:52:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 21:48:31 +00:00
|
|
|
// Only log events if the WikimediaEvents extension is installed.
|
|
|
|
// It provides variables that the above code depends on and registers the schemas.
|
|
|
|
if ( mw.config.exists( 'wgWMESchemaEditAttemptStepSamplingRate' ) ) {
|
2022-10-03 13:05:01 +00:00
|
|
|
// Ensure 'ext.eventLogging' first, it provides mw.eventLog.randomTokenMatch and
|
|
|
|
// mw.eventLog.dispatch.
|
2019-06-19 23:57:17 +00:00
|
|
|
mw.loader.using( 'ext.eventLogging' ).done( function () {
|
2018-10-15 22:33:32 +00:00
|
|
|
ve.trackSubscribe( 'mwedit.', mwEditHandler );
|
|
|
|
ve.trackSubscribe( 'mwtiming.', mwTimingHandler );
|
2022-08-02 07:52:20 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mw.config.exists( 'wgWMESchemaVisualEditorFeatureUseSamplingRate' ) ) {
|
|
|
|
mw.loader.using( 'ext.eventLogging' ).done( function () {
|
2018-10-15 22:33:32 +00:00
|
|
|
ve.trackSubscribe( 'activity.', activityHandler );
|
|
|
|
} );
|
2018-10-11 16:52:14 +00:00
|
|
|
}
|
2014-10-29 01:19:52 +00:00
|
|
|
|
2016-11-12 14:43:43 +00:00
|
|
|
}() );
|