You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+27-20
Original file line number
Diff line number
Diff line change
@@ -9,19 +9,37 @@
9
9
## Installation
10
10
11
11
```
12
-
npm install --save nytm/nyt-react-tracking#v2.2.1
12
+
npm install --save nytm/nyt-react-tracking#v3.0.0
13
13
```
14
14
15
15
(Or whatever is the [latest version](https://github.com/nytm/nyt-react-tracking/releases))
16
16
17
17
## Usage
18
18
`@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)
20
20
-`options` is an optional object that accepts three properties:
21
21
-`dispatch`, which is a function to use instead of the default CustomEvent dispatch behavior. See the section on custom `dispatch()` later in this document.
22
22
-`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.
23
23
-`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.
24
24
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
25
43
`nyt-react-tracking` is best used as a `@decorator()` using the [babel decorators plugin](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy).
26
44
27
45
The decorator can be used on React Classes and on methods within those classes.
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:
54
72
55
73
```js
56
74
importtrackfrom'@nyt/nyt-react-tracking';
57
75
58
76
constFooPage= (props) => {
59
77
return (
60
78
<div onClick={() => {
61
-
props.trackEvent({ action:'click' });
79
+
props.tracking.trackEvent({ action:'click' });
62
80
63
81
// ... other stuff
64
82
}}
@@ -213,34 +231,25 @@ NOTE: That the above code utilizes some of the newer ES6 syntax. This is what it
213
231
// ...
214
232
```
215
233
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.
0 commit comments