2015-10-29 17:58:30 +00:00
|
|
|
// See https://meta.wikimedia.org/wiki/Schema:RelatedArticles
|
2017-03-01 21:36:49 +00:00
|
|
|
( function ( $, mw ) {
|
2015-10-29 17:58:30 +00:00
|
|
|
var $readMore,
|
2015-11-14 00:03:39 +00:00
|
|
|
schemaRelatedPages,
|
2015-10-29 17:58:30 +00:00
|
|
|
skin = mw.config.get( 'skin' ),
|
|
|
|
$window = $( window );
|
|
|
|
|
2017-01-27 21:10:46 +00:00
|
|
|
if ( !$.isFunction( navigator.sendBeacon ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:58:30 +00:00
|
|
|
/**
|
|
|
|
* Log when ReadMore is seen by the user
|
|
|
|
*/
|
|
|
|
function logReadMoreSeen() {
|
2016-04-07 16:23:38 +00:00
|
|
|
if ( mw.viewport.isElementInViewport( $readMore.get( 0 ) ) ) {
|
2015-10-29 17:58:30 +00:00
|
|
|
$window.off( 'scroll', logReadMoreSeen );
|
2015-11-14 00:03:39 +00:00
|
|
|
schemaRelatedPages.log( { eventName: 'seen' } );
|
2015-10-29 17:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:36:49 +00:00
|
|
|
schemaRelatedPages = new mw.eventLog.Schema(
|
|
|
|
'RelatedArticles',
|
|
|
|
// not sampled if the config variable is not set
|
|
|
|
mw.config.get( 'wgRelatedArticlesLoggingSamplingRate', 0 ),
|
|
|
|
{
|
|
|
|
pageId: mw.config.get( 'wgArticleId' ),
|
|
|
|
skin: ( skin === 'minerva' ) ? skin + '-' + mw.config.get( 'wgMFMode' ) : skin,
|
|
|
|
// We cannot depend on the uniqueness of mw.user.generateRandomSessionId(),
|
|
|
|
// thus append the timestamp. See mw.user documentation for more info.
|
|
|
|
userSessionToken: mw.user.generateRandomSessionId() +
|
2015-10-29 17:58:30 +00:00
|
|
|
( new Date() ).getTime().toString()
|
2017-03-01 21:36:49 +00:00
|
|
|
}
|
|
|
|
);
|
2015-10-29 17:58:30 +00:00
|
|
|
|
2017-03-01 21:36:49 +00:00
|
|
|
mw.trackSubscribe( 'ext.relatedArticles.logEnabled', function ( _, data ) {
|
|
|
|
schemaRelatedPages.log( {
|
|
|
|
eventName: data.isEnabled ? 'feature-enabled' : 'feature-disabled'
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.trackSubscribe( 'ext.relatedArticles.logReady', function ( _, data ) {
|
2015-10-29 17:58:30 +00:00
|
|
|
$readMore = data.$readMore;
|
|
|
|
|
|
|
|
// log ready
|
2015-11-14 00:03:39 +00:00
|
|
|
schemaRelatedPages.log( { eventName: 'ready' } );
|
2015-10-29 17:58:30 +00:00
|
|
|
|
|
|
|
// log when ReadMore is seen by the user
|
|
|
|
$window.on(
|
|
|
|
'scroll',
|
|
|
|
$.debounce( 250, logReadMoreSeen )
|
|
|
|
);
|
|
|
|
logReadMoreSeen();
|
|
|
|
|
|
|
|
// track the clicked event
|
|
|
|
// TODO: This should be moved into the PageList component or, preferably, the CardList/Card views.
|
|
|
|
$readMore.on( 'click', 'a', function () {
|
|
|
|
var index = $( this ).parents( 'li' ).index();
|
|
|
|
|
2015-11-14 00:03:39 +00:00
|
|
|
schemaRelatedPages.log( {
|
2015-10-29 17:58:30 +00:00
|
|
|
eventName: 'clicked',
|
|
|
|
clickIndex: index + 1
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-12-07 18:19:16 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|