How a ShrimpTest custom metric type works

The Add New Experiment screen’s saving has been implemented, including various update_* functions in the ShrimpTest core class. I also implemented the save code for the “conversion” metric type.

The custom metric type API works as follows (if you’d like to see the magic happen, feel free to follow along): first, the “plugin” calls the ShrimpTest object’s register_metric_type function to register the metric type “conversion” with the name “Conversion”. Then when we load the “Add New Experiment” screen, the shrimptest_add_metric_extra action is triggered so we can print some additional fields. In the case of conversion, this is just a single URL field. The shrimptest_admin_header action is used to print the associated styling and JS.

Finally, when the form is submitted, the appropriate shrimptest_save_metric_* filter (in this case shrimptest_save_metric_conversion) will be called. It receives an array with the information that it requested through its form fields: here, simply, $metric_data = array( 'conversion_url' => ... ). The custom metric then has to ensure that the metric data includes some basic boilerplate parameters, like whether larger numbers are better or not (for conversions, yes), the name of the metric, etc. The conversion metric type also figures out what query_vars the given URL translates to, and also stores that data.

Finally, the custom metric type must call shrimptest_update_metric function at the appropriate time to record the metric. In the case of the conversion metric type, it will keep an eye out and, if a visitor lands on the specified target page, will ping this function and record the successful conversion.

I’ll put together some more detailed API documentation in the near future.