Home

Awesome

react-i13n

npm version Build Status Coverage Status

react-i13n provides a performant, scalable and pluggable approach to instrumenting your React application.

Typically, you have to manually add instrumentation code throughout your application, e.g., hooking up onClick handlers to the links you want to track. react-i13n provides a simplified approach by letting you define the data model you want to track and handling the beaconing for you.

react-i13n does this by building an instrumentation tree that mirrors your applications React component hierarchy. All you have to do is leverage our React component to denote which components should fire the tracking events.

Features

Install

npm install react-i13n --save

Runtime Compatibility

react-i13n is written with ES2015 in mind and should be used along with polyfills for features like Promise and Object.assign in order to support all browsers and older versions of Node.js. We recommend using Babel.

Usage

import React, { Component } from 'react';
import {
  ReactI13n,
  createI13nNode,
  setupI13n
} from 'react-i13n';
import somePlugin from 'some-i13n-plugin'; // a plugin for a certain instrumentation mechanism

// create a i13n anchor for link tracking
const I13nAnchor = createI13nNode('a', {
  isLeafNode: true,
  bindClickEvent: true,
  follow: true
});

class DemoApp extends Component {
  componentDidMount () {
    // fire a custom event
    this.props.i13n.executeEvent('pageview', {});
  }

  render() {
    <span>
      <I13nAnchor
        href="http://foo.bar"
        i13nModel={{
          action: 'click',
          label: 'foo'
        }}
      >
        ...
      </I13nAnchor>
      // this link will be tracked, and the click event handlers provided by the plugin will get the model data as
      // {site: 'foo', action: 'click', label: 'foo'}
    </span>
  }
};


const I13nDempApp = setupI13n(DemoApp, {
  rootModelData: { site: 'foo' },
  isViewportEnabled: true
}, [somePlugin]);

// then you could use I13nDemoApp to render you app

Available Plugins

Or follow our guide and create your own.

I13n Tree

I13n Tree

react-i13n builds the instrumentation tree by leveraging the React context feature. Each component can define a i13nModel prop that defines the data it needs to track. This approach is more performant, as it means you do not need additional DOM manipulation when you want to collect the tracking data values for sending out beacons.

Since the i13n data is defined at each level. Whenever you want to get the i13nModel for a certain node, react-i13n will traverse back up the tree to merge all the i13nModel information in the hierarchy. Since the tree is already built, you do not need extra DOM access, which is cheap and efficient.

Performance

The performance has always been a topic we are working on, and yes it's an overhead to create an additional react component wrapping the link, the performance benchmark as below:

link-without-react-component x 131,232 ops/sec ±1.08% (82 runs sampled)
link-wrapped-with-react-component x 111,056 ops/sec ±1.55% (88 runs sampled)
link-wrapped-with-react-component-with-i13n-high-order-component x 64,422 ops/sec ±1.95% (84 runs sampled)

Presentation

Take a look at Rafael Martins' slides from a recent React meetup to understand more.

Debugging

Add i13n_debug=1 to the request url, you will get the i13n model for each i13n node directly shown on the page. It shows the information for each model data and where the data inherits from.

Examples

Set ENV during CI process

We check process.env.NODE_ENV !== 'production' to determine if we should do some action like print out warning message, that means it's recommended to use tools like envify as part of your build process to strip out non-production code for production build.

With Webpack

Use DefinePlugin to define the value for process.env.

// Example of the webpack configuration:

plugins: [
  new webpack.DefinePlugin({
    'process.env': {
        NODE_ENV: JSON.stringify('production')
    }
  }),
  ...
]

With Browserify

Similar to webpack, you can also use envify to set process.env.NODE_ENV to the desired environment

$ browserify index.js -t [ envify --NODE_ENV production  ] | uglifyjs -c > bundle.js

Testing

Unit

Functional

License

This software is free to use under the Yahoo Inc. BSD license. See the LICENSE file for license text and copyright information.