Skip to content

Commit f7f27fe

Browse files
committed
2.2.0
1 parent 419697a commit f7f27fe

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
## Installation
1010

1111
```
12-
npm install --save nytm/nyt-react-tracking#v2.1.1
12+
npm install --save nytm/nyt-react-tracking#v2.2.0
1313
```
1414

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

144144
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.
145-
\n\n
145+
146146
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`:
147147

148148
```js
@@ -158,7 +158,8 @@ class Page1 extends Component {...}
158158
class Page2 extends Component {...}
159159
```
160160
161-
When `Page1` mounts, event with data `{page: 'Page1', event: 'pageview'}` will be dispatched. When `Page2` will be mounted nothing will be dispatched.
161+
When `Page1` mounts, event with data `{page: 'Page1', event: 'pageview'}` will be dispatched.
162+
When `Page2` mounts, nothing will be dispatched.
162163
163164
### Advanced Usage
164165

build/withTrackingComponentDecorator.js

+34-22
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
4747

4848
var TrackingPropType = exports.TrackingPropType = _react.PropTypes.shape({
4949
data: _react.PropTypes.object,
50-
dispatch: _react.PropTypes.func
50+
dispatch: _react.PropTypes.func,
51+
process: _react.PropTypes.func
5152
});
5253

5354
function withTrackingComponentDecorator() {
@@ -57,34 +58,36 @@ function withTrackingComponentDecorator() {
5758
_ref$dispatch = _ref.dispatch,
5859
dispatch = _ref$dispatch === undefined ? _dispatchTrackingEvent2.default : _ref$dispatch,
5960
_ref$dispatchOnMount = _ref.dispatchOnMount,
60-
dispatchOnMount = _ref$dispatchOnMount === undefined ? false : _ref$dispatchOnMount;
61+
dispatchOnMount = _ref$dispatchOnMount === undefined ? false : _ref$dispatchOnMount,
62+
process = _ref.process;
6163

6264
return function (DecoratedComponent) {
63-
var _class, _temp2;
65+
var _class, _temp;
6466

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

67-
return _temp2 = _class = function (_Component) {
69+
return _temp = _class = function (_Component) {
6870
(0, _inherits3.default)(WithTracking, _Component);
6971

70-
function WithTracking() {
71-
var _ref2;
72-
73-
var _temp, _this, _ret;
74-
72+
function WithTracking(props, context) {
7573
(0, _classCallCheck3.default)(this, WithTracking);
7674

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

81-
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) {
77+
_this.getTrackingData = function (data) {
8278
return (0, _lodash2.default)({}, _this.getChildContext().tracking.data, data);
83-
}, _this.trackEvent = function (data) {
79+
};
80+
81+
_this.trackEvent = function (data) {
8482
_this.getTrackingDispatcher()(
8583
// deep-merge tracking data from context and tracking data passed in here
8684
_this.getTrackingData(data));
87-
}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
85+
};
86+
87+
if (context.tracking && context.tracking.process && process) {
88+
console.error('[nyt-react-tracking] options.process should be used once on top level component');
89+
}
90+
return _this;
8891
}
8992

9093
(0, _createClass3.default)(WithTracking, [{
@@ -102,20 +105,29 @@ function withTrackingComponentDecorator() {
102105
return {
103106
tracking: {
104107
data: (0, _lodash2.default)({}, contextData, thisTrackingData),
105-
dispatch: this.getTrackingDispatcher()
108+
dispatch: this.getTrackingDispatcher(),
109+
process: this.context.tracking && this.context.tracking.process || process
106110
}
107111
};
108112
}
109113
}, {
110114
key: 'componentDidMount',
111115
value: function componentDidMount() {
112-
if (dispatchOnMount === true) {
116+
var contextTrackingData = this.getTrackingData();
117+
var contextProcess = this.context.tracking && this.context.tracking.process;
118+
119+
if (typeof contextProcess === 'function' && typeof dispatchOnMount === 'function') {
120+
this.trackEvent((0, _lodash2.default)({}, contextProcess(contextTrackingData), dispatchOnMount(contextTrackingData)));
121+
} else if (typeof contextProcess === 'function') {
122+
var processed = contextProcess(contextTrackingData);
123+
if (processed) {
124+
this.trackEvent(processed);
125+
}
126+
} else if (typeof dispatchOnMount === 'function') {
127+
this.trackEvent(dispatchOnMount(contextTrackingData));
128+
} else if (dispatchOnMount === true) {
113129
this.trackEvent();
114130
}
115-
116-
if (typeof dispatchOnMount === 'function') {
117-
this.trackEvent(dispatchOnMount(this.getTrackingData()));
118-
}
119131
}
120132
}, {
121133
key: 'render',
@@ -130,6 +142,6 @@ function withTrackingComponentDecorator() {
130142
tracking: TrackingPropType
131143
}, _class.childContextTypes = {
132144
tracking: TrackingPropType
133-
}, _temp2;
145+
}, _temp;
134146
};
135147
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nyt/nyt-react-tracking",
3-
"version": "2.1.1",
3+
"version": "2.2.0",
44
"description": "Declarative tracking for React apps.",
55
"author": "The New York Times",
66
"contributors": [

0 commit comments

Comments
 (0)