Skip to content

Commit 7b3ad0d

Browse files
authored
Merge pull request #33 from nytm/for-3-0-0
For v3.0.0
2 parents 1bbb852 + 0e2f1d7 commit 7b3ad0d

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

README.md

+27-20
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,37 @@
99
## Installation
1010

1111
```
12-
npm install --save nytm/nyt-react-tracking#v2.2.1
12+
npm install --save nytm/nyt-react-tracking#v3.0.0
1313
```
1414

1515
(Or whatever is the [latest version](https://github.com/nytm/nyt-react-tracking/releases))
1616

1717
## Usage
1818
`@track()` expects two arguments, `trackingData` and `options`.
19-
- `trackingData` represents the data to be tracked
19+
- `trackingData` represents the data to be tracked (or a function returning that data)
2020
- `options` is an optional object that accepts three properties:
2121
- `dispatch`, which is a function to use instead of the default CustomEvent dispatch behavior. See the section on custom `dispatch()` later in this document.
2222
- `dispatchOnMount`, when set to `true`, dispatches the tracking data when the component mounts to the DOM. When provided as a function will be called on componentDidMount with all of the tracking context data as the only argument.
2323
- `process`, which is a function that can be defined once on some top-level component, used for selectively dispatching tracking events based on each component's tracking data. See more details later in this document.
2424

25+
#### Tracking `props`
26+
27+
The `@track()` decorator will expose a `tracking` prop on the component it wraps, that looks like:
28+
29+
```js
30+
{
31+
// tracking prop provided by @track()
32+
tracking: PropTypes.shape({
33+
// function to call to dispatch tracking events
34+
trackEvent: PropTypes.func,
35+
36+
// function to call to grab contextual tracking data
37+
getTrackingData: PropTypes.func,
38+
})
39+
}
40+
```
41+
42+
### Usage as a Decorator
2543
`nyt-react-tracking` is best used as a `@decorator()` using the [babel decorators plugin](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy).
2644

2745
The decorator can be used on React Classes and on methods within those classes.
@@ -50,15 +68,15 @@ export default class FooPage extends React.Component {
5068

5169
### Usage on Stateless Functional Components
5270

53-
You can also track events by importing `track()` and wrapping your stateless functional component, which will provide `props.trackEvent()` that you can call in your component like so:
71+
You can also track events by importing `track()` and wrapping your stateless functional component, which will provide `props.tracking.trackEvent()` that you can call in your component like so:
5472

5573
```js
5674
import track from '@nyt/nyt-react-tracking';
5775

5876
const FooPage = (props) => {
5977
return (
6078
<div onClick={() => {
61-
props.trackEvent({ action: 'click' });
79+
props.tracking.trackEvent({ action: 'click' });
6280

6381
// ... other stuff
6482
}}
@@ -213,34 +231,25 @@ NOTE: That the above code utilizes some of the newer ES6 syntax. This is what it
213231
// ...
214232
```
215233
216-
### Using Data at Run Time
217-
Any data that is passed to the decorator can be accessed in the decorated component via its props. The component that is decorated will be returned with a prop called `tracking`. The prop `tracking` is an object that has a `getTrackingData` method attached to it. This method returns the results of the object or function that was passed into the decorator.
234+
#### Example `props.tracking.getTrackingData()` usage
235+
236+
Any data that is passed to the decorator can be accessed in the decorated component via its props. The component that is decorated will be returned with a prop called `tracking`. The `tracking` prop is an object that has a `getTrackingData()` method on it. This method returns all of the contextual tracking data up until this point in the component hierarchy.
218237
219238
```js
220239
import React from 'react';
221240
import track from '@nyt/nyt-react-tracking';
222241

223242
// Pass a function to the decorator
224-
@track((props) => {
243+
@track(() => {
225244
const randomId = Math.floor(Math.random() * 100);
226245

227246
return {
228247
page_view_id: randomId
229248
}
230249
})
231250
export default class AdComponent extends React.Component {
232-
// The tracking object with getTrackingData is returned from the decorator and applied to this component via props
233-
constructor(props) {
234-
super(props);
235-
}
236-
237-
componentDidMount() {
238-
// access get tracking data anywhere in the component
239-
this.trackingData = this.props.getTrackingData();
240-
}
241-
242251
render() {
243-
const { page_view_id } = this.trackingData;
252+
const { page_view_id } = this.props.tracking.getTrackingData();
244253

245254
return (
246255
<Ad pageViewId={page_view_id} />
@@ -250,8 +259,6 @@ export default class AdComponent extends React.Component {
250259
}
251260
```
252261
253-
####Example
254-
255262
### Tracking Data
256263
257264
Note that there are no restrictions on the objects that are passed in to the decorator.

build/withTrackingComponentDecorator.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ function withTrackingComponentDecorator() {
6363
dispatch = _ref$dispatch === undefined ? _dispatchTrackingEvent2.default : _ref$dispatch,
6464
_ref$dispatchOnMount = _ref.dispatchOnMount,
6565
dispatchOnMount = _ref$dispatchOnMount === undefined ? false : _ref$dispatchOnMount,
66-
process = _ref.process,
67-
_ref$trackingDataProp = _ref.trackingDataProp,
68-
trackingDataProp = _ref$trackingDataProp === undefined ? true : _ref$trackingDataProp;
66+
process = _ref.process;
6967

7068
return function (DecoratedComponent) {
7169
var _class, _temp;
@@ -86,10 +84,14 @@ function withTrackingComponentDecorator() {
8684
(0, _lodash2.default)({}, _this.trackingData, data));
8785
};
8886

89-
_this.getTrackingData = function () {
90-
return (0, _extends3.default)({}, _this.trackingData);
87+
_this.tracking = {
88+
trackEvent: _this.trackEvent,
89+
getTrackingData: function getTrackingData() {
90+
return _this.trackingData;
91+
}
9192
};
9293

94+
9395
if (context.tracking && context.tracking.process && process) {
9496
console.error('[nyt-react-tracking] options.process should be used once on top level component');
9597
}
@@ -110,7 +112,7 @@ function withTrackingComponentDecorator() {
110112
value: function getChildContext() {
111113
return {
112114
tracking: {
113-
data: this.trackingData,
115+
data: (0, _lodash2.default)({}, this.contextTrackingData, this.ownTrackingData),
114116
dispatch: this.getTrackingDispatcher(),
115117
process: this.context.tracking && this.context.tracking.process || process
116118
}
@@ -138,10 +140,7 @@ function withTrackingComponentDecorator() {
138140
key: 'render',
139141
value: function render() {
140142
return _react2.default.createElement(DecoratedComponent, (0, _extends3.default)({}, this.props, {
141-
tracking: {
142-
trackEvent: this.trackEvent,
143-
getTrackingData: trackingDataProp ? this.getTrackingData : null
144-
}
143+
tracking: this.tracking
145144
}));
146145
}
147146
}]);

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.2.2",
3+
"version": "3.0.0",
44
"description": "Declarative tracking for React apps.",
55
"author": "The New York Times",
66
"contributors": [

0 commit comments

Comments
 (0)