mediawiki-skins-MinervaNeue/resources/skins.minerva.scripts/pageIssuesLogger.js
jdlrobson 65f2e5ef51 getPageToken now defined in core
Bug: T201124
Depends-On: Id43e9d3f3e687133c98017d24c10c307a5a855a1
Change-Id: I2b519361dfa974acef918e00ffc1ae14e33e28f5
2018-08-21 20:33:35 +00:00

100 lines
3.6 KiB
JavaScript

( function ( M, mwConfig, mwTrack, mwTrackSubscribe, mwUser ) {
var
util = M.require( 'mobile.startup/util' ),
EVENT_PAGE_ISSUE_LOG = 'minerva.PageIssuesAB';
/**
* Defines default data for Schema:PageIssues that will be recorded with every event.
* @param {boolean} newTreatmentEnabled
* @param {number} namespaceId The namespace for the page that has issues.
* @param {string[]} pageIssueSeverities An array of PageIssue severities.
* @return {Object} A Partial<Schema:PageIssues> Object meant to be mixed with track data.
*/
function newPageIssueSchemaData( newTreatmentEnabled, namespaceId, pageIssueSeverities ) {
return {
pageTitle: mwConfig.get( 'wgTitle' ),
namespaceId: namespaceId,
pageIdSource: mwConfig.get( 'wgArticleId' ),
issuesVersion: bucketToVersion( newTreatmentEnabled ),
issuesSeverity: pageIssueSeverities,
isAnon: mwUser.isAnon(),
editCountBucket: getUserEditBuckets(),
sessionToken: mwUser.sessionId()
};
}
/**
* Enable tracking and add page token to every logged event.
* @param {boolean} newTreatmentEnabled
* @param {Object} pageIssueSchemaData A Partial<Schema:PageIssues> Object that will be mixed with
* with track data.
* @return {void}
*/
function subscribe( newTreatmentEnabled, pageIssueSchemaData ) {
// set the page token on the request.
pageIssueSchemaData.pageToken = mw.user.getPageviewToken();
// intermediary event bus that extends the event data before being passed to event-logging.
mwTrackSubscribe( EVENT_PAGE_ISSUE_LOG, function ( topic, data ) {
var mixedData = util.extend( {}, pageIssueSchemaData, data );
// Log readingDepth schema.(ReadingDepth is guarded against multiple enables).
// See https://gerrit.wikimedia.org/r/#/c/mediawiki/extensions/WikimediaEvents/+/437686/
mwTrack( 'wikimedia.event.ReadingDepthSchema.enable', bucketToGroup( newTreatmentEnabled ) );
// Log PageIssues schema.
mwTrack( 'wikimedia.event.PageIssues', mixedData );
} );
}
/**
* @param {boolean} newTreatmentEnabled
* @return {string} The page issues group associated with the treatment bucket.
*/
function bucketToGroup( newTreatmentEnabled ) {
return newTreatmentEnabled ? 'page-issues-b_sample' : 'page-issues-a_sample';
}
/**
* @param {boolean} newTreatmentEnabled
* @return {string} The page issues version associated with the treatment bucket.
*/
function bucketToVersion( newTreatmentEnabled ) {
return newTreatmentEnabled ? 'new2018' : 'old';
}
/**
* Converts user edit count into a predefined string. Note: these buckets have *nothing* to do
* with A/B bucketing.
* @return {string}
*/
function getUserEditBuckets() {
var editCount = mwConfig.get( 'wgUserEditCount', 0 );
if ( editCount === 0 ) { return '0 edits'; }
if ( editCount < 5 ) { return '1-4 edits'; }
if ( editCount < 100 ) { return '5-99 edits'; }
if ( editCount < 1000 ) { return '100-999 edits'; }
if ( editCount >= 1000 ) { return '1000+ edits'; }
// This is unlikely to ever happen. If so, we'll want to cast to a string
// that is not accepted and allow EventLogging to complain
// about invalid events so we can investigate.
return 'error (' + editCount + ')';
}
/**
* Log data to the PageIssuesAB test schema. It's safe to call this function prior to
* subscription.
* @param {Object} data to log
* @return {void}
*/
function log( data ) {
mwTrack( EVENT_PAGE_ISSUE_LOG, data );
}
M.define( 'skins.minerva.scripts/pageIssuesLogger', {
newPageIssueSchemaData: newPageIssueSchemaData,
subscribe: subscribe,
log: log
} );
}( mw.mobileFrontend, mw.config, mw.track, mw.trackSubscribe, mw.user ) );