summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/resources/uw.EventFlowLogger.js
blob: 0d4f81104fe451e56628fce0ed70df2f9cfcbd8d (plain)
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
/*
 * This file is part of the MediaWiki extension UploadWizard.
 *
 * UploadWizard is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * UploadWizard is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with UploadWizard.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, uw ) {
	/**
	 * Event logging helper for funnel analysis. Should be instantiated at the very beginning; uses internal state
	 * to link events together.
	 * @class uw.EventFlowLogger
	 * @constructor
	 */
	uw.EventFlowLogger = function UWEventFlowLogger() {
	};

	/**
	 * Returns a string identifying this upload session for analytics purposes.
	 * Since UploadWizard is currently implemented as a single-page application, this is just
	 * a number regenerated on every pageview. It's stored as a string to avoid overflow problems
	 * on the backend.
	 *
	 * @private
	 * @return {string}
	 */
	uw.EventFlowLogger.prototype.getFlowId = function () {
		var rnd;

		if ( !uw.EventFlowLogger.flowId ) {
			rnd = '00' + Math.floor( Math.random() * 1000 );
			uw.EventFlowLogger.flowId = new Date().getTime() + rnd.substr( rnd.length - 3, 3 );
		}
		return uw.EventFlowLogger.flowId;
	};

	/**
	 * Returns a number identifying this event's position in the event flow.
	 * (I.e. (flowId, flowPosition) will uniquely identify an event, with the positions for a given
	 * flowId going 1..N.)
	 *
	 * @private
	 * @return {number}
	 */
	uw.EventFlowLogger.prototype.getFlowPosition = function () {
		uw.EventFlowLogger.flowPosition = ( uw.EventFlowLogger.flowPosition || 0 ) + 1;
		return uw.EventFlowLogger.flowPosition;
	};

	/**
	 * Does the work of logging a step.
	 *
	 * @private
	 * @param {'tutorial'|'file'|'deeds'|'details'|'thanks'} step
	 * @param {boolean} [skipped=false]
	 * @param {Object} [extraData] Extra data passed to the log.
	 */
	uw.EventFlowLogger.prototype.performStepLog = function ( step, skipped, extraData ) {
		var data = extraData || {};

		data.step = step;

		if ( skipped === true ) {
			data.skipped = true;
		}

		this.log( 'UploadWizardStep', data );
	};

	/**
	 * Logs arbitrary data. This is for internal use, you should call one of the more specific functions.
	 *
	 * @protected
	 * @param {string} schema EventLogger schema name
	 * @param {Object} data event data (without flowId)
	 */
	uw.EventFlowLogger.prototype.log = function ( schema, data ) {
		data.flowId = this.getFlowId();
		data.flowPosition = this.getFlowPosition();
		mw.track( 'event.' + schema, data );
	};

	/**
	 * Logs entering into a given step of the upload process.
	 *
	 * @param {'tutorial'|'file'|'deeds'|'details'|'thanks'} step
	 * @param {Object} [extraData] Extra data to pass along in the log.
	 */
	uw.EventFlowLogger.prototype.logStep = function ( step, extraData ) {
		this.performStepLog( step, false, extraData );
	};

	/**
	 * Logs skipping a given step of the upload process.
	 *
	 * @param {'tutorial'|'file'|'deeds'|'details'|'thanks'} step
	 */
	uw.EventFlowLogger.prototype.logSkippedStep = function ( step ) {
		this.performStepLog( step, true );
	};

	/**
	 * Logs user action on the tutorial page for UploadWizard.
	 *
	 * @param {'load'|'helpdesk-click'|'skip-check'|'skip-uncheck'|'continue'} type
	 */
	uw.EventFlowLogger.prototype.logTutorialAction = function ( type ) {
		var payload, thisUri;

		payload = {};
		payload.username = mw.config.get( 'wgUserName' );
		payload.language = mw.config.get( 'wgUserLanguage' );

		thisUri = new mw.Uri( window.location.href, { overrideKeys: true } );
		if ( thisUri.query.uselang ) {
			payload.language = thisUri.query.uselang;
		}

		payload.action = type;
		mw.track( 'event.UploadWizardTutorialActions', payload );
	};

	/**
	 * Logs an event.
	 *
	 * @param {string} name Event name. Recognized names:
	 *  - upload-button-clicked
	 *  - flickr-upload-button-clicked
	 *  - retry-uploads-button-clicked
	 *  - continue-clicked
	 *  - continue-anyway-clicked
	 *  - leave-page
	 */
	uw.EventFlowLogger.prototype.logEvent = function ( name ) {
		this.log( 'UploadWizardFlowEvent', { event: name } );
	};

	uw.EventFlowLogger.prototype.logError = function ( step, data ) {
		this.log( 'UploadWizardErrorFlowEvent', {
			step: step,
			code: data.code,
			message: String( data.message ) // could be a function which kills EventLogging
		} );
	};

	uw.EventFlowLogger.prototype.logApiError = function ( step, result ) {
		var code, message;
		if ( !result ) {
			code = 'api/empty';
		} else if ( result.error ) {
			code = 'api/error/' + result.error.code;
		} else if ( result.upload ) {
			code = 'api/warning/' + Object.keys( result.upload.warnings || {} ).sort().join( ',' );
		} else {
			code = '???';
		}
		if ( result && result.upload && result.upload.imageinfo ) {
			// This can contain stupid amounts of data and exceed the length of what EventLogging can log.
			// Let's hope we won't need to look at anything in there.
			result = OO.copy( result );
			delete result.upload.imageinfo;
		}
		try {
			message = JSON.stringify( result );
		} catch ( er ) {
			message = String( result );
		}
		this.log( 'UploadWizardErrorFlowEvent', {
			step: step,
			code: code,
			message: message
		} );
	};

	/**
	 * Sets up logging for global javascript errors.
	 */
	uw.EventFlowLogger.prototype.installExceptionLogger = function () {
		var self = this;

		function toNumber( val ) {
			var num = parseInt( val, 10 );
			if ( isNaN( num ) ) {
				return undefined;
			}
			return num;
		}

		mw.trackSubscribe( 'global.error', function ( topic, data ) {
			self.log( 'UploadWizardExceptionFlowEvent', {
				message: data.errorMessage,
				url: data.url,
				line: toNumber( data.lineNumber ),
				column: toNumber( data.columnNumber ),
				stack: undefined // T91347
			} );
		} );

		mw.trackSubscribe( 'resourceloader.exception', function ( topic, data ) {
			// Ignore noise about 'localStorage' being undefined or module store exceeding the quota
			if (
				data.source === 'store-localstorage-init' ||
				data.source === 'store-localstorage-json' ||
				data.source === 'store-localstorage-update'
			) {
				return;
			}

			self.log( 'UploadWizardExceptionFlowEvent', {
				message: data.exception.message,
				url: 'resourceLoader://' + data.source + '/' + data.module, // Bleh
				line: 0,
				column: 0,
				stack: undefined // T91347
			} );
		} );

		mw.trackSubscribe( 'mediawiki.jqueryMsg', function ( topic, data ) {
			self.log( 'UploadWizardExceptionFlowEvent', {
				message: data.errorMessage,
				url: 'jqueryMsg://' + data.messageKey, // Yeah, we're abusing the schema
				line: 0,
				column: 0,
				stack: undefined
			} );
		} );
	};

	/**
	 * Logs an upload event.
	 *
	 * @param {string} name Event name. Recognized names:
	 *  - upload-started
	 *  - upload-succeeded
	 *  - upload-failed
	 *  - upload-removed
	 *  - uploads-added
	 * @param {Object} data
	 * @param {string} data.extension file extension
	 * @param {number} data.quantity number of files added
	 * @param {number} data.size file size in bytes (will be anonymized)
	 * @param {number} data.duration upload duration in seconds
	 * @param {string} data.error upload error string
	 */
	uw.EventFlowLogger.prototype.logUploadEvent = function ( name, data ) {
		data.event = name;

		if ( 'size' in data ) {
			// anonymize size by rounding to closest number with 1 significant digit
			data.size = parseFloat( Number( data.size ).toPrecision( 1 ), 10 );
		}

		this.log( 'UploadWizardUploadFlowEvent', data );
	};

	/**
	 * If `err` is a Firefox 'NS_ERROR_NOT_AVAILABLE' exception, such as those occasionally spuriously
	 * throw when calling `canvas.getContext( '2d' ).drawImage( … )`, log it for future debugging
	 * along with more data about the image that failed to be drawn. See T136831.
	 *
	 * @param {Error} err
	 * @param {Object} img
	 */
	uw.EventFlowLogger.prototype.maybeLogFirefoxCanvasException = function ( err, img ) {
		if ( err.name !== 'NS_ERROR_NOT_AVAILABLE' ) {
			return;
		}

		this.log( 'UploadWizardExceptionFlowEvent', {
			message: ( err.message || '' ),
			url: 'debug://NS_ERROR_NOT_AVAILABLE',
			line: 0,
			column: 0,
			stack: JSON.stringify( {
				type: img.tagName,
				url: String( img.src ).slice( 0, 100 ),
				// Both of these should be truthy if the image is actually loaded
				complete: img.complete,
				naturalWidth: img.naturalWidth
			} )
		} );
	};

	// FIXME
	uw.eventFlowLogger = new uw.EventFlowLogger();
	uw.eventFlowLogger.installExceptionLogger();
}( mediaWiki, mediaWiki.uploadWizard ) );