Jump to content

This is a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org

Test Kitchen/SDK/JavaScript SDK

From Wikitech

This page provides a guide to using the JavaScript Test Kitchen SDK for instrument and experiment creators.

Setup

To set up a local development environment for writing instrument code, follow the setup guide for MediaWiki and Test Kitchen . For more context and tooling recommendations, see the introduction to Test Kitchen development .

Create an instrument

Assuming that you have registered already your instrument in Test Kitchen UI , you can instantiate an instrument in your instrumentation code as follows:

const instrument = mw.testKitchen.getInstrument( 'my-machine-readable-instrument-name' );

Once you've created an instrument, you can submit an event using either submitClick or submitInteraction .

Submit a click event

submitClick provides a simplified method to submit click events that use the Test Kitchen base schemas . To specify a custom value for action or to use a custom schema, use submitInteraction .

Here's an example of an instrument in JavaScript that uses Instrument#submitClick to submit an event when a user clicks on an interwiki link.

function getLinkInteractionData( jqEvent ) {
    const link = jqEvent.target;

    return {
        action_source: link.href,
        action_context: link.title
    };
}

// 'a.extiw' will match anchors that have a extiw class. extiw is used for interwiki links.
$( '#content' ).on(
    'click',
    'a.extiw',
    ( jqEvent ) => instrument.submitClick( getLinkInteractionData( jqEvent ) )
);

The resulting event:

  • by default, includes action: click
  • includes optional interaction data ( action_source and action_context )
  • is validated against the latest Test Kitchen base schema for web , as set in newInstrument
  • is published to the specified event stream (in this case, mediawiki.product_metrics.example ), as set in newInstrument

Submit an interaction event

An interaction event is meant to represent a basic interaction with some target or some event occurring. For example, a user hovers over a UI element or an app notifies the server of its current state.

Here's an example of an instrument in JavaScript that uses Instrument#submitInteraction to submit an event when a user hovers over an interwiki link.

$( '#content' ).on(
    'mouseover',
    'a.extiw',
    ( jqEvent ) => instrument.submitInteraction( 'hover', getLinkInteractionData( jqEvent ) )
);

The resulting event:

  • includes the specified value of action
  • includes optional interaction data you have provided in getLinkInteractionData ( action_source and action_context )
  • is validated against the specified schema (in this case, the Test Kitchen base schema for web ), as set in newInstrument
  • is published to the specified event stream (in this case, mediawiki.product_metrics.example ), as set in newInstrument

Implementation

In the context of MediaWiki frontend development, the JavaScript SDK is provided by the EventLogging extension . It is delivered as part of the ext.eventLogging ResourceLoader module and its methods are exposed via mw.eventLog .

The JavaScript SDK and stream configurations are both delivered as part of the module and the stream configurations are not fetched again until the user navigates to another page. This minimizes the number of HTTP requests per pageview.

The EventLogging extension maintains a list of streams to be included in the module, $wgEventLoggingStreamNames , which can be used to minimize the size of the module. When $wgEventLoggingStreamNames is falsy the JavaScript SDK will not validate whether the destination stream is configured before submitting the event to the destination event service.

Reference

For parameter descriptions and validation rules, see the web schema definition .

Instruments

mw.testKitchen.getInstrument

Create a new Instrument according to the configuration defined in Test Kitchen UI

Parameters:

  • instrumentName (string): Name of the instrument that has been registered in Test Kitchen UI

Instrument#isEnabled

Check if the instrument's stream is in sample

Instrument#setInstrumentName

Set the instrument name (it will be added to the event automatically as an interaction data field)

Parameters :

  • instrumentName (string) : The name of the instrument

Instrument#submitClick

Submit a click event to an event stream for an instrument. The event data must validate against the base schema for web .

Parameters :

Instrument#submitInteraction

Submit an interaction event to an event stream for an instrument. An interaction event represents a basic interaction with some target or an event occurring, such as the performer clicking a UI element or an app notifying the server of its current state.

Parameters :

Instrument#setSchemaID

Sets the ID of the schema used to validate analytics events sent with

Parameters:

  • schemaID (string): schemaID

Experiments

mw.testKitchen.getExperiment

Get an instance of an existing Experiment according to the configuration defined in Test Kitchen UI .

Note that, by default, product_metrics.web_base will be its stream and analytics/product_metrics/web/base/2.0.0 the schemaID. You can set a different ones by calling Experiment#setStream(streamName) and Experiment#setSchema(schemaID) respectively

Parameters:

  • experimentName (string): Name of the experiment

Experiment#getAssignedGroup

Gets the group assigned to the current user

Experiment#isAssignedGroup

Gets whether the group assigned to the user is one of the given groups

Parameters:

  • groups (...string): groups

Experiment#send

Sends an event to the stream that is configured within the current experiment

Parameters:

  • action (string): The action that the user enrolled in this experiment took
  • (optional) interactionData (key/value pairs): Additional data about the action that the user enrolled in the experiment took

Experiment#setStream

Sets the stream to send analytics events to with

Parameters:

  • streamName (string): The stream name

Experiment#setSchema

Sets the ID of the schema used to validate analytics events sent with

Parameters:

  • schemaID (string): schemaID