react-i13n-ga
Google Analytics plugin for react-i13n
Features
- Integrate react-i13n to provide instrumentation approach using Google Analytics.
- react-i13n handles the beaconing management and handle the click events, this plugin provides event handlers to handle these events and firing
ga beacons
.
Install
npm install react-i13n-ga
Usage
You will need to create a instance of react-i13n-ga
first, then use getPlugin
to get the plugin object, then pass it into setupI13n provided by react-i13n, then it will help to decorate your Top Level Component
with i13n react-i13n-ga
plugin functionalities.
var reactI13nGoogleAnalytics = ;var setupI13n = setupI13n; var reactI13nGoogleAnalytics = your tracking id; // create reactI13nGoogleAnalytics instance with your tracking id// orvar reactI13nGoogleAnalytics = trackingId: mandatory your tracking id cookieDomain: optional cookie domain name by default "auto" name: optional customized tracker name userId: optional; // create reactI13nGoogleAnalytics instance with config object // Suppose that Application is your top level component, use setupI13n with this pluginApplication = ;
Pageview Event
- Integrate page tracking,
var ReactI13n = ReactI13n; // fire pageview at whatever you want, typically we do it at componentDidMountReactI13n; // in component (React 0.14+)thispropsi13n;
Click Event
- Integrate event tracking
- Define the keys in
i13nModel
:tracker
- tracker name, default is undefined.category
- Typically the object that was interacted with, default set asall
.action
- The type of interaction, default set asclick
.label
- Useful for categorizing events, default set as the value of i13nNode.getText.value
- Values must be non-negative. Useful to pass counts (e.g. 4 times).nonInteraction
- Boolean to indicate a non-interaction event.
You can integrate I13nAnchor provided by react-i13n to track the normal links.
var I13nAnchor = I13nAnchor; // in template, will fire event beacon as ga('send', 'event', 'foo', 'click', 'Foo');<I13nAnchor i13nModel=category: 'foo' action: 'click'>Foo</I13nAnchor>
You can also integrate integrate createI13nNode or I13nMixin to get your custom component be tracked
var createI13nNode = createI13nNode;var Foo = React; Foo = ; // in template<Foo i13nModel=category: 'foo' action: 'click' label: 'Foo'> // if Foo is clicked, it will fire event beacon as ga('send', 'event', 'foo', 'click', 'Foo'); ...</Foo>
var I13nMixin = I13nMixin;var Foo = React; // in template<Foo i13nModel=category: 'foo' action: 'click' label: 'Foo'> // if Foo is clicked, it will fire event beacon as ga('send', 'event', 'foo', 'click', 'Foo'); ...</Foo>
For better instrumentation integration, you can leverage the inherit architecture, e.g., create a parent and define the category
with default tracker, or specify tracker
, so that all the links inside will apply it.
var createI13nNode = createI13nNode;var I13nAnchor = createI13nNode;var Foo = React; Foo = ; // in template<Foo i13nModel=tracker: 'myTracker' category: 'foo' />
Tracker Settings
You can also use the following methods to set properties of GA like User Id or dimension.
var userId = 'aa4ebd41203df5b8639e9fe48d4c6c7de7c4b053';var dimension1 = 'administrator';ReactI13n;ReactI13n;ReactI13n;ReactI13n;
ga command queue
You can also execute ga command queue by calling executeEvent. It's also possible to execute command on specific tracker . The following are sample usage:
// send by default trackerthispropsi13n; // send by specific trackerthispropsi13n; // require on default trackerthispropsi13n; // require plugin for specific trackerthispropsi13n; // more and more, such as ga('[myTracker.]set', ...)// ...