Labs/Test Pilot/ExperimentAPI: Difference between revisions

/
(/)
Line 155: Line 155:


== Observer ==
== Observer ==
The Observer of your experiment has the very important responsibility of actually doing the data collection, and passing the collected data along to the store you defined in dataStoreInfo.
Observer must be a constructor function, a.k.a. a "class".  It will be called like this:
new yourExperiment.Observer(window, store);
One Observer is instantiated for each window that is opened in the user's browser.  A reference to that window is passed in as the first argument to the constructor function.
The second argument to the constructor function is a reference to the single data store object.  (No matter how many Observers are instantiated, they all get a reference to the same dataStore instance.)  See the dataStoreInfo section above.  Your Observer can call .storeEvent() on this store object to save events to the database.
The Observer has chrome privileges, so it has free reign to use XPCOM components and so on to do the needed observations.
Besides the constructor, your Observer must provide an uninstall() method, which is called (with no arguments) when the observer is no longer needed.  Any cleanup that you need to do (such as unregistering listeners, etc.) must be done in the uninstall() method.
=== Example ===
Here's the skeleton of an Observer:
exports.Observer = function MyExperimentObserver(window, store) {
  this._init(window, store);
};
exports.Observer.prototype = {
  _init: function MyExperimentObserver__init(window, store) {
    console.info("Initializing a new MyExperimentObserver.");
    this._window = window;
    this._dataStore = store;
    // Install desired listeners, event handlers, etc. into the window here
    // e.g. register this._onClick as an event handler...
  },
  uninstall: function MyExperimentObserver_uninstall() {
    console.info("Uninstalling a MyExperimentObserver.");
    // Unregister this._onClick and any other registered event listeners
    // from this._window
  },
  _onClick: function MyExperimentObserver_onClick(clickEvent) {
    var eventToRecord;
    // Process clickEvent, calculate the properties of the event you want
    // to record, then write it to the data store:
    console.info("Recording a click event.");
    this._dataStore.storeEvent(eventToRecord);
  }
};
1,007

edits