GithubHelp home page GithubHelp logo

alexxnica / react-i13n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yahoo/react-i13n

0.0 0.0 0.0 248 KB

A performant, scalable and pluggable approach to instrumenting your React application.

License: Other

JavaScript 97.99% HTML 1.70% Shell 0.30%

react-i13n's Introduction

react-i13n

npm version Build Status Coverage Status Dependency Status devDependency 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 or mixin to denote which components should fire the tracking events.

Features

  • i13n tree - Automated instrumentation tree creation that mirrors your applications React component hierarchy.
  • React integration - Provides a createI13nNode component that easily integrate with your application.
  • Pluggable - A pluggable interface lets you integrate any data analytics library (i.e. Google Analytics, Segment, etc). Take a look at the available plugins.
  • Performant - Tracking data (i13nModel) can be a plain JS object or custom function. This means you can dynamically change tracking data without causing unnecessary re-renders.
  • Adaptable - If you are using an isomorphic framework (e.g. Fluxible) to build your app, you can easily change the tracking implementation on the server and client side. For example, to track page views, you can fire an http request on server and xhr request on the client.
  • Optimizable - We provide an option to enable viewport (integrating subscribe-ui-event) checking for each I13nNode. Which means that data will only be beaconed when the node is in the viewport. This reduces the network usage for the user and provides better tracking details.
  • Auto Scan Links - Support auto scan links for the cases you are not able to replace the component you are using to get it tracked, e.g., if you have dependencies or you are using dangerouslySetInnerHTML. We scan the tags you define on client side, track them and build nodes for them in i13n tree.

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

var React = require('react');
var ReactI13n = require('react-i13n').ReactI13n;
var setupI13n = require('react-i13n').setupI13n;
var somePlugin = require('some-i13n-plugin'); // a plugin for a certain instrumentation mechanism

// create a i13n anchor for link tracking
// or you can use the mixin to track an existing component
var createI13nNode = require('react-i13n').createI13nNode;
var I13nAnchor = createI13nNode('a', {
    isLeafNode: true,
    bindClickEvent: true,
    follow: true
});

var DemoApp = React.createClass({
    componentWillMount: function () {
        this.props.i13n.executeEvent('pageview', {}); // fire a custom event
    },
    render: function () {
        ...
        <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'}
    }
});

var 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 undocumented React context feature and the componentWillMount life cycle event. 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-mixin x 54,357 ops/sec ±1.01% (79 runs sampled)
link-wrapped-with-react-component-with-i13n-high-order-component x 64,422 ops/sec ±1.95% (84 runs sampled)

We recommend to use createI13nNode instead of I13nMixin as it performs better. As the benchmark result, on server side, rendering 64 react components with i13n functionalities takes 1 ms. Let's say it takes 3 ms overhead if you have 200 links on the page. That's a trade off if you want to organize i13n implementation better with react-i13n. We are working on performance improvement, if you have any insight or performance benchmark, please let us know!

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 massage, 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

  • grunt unit to run unit tests
  • grunt cover to generate the istanbul coverage report

Functional

  • debug locally:
    • grunt functional-debug
    • check functional testing result on http://127.0.0.1:9999/tests/functional/page.html
  • run functional test on saucelabs:
    • make sure you have a saucelab account setup, get the user id ane the access key
    • setup sauce-connect
    • SAUCE_USERNAME={id} SAUCE_ACCESS_KEY={accessKey} grunt functional

License

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

react-i13n's People

Contributors

adlenafane avatar ebertb avatar ericf avatar greenkeeperio-bot avatar hankhsiao avatar kaesonho avatar kfay avatar longlho avatar markreeder avatar mattho avatar maximilianschmitt avatar moimikey avatar mridgway avatar redonkulus avatar roderickhsiao avatar sebastiendavid avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.