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
+25-6
Original file line number
Diff line number
Diff line change
@@ -9,17 +9,17 @@
9
9
## Installation
10
10
11
11
```
12
-
npm install --save nytm/nyt-react-tracking#v2.0.0
12
+
npm install --save nytm/nyt-react-tracking#v2.1.0
13
13
```
14
14
15
-
(Or whatever is [latest](https://github.com/nytm/nyt-react-tracking/releases), it was 2.0.0 as of this writing)
15
+
(Or whatever is the [latest version](https://github.com/nytm/nyt-react-tracking/releases))
16
16
17
17
## Usage
18
-
@track() expects two arguments, `trackingData` and `options`.
18
+
`@track()` expects two arguments, `trackingData` and `options`.
19
19
-`trackingData` represents the data to be tracked
20
20
-`options` is an optional object that accepts two 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
-
-`dispatchOnMount`, when set to `true`, dispatches the tracking data when the component mounts to the DOM.
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
24
24
`nyt-react-tracking` is best used as a `@decorator()` using the [babel decorators plugin](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy).
25
25
@@ -94,9 +94,11 @@ NOTE: It is recommended to do this on some top-level component so that you only
94
94
95
95
### When to use `options.dispatchOnMount`
96
96
97
-
To dispatch tracking data when a component mounts, you can pass in `{ dispatchOnMount: true }` as the second parameter to `@track()`. This is useful for dispatching tracking data on "Page" components, for example.
97
+
You can pass in a second parameter to `@track`, `options.dispatchOnMount`. There are two valid types for this, as a boolean or as a function. The use of the two is explained in the next sections:
98
+
99
+
#### Using `options.dispatchOnMount` as a boolean
98
100
99
-
For example:
101
+
To dispatch tracking data when a component mounts, you can pass in `{ dispatchOnMount: true }` as the second parameter to `@track()`. This is useful for dispatching tracking data on "Page" components, for example.
@@ -115,6 +117,23 @@ Of course, you could have achieved this same behavior by just decorating the `co
115
117
116
118
_Note: this is only in affect when decorating a Class or stateless functional component. It is not necessary when decorating class methods since any invocations of those methods will immediately dispatch the tracking data, as expected._
117
119
120
+
#### Using `options.dispatchOnMount` as a function
121
+
122
+
If you pass in a function, the function will be called with all of the tracking data from the app's context when the component mounts. The return value of this function will be dispatched in `componentDidMount()`. You do not have to use the app's context data, so you can explicitly define the entire shape of the tracking data to dispatch for a "page view" event, for example.
Will dispatch the following data (notice, the `contextData` was ignored in the function we defined):
130
+
131
+
```
132
+
{
133
+
event: 'pageDataReady'
134
+
}
135
+
```
136
+
118
137
### Advanced Usage
119
138
120
139
You can also pass a function as an argument instead of an object literal, which allows for some advanced usage scenarios such as when your tracking data is a function of some runtime values, like so:
0 commit comments