summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UniversalLanguageSelector/resources/js/ext.uls.eventlogger.js
blob: 02cfa88eec04f6ef669245783ea33ce120ed0dfb (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
/*!
 * ULS Event logger
 *
 * See https://meta.wikimedia.org/wiki/Schema:UniversalLanguageSelector
 *
 * @private
 * @since 2013.08
 *
 * Copyright (C) 2012-2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
 * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
 * contributors. See CREDITS for a list.
 *
 * UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
 * have to do anything special to choose one license or the other and you don't
 * have to notify anyone which license you are using. You are free to use
 * UniversalLanguageSelector in commercial projects as long as the copyright
 * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
 *
 * @file
 * @ingroup Extensions
 * @licence GNU General Public Licence 2.0 or later
 * @licence MIT License
 */

( function () {
	'use strict';

	/**
	 * Try to emit an EventLogging event with schema 'UniversalLanguageSelector'.
	 *
	 * If EventLogging is not installed, this simply does nothing.
	 *
	 * @param {Object} event Event action and optional fields
	 */
	function log( event ) {
		event = $.extend( {
			version: 1,
			token: mw.user.id(),
			contentLanguage: mw.config.get( 'wgContentLanguage' ),
			interfaceLanguage: mw.config.get( 'wgUserLanguage' )
		}, event );

		mw.track( 'event.UniversalLanguageSelector', event );
	}

	/**
	 * Log language settings open
	 *
	 * @param {string} context Where it was opened from
	 */
	function ulsSettingsOpen( context ) {
		log( {
			action: 'settings-open',
			context: context
		} );
	}

	/**
	 * Log language revert
	 *
	 * @param {jQuery.Deferred} deferred
	 */
	function ulsLanguageRevert( deferred ) {
		log( { action: 'ui-lang-revert' } ).always( deferred.resolve() );
	}

	/**
	 * Log IME disabling
	 *
	 * @param {string} context Where the setting was changed.
	 */
	function disableIME( context ) {
		log( { action: 'ime-disable', context: context } );
	}

	/**
	 * Log IME enabling
	 *
	 * @param {string} context Where the setting was changed.
	 */
	function enableIME( context ) {
		log( { action: 'ime-enable', context: context } );
	}

	/**
	 * Log IME change
	 *
	 * @param {string} inputMethod
	 */
	function changeIME( inputMethod ) {
		log( {
			action: 'ime-change',
			inputMethod: inputMethod
		} );
	}

	/**
	 * Log login link click in display settings.
	 *
	 * @param {jQuery.Deferred} deferred
	 */
	function loginClick( deferred ) {
		log( { action: 'login-click' } );
		deferred.resolve();
	}

	/**
	 * Log when "More languages" item in IME menu is clicked.
	 */
	function imeMoreLanguages() {
		log( {
			action: 'more-languages-access',
			context: 'ime'
		} );
	}

	/**
	 * Log interface language change
	 *
	 * @param {string} language language code
	 * @param {jQuery.Deferred} deferred
	 */
	function interfaceLanguageChange( language, deferred ) {
		var logParams = {
			action: 'language-change',
			context: 'interface',
			interfaceLanguage: language
		};

		log( logParams );
		deferred.resolve();
	}

	/**
	 * More languages in display settings is clicked
	 */
	function interfaceMoreLanguages() {
		log( {
			action: 'more-languages-access',
			context: 'interface'
		} );
	}

	/**
	 * Log font preference changes
	 *
	 * @param {string} context Either 'interface' or 'content'
	 * @param {string} language
	 * @param {string} font
	 */
	function fontChange( context, language, font ) {
		var logParams = {
			action: 'font-change',
			context: context
		};

		if ( context === 'interface' ) {
			logParams.interfaceFont = font;
			// Override in case the user changed the ui language but hasn't applied it yet
			logParams.interfaceLanguage = language;
		} else {
			logParams.contentFont = font;
		}

		log( logParams );
	}

	/**
	 * Log webfonts disabling
	 *
	 * @param {string} context Where the setting was changed.
	 */
	function disableWebfonts( context ) {
		log( { action: 'webfonts-disable', context: context } );
	}

	/**
	 * Log webfonts enabling
	 *
	 * @param {string} context Where the setting was changed.
	 */
	function enableWebfonts( context ) {
		log( { action: 'webfonts-enable', context: context } );
	}

	/**
	 * Log search strings which produce no search results.
	 *
	 * @param {jQuery.event} event The original event
	 * @param {Object} data Information about the failed search
	 */
	function noSearchResults( event, data ) {
		log( {
			action: 'no-search-results',
			context: data.query,
			ulsPurpose: data.ulsPurpose,
			title: mw.config.get( 'wgPageName' )
		} );
	}

	/**
	 * Start listening for event logging
	 */
	function listen() {
		// Register handlers for event logging triggers
		mw.hook( 'mw.uls.settings.open' ).add( ulsSettingsOpen );
		mw.hook( 'mw.uls.language.revert' ).add( ulsLanguageRevert );
		mw.hook( 'mw.uls.ime.enable' ).add( enableIME );
		mw.hook( 'mw.uls.ime.disable' ).add( disableIME );
		mw.hook( 'mw.uls.ime.change' ).add( changeIME );
		mw.hook( 'mw.uls.login.click' ).add( loginClick );
		mw.hook( 'mw.uls.ime.morelanguages' ).add( imeMoreLanguages );
		mw.hook( 'mw.uls.interface.morelanguages' ).add( interfaceMoreLanguages );
		mw.hook( 'mw.uls.interface.language.change' ).add( interfaceLanguageChange );
		mw.hook( 'mw.uls.font.change' ).add( fontChange );
		mw.hook( 'mw.uls.webfonts.enable' ).add( enableWebfonts );
		mw.hook( 'mw.uls.webfonts.disable' ).add( disableWebfonts );

		$( 'body' ).on(
			'noresults.uls',
			'.uls-menu .uls-languagefilter',
			noSearchResults
		);
	}

	listen();
}() );