mediawiki-extensions-Relate.../resources/ext.relatedArticles.readMore/eventLogging.js
Baha cfc73ff5c9 Remove the isElementInViewport function
Use mw.viewport.isElementInViewport instead.

Bug: T124317
Change-Id: I621aa4c21d673d8d431bb38ba094271b570ca93c
2016-04-07 12:23:38 -04:00

58 lines
1.6 KiB
JavaScript

// See https://meta.wikimedia.org/wiki/Schema:RelatedArticles
( function ( $ ) {
var $readMore,
schemaRelatedPages,
skin = mw.config.get( 'skin' ),
$window = $( window );
/**
* 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' } );
}
}
mw.trackSubscribe( 'ext.relatedArticles.logReady', function ( _, data ) {
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() +
( new Date() ).getTime().toString()
}
);
$readMore = data.$readMore;
// log ready
schemaRelatedPages.log( { eventName: 'ready' } );
// 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();
schemaRelatedPages.log( {
eventName: 'clicked',
clickIndex: index + 1
} );
} );
} );
}( jQuery, mediaWiki ) );