mediawiki-extensions-Relate.../resources/ext.relatedArticles.readMore.eventLogging/index.js
Sam Smith 153573dbe5 i13n: Don't sample by pageview
Currently, the mw.eventLog.Schema class samples per pageview. However,
we expect that if a user is bucketed for a session, then all
EventLogging events logged during that session are in the sample.

Unlike Popups in I4f653bba, the RelatedArticles instrumentation does
rely on another mw.eventLog.Schema behavior: distinguishing between
static and dynamic data. This and the use of mw.eventLog.Schema could be
removed in a follow-on change.

Bug: T167236
Change-Id: I9583cb98264efd7dc46b45bbce5323036a100536
2017-06-19 12:20:42 +01:00

110 lines
2.6 KiB
JavaScript

// See https://meta.wikimedia.org/wiki/Schema:RelatedArticles
( function ( $, mw ) {
var $readMore,
schemaRelatedPages,
skin = mw.config.get( 'skin' ),
$window = $( window );
/**
* 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 );
}
/**
* 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}
*/
function isEnabledForCurrentUser() {
// TODO: Rename this and other instances to bucketingRate.
var samplingRate = mw.config.get( 'wgRelatedArticlesLoggingSamplingRate', 0 );
if ( !supportsBeacon() ) {
return false;
}
return mw.experiments.getBucket( {
name: 'ext.relatedArticles.instrumentation',
enabled: true,
buckets: {
control: 1 - samplingRate,
A: samplingRate
}
}, mw.user.sessionId() ) === 'A';
}
if ( !isEnabledForCurrentUser() ) {
return;
}
// ---
// BEGIN INSTRUMENTATION
// ---
schemaRelatedPages = new mw.eventLog.Schema(
'RelatedArticles',
// The instrumentation is enabled for the user's session. DON'T SAMPLE AT
// THE EVENT LEVEL.
1,
{
pageId: mw.config.get( 'wgArticleId' ),
skin: ( skin === 'minerva' ) ? skin + '-' + mw.config.get( 'wgMFMode' ) : skin,
userSessionToken: mw.user.sessionId()
}
);
/**
* 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.logEnabled', function ( _, data ) {
schemaRelatedPages.log( {
eventName: data.isEnabled ? 'feature-enabled' : 'feature-disabled'
} );
} );
mw.trackSubscribe( 'ext.relatedArticles.logReady', function ( _, data ) {
$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 ) );