Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-metrics
Advanced tools
An analytics module for React.
$ npm install --save react-metrics
React Metrics depends on Promise to be available in browser. If your application support the browser which doesn't support Promise, please include the polyfill.
Refer to the docs for more information on configuration.
// Application.js
import {metrics} from "react-metrics";
const config = {
vendors: [{
name: "Google Analytics",
api: new GoogleAnalytics({
trackingId: "UA-********-*"
})
}],
pageViewEvent: "pageLoad",
pageDefaults: () => {
return {
siteName: "My Web Site",
...
};
}
}
Compose your top level application component with metrics
in order to provide metrics
to all components and automatically enable page view tracking.
// Application.js
class Application extends React.Component {
render() {
return (
{this.props.children}
);
}
}
export default metrics(config)(Application);
Alternatively, if your development environment supports ES7, use the @decorator syntax instead:
// Application.js
@metrics(config)
class Application extends React.Component {
render() {
return (
{this.props.children}
);
}
}
Your application will now automatically trigger page view tracking.
Use data-
attributes to enable custom link tracking on your DOM elements.
// PaginationComponent.js
class PaginationComponent extends React.Component {
render() {
const {commentId, totalPage, currentPage} = this.props;
return (
<ul>
<li className={currentPage > 0 ? "active" : ""}>
<a
href="#"
data-metrics-event-name="commentPageClick"
data-metrics-comment-id={commentId}
data-metrics-page-num={currentPage - 1}>
Back
</a>
</li>
<li>...</li>
<li className={currentPage < totalPage - 1 ? "active" : ""}>
<a
href="#"
data-metrics-event-name="commentPageClick"
data-metrics-comment-id={commentId}
data-metrics-page-num={currentPage + 1}>
Next
</a>
</li>
</ul>
);
}
}
react-metrics
does not automatically supply any vendor analytics. You need to integrate with an analytics vendor to actually track something for reporting.
Refer to Vendor Examples for Omniture, Google Analytics and other implementations.
Also check out the awesome segmentio library which provides a lot of third party analytics vendors.
Use the @exposeMetrics
decorator and willTrackPageView()
methods on a route-handling component to override the default page view tracking behavior and pageDefaults
data.
// PageComponent.js
// Must be a "route handling" component: <Route path="my-page" component={PageComponent}/>
import {exposeMetrics, PropTypes} from "react-metrics";
@exposeMetrics
class PageComponent extends React.Component {
static contextTypes = {
metrics: PropTypes.metrics
}
static willTrackPageView() {
// first, suppress the automatic call.
return false;
}
componentDidMount() {
const {value1, value2} = this.props;
this.context.metrics.pageView({value1, value2});
}
render () {
...
}
}
// PageComponent.js "route-handler
// A route handling component: <Route path="my-page" component={PageComponent}/>
import {exposeMetrics} from "react-metrics";
@exposeMetrics
class PageComponent extends React.Component {
static willTrackPageView(routeState) {
// return a promise that resolves to custom data.
return yourPromise.then(data => {
// data gets merged with `pageDefaults` object
return data;
});
}
render () {
...
}
}
Use this.context.metrics.track()
to trigger custom event tracking as an alternative to declarative custom link tracking.
Define metrics
as a contextType
in your component and trigger custom track events using metrics.track()
.
import {PropTypes} from "react-metrics";
class YourComponent extends React.Component {
static contextTypes = {
metrics: PropTypes.metrics
}
onSomethingUpdated(value) {
this.context.metrics.track("customEventName", {value});
}
render() {
...
}
}
react-metrics
provides a low level factory API; this is convenient for exposing an instance of the metrics
API outside of a React component.
Use createMetrics
to create a metrics
instance and expose the metrics.api
.
// creating middleware for Redux
import {createMetrics} from "react-metrics";
const metrics = createMetrics(config);
export default function metricsMiddleware() {
return next => action => {
const returnValue = next(action);
switch (action.type) {
case ActionTypes.ROUTE_CHANGE:
const {location} = action;
const paths = location.pathname.substr(1).split("/");
const routeState = location;
metrics.setRouteState(routeState);
metrics.api.pageView({
category: !paths[0] ? "landing" : paths[0]
});
}
return returnValue;
};
}
metrics
APInpm install
npm run examples
Please take a moment to review the guidelines for contributing.
MIT
FAQs
An analytics library for React.js
The npm package react-metrics receives a total of 375 weekly downloads. As such, react-metrics popularity was classified as not popular.
We found that react-metrics demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.