Skip to content

Commit

Permalink
2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tizmagik committed Apr 7, 2017
1 parent 419697a commit f7f27fe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Installation

```
npm install --save nytm/nyt-react-tracking#v2.1.1
npm install --save nytm/nyt-react-tracking#v2.2.0
```

(Or whatever is the [latest version](https://github.com/nytm/nyt-react-tracking/releases))
Expand Down Expand Up @@ -142,7 +142,7 @@ Will dispatch the following data (assuming no other tracking data in context fro
### Top level `options.process`

When there's a need to implicitly dispatch an event with some data for *every* component, you can define an `options.process` function. This function should be declared once, at some top-level component. It will get called with each component's tracking data as the only argument. The returned object from this function will be merged with all the tracking context data and dispatched in `componentDidMount()`. If a falsy value is returned (`false`, `null`, `undefined`, ...), nothing will be dispatched.
\n\n

A common use case for this is to dispatch a `pageview` event for every component in the application that has a `page` property on its `trackingData`:

```js
Expand All @@ -158,7 +158,8 @@ class Page1 extends Component {...}
class Page2 extends Component {...}
```
When `Page1` mounts, event with data `{page: 'Page1', event: 'pageview'}` will be dispatched. When `Page2` will be mounted nothing will be dispatched.
When `Page1` mounts, event with data `{page: 'Page1', event: 'pageview'}` will be dispatched.
When `Page2` mounts, nothing will be dispatched.
### Advanced Usage
Expand Down
56 changes: 34 additions & 22 deletions build/withTrackingComponentDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

var TrackingPropType = exports.TrackingPropType = _react.PropTypes.shape({
data: _react.PropTypes.object,
dispatch: _react.PropTypes.func
dispatch: _react.PropTypes.func,
process: _react.PropTypes.func
});

function withTrackingComponentDecorator() {
Expand All @@ -57,34 +58,36 @@ function withTrackingComponentDecorator() {
_ref$dispatch = _ref.dispatch,
dispatch = _ref$dispatch === undefined ? _dispatchTrackingEvent2.default : _ref$dispatch,
_ref$dispatchOnMount = _ref.dispatchOnMount,
dispatchOnMount = _ref$dispatchOnMount === undefined ? false : _ref$dispatchOnMount;
dispatchOnMount = _ref$dispatchOnMount === undefined ? false : _ref$dispatchOnMount,
process = _ref.process;

return function (DecoratedComponent) {
var _class, _temp2;
var _class, _temp;

var decoratedComponentName = DecoratedComponent.displayName || DecoratedComponent.name || 'Component';

return _temp2 = _class = function (_Component) {
return _temp = _class = function (_Component) {
(0, _inherits3.default)(WithTracking, _Component);

function WithTracking() {
var _ref2;

var _temp, _this, _ret;

function WithTracking(props, context) {
(0, _classCallCheck3.default)(this, WithTracking);

for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var _this = (0, _possibleConstructorReturn3.default)(this, (WithTracking.__proto__ || (0, _getPrototypeOf2.default)(WithTracking)).call(this, props, context));

return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref2 = WithTracking.__proto__ || (0, _getPrototypeOf2.default)(WithTracking)).call.apply(_ref2, [this].concat(args))), _this), _this.getTrackingData = function (data) {
_this.getTrackingData = function (data) {
return (0, _lodash2.default)({}, _this.getChildContext().tracking.data, data);
}, _this.trackEvent = function (data) {
};

_this.trackEvent = function (data) {
_this.getTrackingDispatcher()(
// deep-merge tracking data from context and tracking data passed in here
_this.getTrackingData(data));
}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
};

if (context.tracking && context.tracking.process && process) {
console.error('[nyt-react-tracking] options.process should be used once on top level component');
}
return _this;
}

(0, _createClass3.default)(WithTracking, [{
Expand All @@ -102,20 +105,29 @@ function withTrackingComponentDecorator() {
return {
tracking: {
data: (0, _lodash2.default)({}, contextData, thisTrackingData),
dispatch: this.getTrackingDispatcher()
dispatch: this.getTrackingDispatcher(),
process: this.context.tracking && this.context.tracking.process || process
}
};
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
if (dispatchOnMount === true) {
var contextTrackingData = this.getTrackingData();
var contextProcess = this.context.tracking && this.context.tracking.process;

if (typeof contextProcess === 'function' && typeof dispatchOnMount === 'function') {
this.trackEvent((0, _lodash2.default)({}, contextProcess(contextTrackingData), dispatchOnMount(contextTrackingData)));
} else if (typeof contextProcess === 'function') {
var processed = contextProcess(contextTrackingData);
if (processed) {
this.trackEvent(processed);
}
} else if (typeof dispatchOnMount === 'function') {
this.trackEvent(dispatchOnMount(contextTrackingData));
} else if (dispatchOnMount === true) {
this.trackEvent();
}

if (typeof dispatchOnMount === 'function') {
this.trackEvent(dispatchOnMount(this.getTrackingData()));
}
}
}, {
key: 'render',
Expand All @@ -130,6 +142,6 @@ function withTrackingComponentDecorator() {
tracking: TrackingPropType
}, _class.childContextTypes = {
tracking: TrackingPropType
}, _temp2;
}, _temp;
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nyt/nyt-react-tracking",
"version": "2.1.1",
"version": "2.2.0",
"description": "Declarative tracking for React apps.",
"author": "The New York Times",
"contributors": [
Expand Down

0 comments on commit f7f27fe

Please sign in to comment.