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-06-19 11:15:25 +00:00
|
|
|
/**
|
|
|
|
* Gets whether the UA supports [the Beacon API][0].
|
|
|
|
*
|
|
|
|
* [0]: https://www.w3.org/TR/beacon/
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function supportsBeacon() {
|
|
|
|
return $.isFunction( window.navigator.sendBeacon );
|
2017-01-27 21:10:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 17:58:30 +00:00
|
|
|
/**
|
2017-06-19 11:15:25 +00:00
|
|
|
* Gets whether the instrumentation is enabled for the user.
|
|
|
|
*
|
|
|
|
* We sample at the feature level, not by pageview. If the instrumentation is
|
|
|
|
* enabled for the user, then it's enabled for the duration of their session.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
2015-10-29 17:58:30 +00:00
|
|
|
*/
|
2017-06-19 11:15:25 +00:00
|
|
|
function isEnabledForCurrentUser() {
|
|
|
|
|
|
|
|
// TODO: Rename this and other instances to bucketingRate.
|
|
|
|
var samplingRate = mw.config.get( 'wgRelatedArticlesLoggingSamplingRate', 0 );
|
|
|
|
|
|
|
|
if ( !supportsBeacon() ) {
|
|
|
|
return false;
|
2015-10-29 17:58:30 +00:00
|
|
|
}
|
2017-06-19 11:15:25 +00:00
|
|
|
|
|
|
|
return mw.experiments.getBucket( {
|
|
|
|
name: 'ext.relatedArticles.instrumentation',
|
|
|
|
enabled: true,
|
|
|
|
buckets: {
|
|
|
|
control: 1 - samplingRate,
|
|
|
|
A: samplingRate
|
|
|
|
}
|
|
|
|
}, mw.user.sessionId() ) === 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isEnabledForCurrentUser() ) {
|
|
|
|
return;
|
2015-10-29 17:58:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:15:25 +00:00
|
|
|
// ---
|
|
|
|
// BEGIN INSTRUMENTATION
|
|
|
|
// ---
|
|
|
|
|
2017-03-01 21:36:49 +00:00
|
|
|
schemaRelatedPages = new mw.eventLog.Schema(
|
|
|
|
'RelatedArticles',
|
2017-06-19 11:15:25 +00:00
|
|
|
|
|
|
|
// The instrumentation is enabled for the user's session. DON'T SAMPLE AT
|
|
|
|
// THE EVENT LEVEL.
|
|
|
|
1,
|
|
|
|
|
2017-03-01 21:36:49 +00:00
|
|
|
{
|
|
|
|
pageId: mw.config.get( 'wgArticleId' ),
|
2017-04-07 20:21:12 +00:00
|
|
|
skin: ( skin === 'minerva' ) ? skin + '-' + mw.config.get( 'wgMFMode' ) : skin,
|
2017-06-06 19:42:06 +00:00
|
|
|
userSessionToken: mw.user.sessionId()
|
2017-03-01 21:36:49 +00:00
|
|
|
}
|
|
|
|
);
|
2015-10-29 17:58:30 +00:00
|
|
|
|
2017-06-19 11:15:25 +00:00
|
|
|
/**
|
|
|
|
* Log when ReadMore is seen by the user
|
|
|
|
*/
|
|
|
|
function logReadMoreSeen() {
|
|
|
|
if ( mw.viewport.isElementInViewport( $readMore.get( 0 ) ) ) {
|
|
|
|
$window.off( 'scroll', logReadMoreSeen );
|
|
|
|
schemaRelatedPages.log( { eventName: 'seen' } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ) );
|