concept analytic in category isomorphic web applications
appears as: nalytics

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.
Every web app I’ve ever built has a dependency on the window or document object. Sometimes it’s to support a social widget, and other times it’s a specific library I want to use (for example, analytics or bug tracking). But this poses a problem in an isomorphic app: how do you continue to use this kind of code without breaking your app on the server?
Listing C.1. Add analytics to onRouteChange—components/onRouteChange.jsx
import { sendData } from '../analytics.es6'; #1 const onRouteChange = (WrappedComponent) => { return class extends React.PureComponent { trackPageView(location) { #2 this.sendAnalytics(location); #2 console.log('Tracked a pageview'); }; fetchData(nextProps) {} sendAnalytics(location) { #3 sendData({ #3 location: location && location.pathname, type: 'navigation' }); } componentDidMount() { this.trackPageView(this.props.location); #4 } componentWillReceiveProps(nextProps) { //...other code if (navigated) { this.trackPageView(nextProps.location); #4 } } } } #1 Imports sendData from the analytics module #2 Passes location into the function. Calls sendAnalytics. #3 Creates a sendAnalytics function that calls sendData. The location will be passed in. #4 Calls trackPageView each time the route changes. Make sure to pass in the correct location. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":57}],[{\"line\":4,\"ch\":18},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":25},{\"line\":5,\"ch\":33}],[{\"line\":11,\"ch\":18},{\"line\":11,\"ch\":26}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":16}],[{\"line\":19,\"ch\":36},{\"line\":19,\"ch\":44}],[{\"line\":26,\"ch\":37},{\"line\":26,\"ch\":45}],[{\"line\":33,\"ch\":10},{\"line\":33,\"ch\":18}],[{\"line\":34,\"ch\":61},{\"line\":34,\"ch\":69}],[{\"line\":35,\"ch\":85},{\"line\":35,\"ch\":93}],[{\"line\":4,\"ch\":55},{\"line\":4,\"ch\":57}],[{\"line\":5,\"ch\":55},{\"line\":5,\"ch\":57}],[{\"line\":33,\"ch\":0},{\"line\":33,\"ch\":2}],[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":57}],[{\"line\":11,\"ch\":4},{\"line\":11,\"ch\":57}],[{\"line\":12,\"ch\":6},{\"line\":12,\"ch\":57}],[{\"line\":13,\"ch\":8},{\"line\":13,\"ch\":48}],[{\"line\":14,\"ch\":8},{\"line\":14,\"ch\":26}],[{\"line\":15,\"ch\":6},{\"line\":15,\"ch\":9}],[{\"line\":0,\"ch\":18},{\"line\":0,\"ch\":19}],[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":5}],[{\"line\":9,\"ch\":26},{\"line\":9,\"ch\":27}],[{\"line\":15,\"ch\":6},{\"line\":15,\"ch\":7}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":5}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":5}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":7}],[{\"line\":28,\"ch\":4},{\"line\":28,\"ch\":5}],[{\"line\":29,\"ch\":2},{\"line\":29,\"ch\":3}],[{\"line\":30,\"ch\":0},{\"line\":30,\"ch\":1}],[{\"line\":19,\"ch\":25},{\"line\":19,\"ch\":44}],[{\"line\":19,\"ch\":55},{\"line\":19,\"ch\":57}],[{\"line\":26,\"ch\":55},{\"line\":26,\"ch\":57}],[{\"line\":35,\"ch\":0},{\"line\":35,\"ch\":2}],[{\"line\":26,\"ch\":27},{\"line\":26,\"ch\":45}],[{\"line\":19,\"ch\":55},{\"line\":19,\"ch\":57}],[{\"line\":26,\"ch\":55},{\"line\":26,\"ch\":57}],[{\"line\":35,\"ch\":0},{\"line\":35,\"ch\":2}]]"} !@%STYLE%@!