createPlugins.md
May 27, 2020 ยท View on GitHub
Creating a plugin
A valid plugin must contain:
name- the plugin nameeventHandlers- handlers functions for the events, typically in the eventHandler function we would send out a tracking beacon, nowreact-i13nhasclick,createdandenterViewport, you can define the events you need and implement the handlers function, e.g.,pageview.
All of the eventHandlers will receive a payload object and a callback function. By default, in payload you will get:
payload.I13nNode- the I13n node related to the event, then you could use the APIs provided by I13nNode to get the information you need. It's typically used for default events e.g.,click,created,enterViewport. For custom events, if you don't passI13nNode, the default will be the root I13nNode.payload.env-serverorclient, some events e.g.,pageviewwill fire on both server and client side, you can define the prefer way you want to handle the beacon.
import { ReactI13n } from 'react-i13n';
// define the plugin
var fooPlugin = {
name: 'foo',
eventHandlers: {
click: function(payload, callback) {
// click handlers
},
pageview: function(payload, callback) {
if ('client' === payload.env) {
// client side pageview handlers
} else {
// server side pageview handlers
}
},
created: function(payload, callback) {
// created handlers
}
}
};
Then we can plug the plugin with setupI13n.