Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/init/ve.init.mw.ArticleTargetEvents.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki Initialization class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Initialization MediaWiki Article Target Analytics.
10 *
11 * @class
12 *
13 * @constructor
14 * @param {ve.init.mw.ArticleTarget} target Target class to log events for
15 */
16 ve.init.mw.ArticleTargetEvents = function VeInitMwArticleTargetEvents( target ) {
17 this.target = target;
18 this.timings = { saveRetries: 0 };
19 // Events
20 this.target.connect( this, {
21 saveWorkflowBegin: 'onSaveWorkflowBegin',
22 saveWorkflowEnd: 'onSaveWorkflowEnd',
23 saveInitiated: 'onSaveInitiated',
24 save: 'onSaveComplete',
25 saveReview: 'onSaveReview',
26 saveError: 'trackSaveError',
27 surfaceReady: 'onSurfaceReady',
28 showChanges: 'onShowChanges',
29 showChangesError: 'onShowChangesError',
30 noChanges: 'onNoChanges',
31 serializeComplete: 'onSerializeComplete',
32 serializeError: 'onSerializeError'
33 } );
34 };
35
36 /**
37 * Target specific ve.track wrapper
38 *
39 * @param {string} topic Event name
40 * @param {Object} data Additional data describing the event, encoded as an object
41 */
42 ve.init.mw.ArticleTargetEvents.prototype.track = function ( topic, data ) {
43 ve.track( topic, ve.extendObject( {
44 mode: this.target.surface ? this.target.surface.getMode() : this.target.getDefaultMode()
45 }, data ) );
46 };
47
48 /**
49 * Target specific ve.track wrapper, focused on timing
50 *
51 * @param {string} topic Event name
52 * @param {Object} data Additional data describing the event, encoded as an object
53 */
54 ve.init.mw.ArticleTargetEvents.prototype.trackTiming = function ( topic, data ) {
55 if ( topic.indexOf( 'performance.system.serializeforcache' ) === 0 ) {
56 // HACK: track serializeForCache duration here, because there's no event for that
57 this.timings.serializeForCache = data.duration;
58 }
59
60 // Add type for save errors; not in the topic for stupid historical reasons
61 if ( topic === 'performance.user.saveError' ) {
62 topic = topic + '.' + data.type;
63 }
64
65 topic = 'timing.ve.' + this.target.constructor.static.trackingName + '.' + topic;
66
67 mw.track( topic, data.duration );
68 };
69
70 /**
71 * Track when the user makes their first transaction
72 */
73 ve.init.mw.ArticleTargetEvents.prototype.onFirstTransaction = function () {
74 this.track( 'editAttemptStep', { action: 'firstChange' } );
75
76 this.trackTiming( 'behavior.firstTransaction', {
77 duration: ve.now() - this.timings.surfaceReady
78 } );
79 };
80
81 /**
82 * Track when user begins the save workflow
83 */
84 ve.init.mw.ArticleTargetEvents.prototype.onSaveWorkflowBegin = function () {
85 this.timings.saveWorkflowBegin = ve.now();
86 this.trackTiming( 'behavior.lastTransactionTillSaveDialogOpen', {
87 duration: this.timings.saveWorkflowBegin - this.timings.lastTransaction
88 } );
89 this.track( 'editAttemptStep', { action: 'saveIntent' } );
90 };
91
92 /**
93 * Track when user ends the save workflow
94 */
95 ve.init.mw.ArticleTargetEvents.prototype.onSaveWorkflowEnd = function () {
96 this.trackTiming( 'behavior.saveDialogClose', { duration: ve.now() - this.timings.saveWorkflowBegin } );
97 this.timings.saveWorkflowBegin = null;
98 };
99
100 /**
101 * Track when document save is initiated
102 */
103 ve.init.mw.ArticleTargetEvents.prototype.onSaveInitiated = function () {
104 this.timings.saveInitiated = ve.now();
105 this.timings.saveRetries++;
106 this.trackTiming( 'behavior.saveDialogOpenTillSave', {
107 duration: this.timings.saveInitiated - this.timings.saveWorkflowBegin
108 } );
109 this.track( 'editAttemptStep', { action: 'saveAttempt' } );
110 };
111
112 /**
113 * Track when the save is complete
114 *
115 * @param {Object} data Save data from the API, see ve.init.mw.ArticleTarget#saveComplete
116 */
117 ve.init.mw.ArticleTargetEvents.prototype.onSaveComplete = function ( data ) {
118 this.trackTiming( 'performance.user.saveComplete', { duration: ve.now() - this.timings.saveInitiated } );
119 this.timings.saveRetries = 0;
120 this.track( 'editAttemptStep', {
121 action: 'saveSuccess',
122 timing: ve.now() - this.timings.saveInitiated + ( this.timings.serializeForCache || 0 ),
123 // eslint-disable-next-line camelcase
124 revision_id: data.newrevid
125 } );
126 };
127
128 /**
129 * Track a save error by type
130 *
131 * @param {string} code Error code
132 */
133 ve.init.mw.ArticleTargetEvents.prototype.trackSaveError = function ( code ) {
134 // Maps error codes to editAttemptStep types
135 const typeMap = {
136 badtoken: 'userBadToken',
137 assertanonfailed: 'userNewUser',
138 assertuserfailed: 'userNewUser',
139 assertnameduserfailed: 'userNewUser',
140 'abusefilter-disallowed': 'extensionAbuseFilter',
141 'abusefilter-warning': 'extensionAbuseFilter',
142 captcha: 'extensionCaptcha',
143 spamblacklist: 'extensionSpamBlacklist',
144 'titleblacklist-forbidden': 'extensionTitleBlacklist',
145 pagedeleted: 'editPageDeleted',
146 editconflict: 'editConflict'
147 },
148 // Types that are logged as performance.user.saveError.{code}
149 // (for historical reasons; this sucks)
150 specialTypes = [ 'editconflict' ];
151
152 let key = 'performance.user.saveError';
153 if ( specialTypes.indexOf( code ) !== -1 ) {
154 key += '.' + code;
155 }
156 this.trackTiming( key, {
157 duration: ve.now() - this.timings.saveInitiated,
158 type: code
159 } );
160
161 this.track( 'editAttemptStep', {
162 action: 'saveFailure',
163 message: code,
164 type: typeMap[ code ] || 'responseUnknown',
165 timing: ve.now() - this.timings.saveInitiated + ( this.timings.serializeForCache || 0 )
166 } );
167 };
168
169 /**
170 * Record activation having started.
171 *
172 * @param {number} [startTime] Timestamp activation started. Defaults to current time
173 */
174 ve.init.mw.ArticleTargetEvents.prototype.trackActivationStart = function ( startTime ) {
175 this.timings.activationStart = startTime || ve.now();
176 };
177
178 /**
179 * Record activation being complete.
180 */
181 ve.init.mw.ArticleTargetEvents.prototype.trackActivationComplete = function () {
182 this.trackTiming( 'performance.system.activation', { duration: ve.now() - this.timings.activationStart } );
183 };
184
185 /**
186 * Record the time of the last transaction in response to a 'transact' event on the document.
187 */
188 ve.init.mw.ArticleTargetEvents.prototype.recordLastTransactionTime = function () {
189 this.timings.lastTransaction = ve.now();
190 };
191
192 /**
193 * Track time elapsed from beginning of save workflow to review
194 */
195 ve.init.mw.ArticleTargetEvents.prototype.onSaveReview = function () {
196 this.timings.saveReview = ve.now();
197 this.trackTiming( 'behavior.saveDialogOpenTillReview', {
198 duration: this.timings.saveReview - this.timings.saveWorkflowBegin
199 } );
200 };
201
202 ve.init.mw.ArticleTargetEvents.prototype.onSurfaceReady = function () {
203 this.timings.surfaceReady = ve.now();
204 this.target.surface.getModel().getDocument().connect( this, {
205 transact: 'recordLastTransactionTime'
206 } ).once( 'transact', this.onFirstTransaction.bind( this ) );
207 };
208
209 /**
210 * Track when the user enters the review workflow
211 */
212 ve.init.mw.ArticleTargetEvents.prototype.onShowChanges = function () {
213 this.trackTiming( 'performance.user.reviewComplete', { duration: ve.now() - this.timings.saveReview } );
214 };
215
216 /**
217 * Track when the diff request fails in the review workflow
218 */
219 ve.init.mw.ArticleTargetEvents.prototype.onShowChangesError = function () {
220 this.trackTiming( 'performance.user.reviewError', { duration: ve.now() - this.timings.saveReview } );
221 };
222
223 /**
224 * Track when the diff request detects no changes
225 */
226 ve.init.mw.ArticleTargetEvents.prototype.onNoChanges = function () {
227 this.trackTiming( 'performance.user.reviewComplete', { duration: ve.now() - this.timings.saveReview } );
228 };
229
230 /**
231 * Track when serialization is complete in review workflow
232 */
233 ve.init.mw.ArticleTargetEvents.prototype.onSerializeComplete = function () {
234 this.trackTiming( 'performance.user.reviewComplete', { duration: ve.now() - this.timings.saveReview } );
235 };
236
237 /**
238 * Track when there is a serialization error
239 */
240 ve.init.mw.ArticleTargetEvents.prototype.onSerializeError = function () {
241 if ( this.timings.saveWorkflowBegin ) {
242 // This function can be called by the switch to wikitext button as well, so only log
243 // reviewError if we actually got here from the save workflow
244 this.trackTiming( 'performance.user.reviewError', { duration: ve.now() - this.timings.saveReview } );
245 }
246 };