1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*!
* VisualEditor MediaWiki event subscriber.
*
* Subscribes to ve.track() events and routes them to mw.track().
*
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
( function () {
var actionPrefixMap = {
firstChange: 'first_change',
saveIntent: 'save_intent',
saveAttempt: 'save_attempt',
saveSuccess: 'save_success',
saveFailure: 'save_failure'
},
trackdebug = !!mw.Uri().query.trackdebug,
firstInitDone = false;
function getEditingSessionIdFromRequest() {
return mw.config.get( 'wgWMESchemaEditAttemptStepSessionId' ) ||
mw.Uri().query.editingStatsId;
}
var timing = {};
var editingSessionId = getEditingSessionIdFromRequest() || mw.user.generateRandomSessionId();
ve.init.editingSessionId = editingSessionId;
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 );
}
function inSample() {
// Not using mw.eventLog.inSample() because we need to be able to pass our own editingSessionId
return mw.eventLog.randomTokenMatch(
1 / mw.config.get( 'wgWMESchemaEditAttemptStepSamplingRate' ),
editingSessionId
);
}
function addABTestData( data ) {
// DiscussionTools New Topic A/B test for logged out users
if ( !mw.config.get( 'wgDiscussionToolsABTest' ) ) {
return;
}
if ( mw.config.get( 'wgDiscussionToolsABTestBucket' ) ) {
data.bucket = mw.config.get( 'wgDiscussionToolsABTestBucket' );
}
}
function computeDuration( action, event, timeStamp ) {
if ( event.timing !== undefined ) {
return event.timing;
}
switch ( action ) {
case 'ready':
return timeStamp - timing.init;
case 'loaded':
return timeStamp - timing.init;
case 'firstChange':
return timeStamp - timing.ready;
case 'saveIntent':
return timeStamp - timing.ready;
case 'saveAttempt':
return timeStamp - timing.saveIntent;
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':
switch ( event.type ) {
case 'preinit':
return timeStamp - timing.init;
case 'nochange':
case 'switchwith':
case 'switchwithout':
case 'switchnochange':
case 'abandon':
return timeStamp - timing.ready;
case 'abandonMidsave':
return timeStamp - timing.saveAttempt;
}
}
mw.log.warn( 've.init.mw.trackSubscriber: Unrecognized action', action );
return -1;
}
/**
* 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 );
}
function mwEditHandler( topic, data, timeStamp ) {
var action = topic.split( '.' )[ 1 ],
actionPrefix = actionPrefixMap[ action ] || action,
duration = 0;
if ( action === 'init' ) {
if ( firstInitDone ) {
// Regenerate editingSessionId
editingSessionId = mw.user.generateRandomSessionId();
ve.init.editingSessionId = editingSessionId;
}
firstInitDone = true;
}
if (
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';
}
}
// Convert mode=source/visual to interface name
if ( data && data.mode ) {
// eslint-disable-next-line camelcase
data.editor_interface = data.mode === 'source' ? 'wikitext-2017' : 'visualeditor';
delete data.mode;
}
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 );
}
}
// 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 );
if ( !inSample() && !mw.config.get( 'wgWMESchemaEditAttemptStepOversample' ) && !trackdebug ) {
return;
}
/* eslint-disable camelcase */
var event = ve.extendObject( {
version: 1,
action: action,
is_oversample: !inSample(),
editor_interface: 'visualeditor',
integration: ve.init && ve.init.target && ve.init.target.constructor.static.integrationType || 'page',
page_id: mw.config.get( 'wgArticleId' ),
page_title: mw.config.get( 'wgPageName' ),
page_ns: mw.config.get( 'wgNamespaceNumber' ),
// eslint-disable-next-line no-jquery/no-global-selector
revision_id: mw.config.get( 'wgRevisionId' ) || +$( 'input[name=parentRevId]' ).val() || 0,
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' )
}, data );
if ( mw.user.isAnon() ) {
event.user_class = 'IP';
}
if ( trackdebug ) {
log( topic, duration + 'ms', event );
} else {
mw.track( 'event.EditAttemptStep', event );
}
}
function mwTimingHandler( topic, data ) {
// Add type for save errors; not in the topic for stupid historical reasons
if ( topic === 'mwtiming.performance.user.saveError' ) {
topic = topic + '.' + data.type;
}
// Map mwtiming.foo --> timing.ve.foo.mobile
topic = topic.replace( /^mwtiming/, 'timing.ve.' + data.targetName );
if ( trackdebug ) {
log( topic, Math.round( data.duration ) + 'ms' );
} else {
mw.track( topic, data.duration );
}
}
function activityHandler( topic, data ) {
var feature = topic.split( '.' )[ 1 ];
if ( !inSample() && !mw.config.get( 'wgWMESchemaEditAttemptStepOversample' ) && !trackdebug ) {
return;
}
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.)
return;
}
/* eslint-disable camelcase */
var event = {
feature: feature,
action: data.action,
editingSessionId: editingSessionId,
is_oversample: !inSample(),
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'
};
/* eslint-enable camelcase */
addABTestData( data );
if ( trackdebug ) {
log( topic, event );
} else {
mw.track( 'event.VisualEditorFeatureUse', event );
}
}
// 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' ) ) {
// Ensure 'ext.eventLogging' first, it provides mw.eventLog.randomTokenMatch.
mw.loader.using( 'ext.eventLogging' ).done( function () {
ve.trackSubscribe( 'mwedit.', mwEditHandler );
ve.trackSubscribe( 'mwtiming.', mwTimingHandler );
ve.trackSubscribe( 'activity.', activityHandler );
} );
}
}() );
|