mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-24 07:34:11 +00:00
118 lines
2.8 KiB
JavaScript
118 lines
2.8 KiB
JavaScript
( function ( $, mw ) {
|
|
|
|
/**
|
|
* @class mw.popups.eventLogging
|
|
* @singleton
|
|
*/
|
|
var eventLogging = {};
|
|
|
|
/**
|
|
* Unix timestamp of when the popup was rendered
|
|
* @property time
|
|
*/
|
|
eventLogging.time = undefined;
|
|
|
|
/**
|
|
* How long was the popup open in milliseconds
|
|
* @property {Number} duration
|
|
*/
|
|
eventLogging.duration = undefined;
|
|
|
|
/**
|
|
* Was the popup clicked, middle clicked or dismissed
|
|
* @property {String} action
|
|
*/
|
|
eventLogging.action = undefined;
|
|
|
|
/**
|
|
* Logs different actions such as meta and shift click on the popup
|
|
* Is bound to the `click` event
|
|
*
|
|
* @method logClick
|
|
* @param {Object} event
|
|
*/
|
|
eventLogging.logClick = function ( event ) {
|
|
if ( event.which === 2 ) { // middle click
|
|
eventLogging.action = 'opened in new tab';
|
|
} else if ( event.which === 1 ) {
|
|
if ( event.ctrlKey || event.metaKey ) {
|
|
eventLogging.action = 'opened in new tab';
|
|
} else if ( event.shiftKey ) {
|
|
eventLogging.action = 'opened in new window';
|
|
} else {
|
|
eventLogging.action = 'opened in same tab';
|
|
eventLogging.duration = mw.now() - eventLogging.time;
|
|
eventLogging.logEvent( mw.popups.render.currentLink.attr( 'href' ) );
|
|
event.preventDefault();
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Logs the popup event as defined in the following schema-
|
|
* https://meta.wikimedia.org/wiki/Schema:Popups
|
|
* If `href` is passed it redirects to that location after the event is logged.
|
|
*
|
|
* @method logEvent
|
|
* @param {String} href
|
|
* @return {Boolean} logged Whether or not the event was logged
|
|
*/
|
|
eventLogging.logEvent = function ( href ) {
|
|
if ( mw.eventLog === undefined ) {
|
|
return false;
|
|
}
|
|
|
|
var
|
|
deferred = $.Deferred(),
|
|
event = {
|
|
'duration': Math.round( eventLogging.duration ),
|
|
'action': eventLogging.action
|
|
};
|
|
|
|
if ( eventLogging.sessionId !== null ) {
|
|
event.sessionId = eventLogging.sessionId;
|
|
}
|
|
|
|
if ( href ) {
|
|
deferred.always( function () {
|
|
location.href = href;
|
|
} );
|
|
}
|
|
|
|
mw.eventLog.logEvent( 'Popups', event ).then( deferred.resolve, deferred.reject );
|
|
|
|
// reset
|
|
eventLogging.time = undefined;
|
|
eventLogging.duration = undefined;
|
|
eventLogging.action = undefined;
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Generates a unique sessionId or pulls an existing one from localStorage
|
|
*
|
|
* @method getSessionsId
|
|
* @return {String} sessionId
|
|
*/
|
|
eventLogging.getSessionId = function () {
|
|
var sessionId = null;
|
|
try {
|
|
sessionId = localStorage.getItem( 'popupsSessionId' );
|
|
if ( sessionId === null ) {
|
|
sessionId = mw.user.generateRandomSessionId();
|
|
localStorage.setItem( 'popupsSessionId', sessionId );
|
|
}
|
|
} catch ( e ) {}
|
|
return sessionId;
|
|
};
|
|
|
|
/**
|
|
* @property sessionId
|
|
*/
|
|
eventLogging.sessionId = eventLogging.getSessionId();
|
|
|
|
mw.popups.eventLogging = eventLogging;
|
|
|
|
} ) ( jQuery, mediaWiki );
|