@atlaskit/analytics-next 11.2.0 → 11.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/afm-cc/tsconfig.json +0 -1
- package/afm-products/tsconfig.json +0 -1
- package/dist/cjs/events/UIAnalyticsEvent.js +11 -1
- package/dist/es2019/events/UIAnalyticsEvent.js +13 -1
- package/dist/esm/events/UIAnalyticsEvent.js +11 -1
- package/docs/0-intro.tsx +68 -37
- package/docs/10-concepts.tsx +138 -112
- package/docs/20-usage-with-presentational-components.tsx +177 -198
- package/docs/30-usage-for-container-components.tsx +69 -53
- package/docs/40-listeners.tsx +17 -14
- package/docs/50-error-boundary.tsx +28 -22
- package/docs/60-events.tsx +27 -19
- package/docs/70-advanced-usage.tsx +182 -179
- package/docs/80-upgrade-guide.tsx +182 -100
- package/docs/DocBlocks.tsx +106 -0
- package/package.json +2 -2
- package/afm-jira/tsconfig.json +0 -30
|
@@ -1,85 +1,9 @@
|
|
|
1
|
+
/* eslint-disable @atlaskit/design-system/use-primitives-text, @atlaskit/design-system/use-heading, @atlaskit/design-system/no-html-anchor -- Legacy analytics-next docs intentionally use plain HTML prose, tables, and links instead of ADS docs primitives. */
|
|
1
2
|
import React from 'react';
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const _default_1: any = md`
|
|
7
|
-
This section will guide how to add analytics tracking to presentational and other
|
|
8
|
-
components that don't fit into the "Container" category.
|
|
9
|
-
|
|
10
|
-
For a conceptual overview of \`@atlaskit/analytics-next\`, please consult the
|
|
11
|
-
[concepts page](./concepts).
|
|
12
|
-
|
|
13
|
-
There are several ways to add analytics to a component, via React hooks, or
|
|
14
|
-
via higher order components (HOCs) - consult the Recommended Usage table for
|
|
15
|
-
our suggested approach.
|
|
16
|
-
|
|
17
|
-
## API
|
|
18
|
-
|
|
19
|
-
### Hooks API
|
|
20
|
-
|
|
21
|
-
* [\`useAnalyticsEvents\`](#use-analytics-events)
|
|
22
|
-
* [\`useCallbackWithAnalytics\`](#use-callback-with-analytics)
|
|
23
|
-
* [\`usePlatformLeafEventHandler\` and \`usePlatformLeafSyntheticEventHandler\`](#use-platform-leaf-event-handlers)
|
|
24
|
-
|
|
25
|
-
### HOCs API
|
|
26
|
-
|
|
27
|
-
* [\`withAnalyticsEvents\`](#with-analytics-events)
|
|
28
|
-
|
|
29
|
-
### Recommended Usage
|
|
30
|
-
|
|
31
|
-
${(
|
|
32
|
-
<table>
|
|
33
|
-
<tr>
|
|
34
|
-
<th>Component Type</th>
|
|
35
|
-
<th>Recommendation</th>
|
|
36
|
-
</tr>
|
|
37
|
-
<tr>
|
|
38
|
-
<td>Class components</td>
|
|
39
|
-
<td>
|
|
40
|
-
You have only the <Link href="#with-analytics-events">HOC option</Link>
|
|
41
|
-
</td>
|
|
42
|
-
</tr>
|
|
43
|
-
<tr>
|
|
44
|
-
<td>Atlaskit function components</td>
|
|
45
|
-
<td>
|
|
46
|
-
Use{' '}
|
|
47
|
-
<Link href="#use-platform-leaf-event-handler">
|
|
48
|
-
{/* eslint-disable-next-line @atlaskit/design-system/no-html-code */}
|
|
49
|
-
<code>usePlatformLeafEventHandler</code>
|
|
50
|
-
</Link>
|
|
51
|
-
.
|
|
52
|
-
</td>
|
|
53
|
-
</tr>
|
|
54
|
-
<tr>
|
|
55
|
-
<td>Other function components</td>
|
|
56
|
-
<td>
|
|
57
|
-
Use{' '}
|
|
58
|
-
<Link href="#use-callback-with-analytics">
|
|
59
|
-
{/* eslint-disable-next-line @atlaskit/design-system/no-html-code */}
|
|
60
|
-
<code>useCallbackWithAnalytics</code>
|
|
61
|
-
</Link>{' '}
|
|
62
|
-
if you want something basic.
|
|
63
|
-
<br />
|
|
64
|
-
Use{' '}
|
|
65
|
-
<Link href="#use-analytics-events">
|
|
66
|
-
{/* eslint-disable-next-line @atlaskit/design-system/no-html-code */}
|
|
67
|
-
<code>useAnalyticsEvents</code>
|
|
68
|
-
</Link>{' '}
|
|
69
|
-
if you want more control.
|
|
70
|
-
</td>
|
|
71
|
-
</tr>
|
|
72
|
-
</table>
|
|
73
|
-
)}
|
|
74
|
-
|
|
75
|
-
### Examples
|
|
76
|
-
|
|
77
|
-
<a name="use-analytics-events"></a>
|
|
78
|
-
#### The \`useAnalyticsEvents\` hook
|
|
79
|
-
|
|
80
|
-
This custom React hook provides a method \`createAnalyticsEvent\` for creating \`UIAnalyticsEvent\`s.
|
|
81
|
-
|
|
82
|
-
${code`
|
|
4
|
+
import { CodeBlock } from './DocBlocks';
|
|
5
|
+
|
|
6
|
+
const useAnalyticsEventsCode = `
|
|
83
7
|
import React, { useCallback } from 'react';
|
|
84
8
|
import { useAnalyticsEvents } from '@atlaskit/analytics-next';
|
|
85
9
|
|
|
@@ -94,16 +18,8 @@ const MyButton = ({ onClick = () => {} }) => {
|
|
|
94
18
|
packageVersion: '1.0.0'
|
|
95
19
|
});
|
|
96
20
|
|
|
97
|
-
// do what you like with the event, fire / modify / clone
|
|
98
|
-
// this here is the typical usage pattern for atlaskit components
|
|
99
|
-
|
|
100
21
|
const clonedEvent = analyticsEvent.clone();
|
|
101
|
-
|
|
102
22
|
analyticsEvent.fire("atlaskit");
|
|
103
|
-
|
|
104
|
-
// firing is prevented from happening more than once
|
|
105
|
-
// so that's why we pass a clone to be accessed by the parent component
|
|
106
|
-
// which might also want to fire their own event on this ui interaction
|
|
107
23
|
onClick(clickEvent, clonedEvent);
|
|
108
24
|
}, [ createAnalyticsEvent, onClick ]);
|
|
109
25
|
|
|
@@ -113,17 +29,9 @@ const MyButton = ({ onClick = () => {} }) => {
|
|
|
113
29
|
}
|
|
114
30
|
|
|
115
31
|
export MyButton;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
<a name="use-callback-with-analytics"></a>
|
|
119
|
-
#### The \`useCallbackWithAnalytics\` hook
|
|
120
|
-
|
|
121
|
-
This custom React hook takes a callback function and an event payload, and channel, and
|
|
122
|
-
returns a callback to fire the event and call the provided function.
|
|
123
|
-
|
|
124
|
-
The hooks stores the input and memoizes the return value to optimize performance.
|
|
32
|
+
`;
|
|
125
33
|
|
|
126
|
-
|
|
34
|
+
const useCallbackWithAnalyticsCode = `
|
|
127
35
|
import React, { useCallback } from 'react';
|
|
128
36
|
import { useCallbackWithAnalytics } from '@atlaskit/analytics-next';
|
|
129
37
|
|
|
@@ -135,25 +43,15 @@ const MyButton = ({ onClick = () => {} }) => {
|
|
|
135
43
|
packageVersion: '1.0.0'
|
|
136
44
|
}, 'atlaskit');
|
|
137
45
|
|
|
138
|
-
|
|
46
|
+
return (
|
|
139
47
|
<button onClick={handler}>Track me</button>
|
|
140
48
|
);
|
|
141
49
|
}
|
|
142
50
|
|
|
143
51
|
export MyButton;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
<a name="with-analytics-events"></a>
|
|
147
|
-
#### The \`withAnalyticsEvents\` HOC
|
|
148
|
-
|
|
149
|
-
A HOC which provides the wrapped component with a method for creating \`UIAnalyticsEvent\`s,
|
|
150
|
-
via \`props.createAnalyticsEvent\`.
|
|
151
|
-
|
|
152
|
-
The HOC supports a few ways to use it for convinvience.
|
|
153
|
-
|
|
154
|
-
The first is to use the \`createAnalyticsEvent\` prop the HOC passes to the component manually:
|
|
52
|
+
`;
|
|
155
53
|
|
|
156
|
-
|
|
54
|
+
const hocManualCode = `
|
|
157
55
|
import { withAnalyticsEvents } from '@atlaskit/analytics-next';
|
|
158
56
|
|
|
159
57
|
const MyButton = ({ onClick = () => {}, createAnalyticsEvent }) => {
|
|
@@ -165,16 +63,8 @@ const MyButton = ({ onClick = () => {}, createAnalyticsEvent }) => {
|
|
|
165
63
|
packageVersion: '1.0.0'
|
|
166
64
|
});
|
|
167
65
|
|
|
168
|
-
// do what you like with the event, fire / modify / clone
|
|
169
|
-
// this here is the typical usage pattern for atlaskit components
|
|
170
|
-
|
|
171
66
|
const clonedEvent = analyticsEvent.clone();
|
|
172
|
-
|
|
173
67
|
analyticsEvent.fire("atlaskit");
|
|
174
|
-
|
|
175
|
-
// firing is prevented from happening more than once
|
|
176
|
-
// so that's why we pass a clone to be accessed by the parent component
|
|
177
|
-
// which might also want to fire their own event on this ui interaction
|
|
178
68
|
onClick(clickEvent, clonedEvent);
|
|
179
69
|
};
|
|
180
70
|
|
|
@@ -186,28 +76,15 @@ const MyButton = ({ onClick = () => {}, createAnalyticsEvent }) => {
|
|
|
186
76
|
const AnalyticsWrappedButton = withAnalyticsEvents()(MyButton);
|
|
187
77
|
|
|
188
78
|
export AnalyticsWrappedButton;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
Altenatively, the HOC accepts an optional map as its first argument, which provides a shortcut
|
|
194
|
-
for passing a new analytics event as an additional parameter to the corresponding callback prop.
|
|
79
|
+
`;
|
|
195
80
|
|
|
196
|
-
|
|
81
|
+
const hocMappedCode = `
|
|
197
82
|
import { withAnalyticsEvents } from '@atlaskit/analytics-next';
|
|
198
83
|
|
|
199
84
|
const MyButton = ({ onClick = () => {} }) => {
|
|
200
85
|
const handler = (clickEvent, analyticsEvent) => {
|
|
201
|
-
// do what you like with the event, fire / modify / clone
|
|
202
|
-
// this here is the typical usage pattern for atlaskit components
|
|
203
|
-
|
|
204
86
|
const clonedEvent = analyticsEvent.clone();
|
|
205
|
-
|
|
206
87
|
analyticsEvent.fire("atlaskit");
|
|
207
|
-
|
|
208
|
-
// firing is prevented from happening more than once
|
|
209
|
-
// so that's why we pass a clone to be accessed by the parent component
|
|
210
|
-
// which might also want to fire their own event on this ui interaction
|
|
211
88
|
onClick(clickEvent, clonedEvent);
|
|
212
89
|
};
|
|
213
90
|
|
|
@@ -226,27 +103,15 @@ const AnalyticsWrappedButton = withAnalyticsEvents({
|
|
|
226
103
|
})(MyButton);
|
|
227
104
|
|
|
228
105
|
export AnalyticsWrappedButton;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
Since creating and returning an event is such a common pattern an even more concise shorthand is supported:
|
|
106
|
+
`;
|
|
234
107
|
|
|
235
|
-
|
|
108
|
+
const hocShorthandCode = `
|
|
236
109
|
import { withAnalyticsEvents } from '@atlaskit/analytics-next';
|
|
237
110
|
|
|
238
111
|
const MyButton = ({ onClick = () => {} }) => {
|
|
239
112
|
const handler = (clickEvent, analyticsEvent) => {
|
|
240
|
-
// do what you like with the event, fire / modify / clone
|
|
241
|
-
// this here is the typical usage pattern for atlaskit components
|
|
242
|
-
|
|
243
113
|
const clonedEvent = analyticsEvent.clone();
|
|
244
|
-
|
|
245
114
|
analyticsEvent.fire("atlaskit");
|
|
246
|
-
|
|
247
|
-
// firing is prevented from happening more than once
|
|
248
|
-
// so that's why we pass a clone to be accessed by the parent component
|
|
249
|
-
// which might also want to fire their own event on this ui interaction
|
|
250
115
|
onClick(clickEvent, clonedEvent);
|
|
251
116
|
};
|
|
252
117
|
|
|
@@ -265,32 +130,9 @@ const AnalyticsWrappedButton = withAnalyticsEvents({
|
|
|
265
130
|
})(MyButton);
|
|
266
131
|
|
|
267
132
|
export AnalyticsWrappedButton;
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
<a name="use-platform-leaf-event-handlers"></a>
|
|
271
|
-
#### The \`usePlatformLeafEventHandler\` and \`usePlatformLeafSyntheticEventHandler\` hooks
|
|
272
|
-
|
|
273
|
-
These hooks were built with internal leaf node components purely in mind.
|
|
274
|
-
|
|
275
|
-
They dispatch an event on the \`atlaskit\` channel, then pass it to the
|
|
276
|
-
wrapped function as the last argument in case you want to something additional with the event.
|
|
277
|
-
|
|
278
|
-
\`usePlatformLeafEventHandler\` takes a function with two arguments (\`value\` and
|
|
279
|
-
\`analyticsEvent\`), while \`usePlatformLeafSyntheticEventHandler\` takes a function
|
|
280
|
-
with just one argument for the \`analyticsEvent\`
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
**WARNING:** *These hooks make an assumption that the component being wrapped is a "leaf-node"
|
|
285
|
-
component, i.e., they have no children that require analytics themselves. This
|
|
286
|
-
is so it can include the component name, package name, and package version as context
|
|
287
|
-
in the analytics event. It assumes no children need this context and makes no attempt to
|
|
288
|
-
pass it to them. This gains us a decent performance optimization. Use the other hooks
|
|
289
|
-
if you are unsure you can use these safely.*
|
|
290
|
-
|
|
291
|
-
|
|
133
|
+
`;
|
|
292
134
|
|
|
293
|
-
|
|
135
|
+
const leafHandlersCode = `
|
|
294
136
|
import React, { useCallback } from 'react';
|
|
295
137
|
import {
|
|
296
138
|
usePlatformLeafEventHandler,
|
|
@@ -298,64 +140,201 @@ import {
|
|
|
298
140
|
} from '@atlaskit/analytics-next';
|
|
299
141
|
|
|
300
142
|
const MyButton = ({ onClick = () => {}, onActivate = () => {}}) => {
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Event 1: usePlatformLeafEventHandler
|
|
304
|
-
*/
|
|
305
|
-
// this isn't necessary if you are happy with just firing on the 'atlaskit' channel
|
|
306
143
|
const wrapped = (clickEvent, analyticsEvent) => {
|
|
307
|
-
// do what you like with the event, fire / modify / clone
|
|
308
|
-
// this here is the typical usage pattern for atlaskit components
|
|
309
144
|
const clonedEvent = analyticsEvent.clone();
|
|
310
145
|
analyticsEvent.fire("otherChannel");
|
|
311
|
-
|
|
312
|
-
// firing is prevented from happening more than once
|
|
313
|
-
// so that's why we pass a clone to be accessed by the parent component
|
|
314
|
-
// which might also want to fire their own event on this ui interaction
|
|
315
146
|
onClick(clickEvent, clonedEvent);
|
|
316
147
|
};
|
|
317
148
|
const handler = usePlatformLeafEventHandler({
|
|
318
|
-
fn: wrapped,
|
|
149
|
+
fn: wrapped,
|
|
319
150
|
action: 'clicked',
|
|
320
151
|
componentName: 'my-button',
|
|
321
|
-
actionSubject : 'clickButton',
|
|
152
|
+
actionSubject : 'clickButton',
|
|
322
153
|
packageName: '@atlaskit/my-button',
|
|
323
154
|
packageVersion: '1.0.0',
|
|
324
155
|
analyticsData: {
|
|
325
|
-
// any additional data can live here
|
|
326
156
|
style: 'fancy'
|
|
327
157
|
},
|
|
328
158
|
});
|
|
329
159
|
|
|
330
|
-
|
|
331
|
-
* Event 2: Synthetic event using usePlatformLeafSyntheticEventHandler
|
|
332
|
-
*/
|
|
333
|
-
const wrappedSynthetic = (analyticsEvent) => {
|
|
334
|
-
// A synthetic 'activate' event; same as above, but without a DOM event passed in
|
|
160
|
+
const wrappedSynthetic = (analyticsEvent) => {
|
|
335
161
|
const clonedEvent = analyticsEvent.clone();
|
|
336
162
|
analyticsEvent.fire("otherChannel");
|
|
337
163
|
onActivate(clonedEvent);
|
|
338
164
|
};
|
|
339
165
|
const syntheticHandler = usePlatformLeafSyntheticEventHandler({
|
|
340
|
-
fn: wrappedSynthetic,
|
|
166
|
+
fn: wrappedSynthetic,
|
|
341
167
|
action: 'activated',
|
|
342
168
|
componentName: 'my-button',
|
|
343
169
|
packageName: '@atlaskit/my-button',
|
|
344
170
|
packageVersion: '1.0.0',
|
|
345
171
|
analyticsData: {
|
|
346
|
-
// any additional data can live here
|
|
347
172
|
style: 'fancy'
|
|
348
173
|
},
|
|
349
174
|
});
|
|
350
175
|
|
|
351
176
|
return (
|
|
352
|
-
// In this example the synthetic event is triggered by onFocus
|
|
353
177
|
<button onClick={handler} onFocus={syntheticHandler}>Track me</button>
|
|
354
178
|
);
|
|
355
179
|
}
|
|
356
180
|
|
|
357
181
|
export MyButton;
|
|
358
|
-
`}
|
|
359
|
-
|
|
360
182
|
`;
|
|
361
|
-
|
|
183
|
+
|
|
184
|
+
export default function UsageWithPresentationalComponents(): React.JSX.Element {
|
|
185
|
+
return (
|
|
186
|
+
<div>
|
|
187
|
+
<p>
|
|
188
|
+
This section will guide how to add analytics tracking to presentational and other components
|
|
189
|
+
that don't fit into the "Container" category.
|
|
190
|
+
</p>
|
|
191
|
+
<p>
|
|
192
|
+
For a conceptual overview of <code>@atlaskit/analytics-next</code>, please consult the{' '}
|
|
193
|
+
<a href="./concepts">concepts page</a>.
|
|
194
|
+
</p>
|
|
195
|
+
<p>
|
|
196
|
+
There are several ways to add analytics to a component, via React hooks, or via higher order
|
|
197
|
+
components (HOCs) - consult the Recommended Usage table for our suggested approach.
|
|
198
|
+
</p>
|
|
199
|
+
<h2>API</h2>
|
|
200
|
+
<h3>Hooks API</h3>
|
|
201
|
+
<ul>
|
|
202
|
+
<li>
|
|
203
|
+
<a href="#use-analytics-events">
|
|
204
|
+
<code>useAnalyticsEvents</code>
|
|
205
|
+
</a>
|
|
206
|
+
</li>
|
|
207
|
+
<li>
|
|
208
|
+
<a href="#use-callback-with-analytics">
|
|
209
|
+
<code>useCallbackWithAnalytics</code>
|
|
210
|
+
</a>
|
|
211
|
+
</li>
|
|
212
|
+
<li>
|
|
213
|
+
<a href="#use-platform-leaf-event-handlers">
|
|
214
|
+
<code>usePlatformLeafEventHandler</code> and{' '}
|
|
215
|
+
<code>usePlatformLeafSyntheticEventHandler</code>
|
|
216
|
+
</a>
|
|
217
|
+
</li>
|
|
218
|
+
</ul>
|
|
219
|
+
<h3>HOCs API</h3>
|
|
220
|
+
<ul>
|
|
221
|
+
<li>
|
|
222
|
+
<a href="#with-analytics-events">
|
|
223
|
+
<code>withAnalyticsEvents</code>
|
|
224
|
+
</a>
|
|
225
|
+
</li>
|
|
226
|
+
</ul>
|
|
227
|
+
<h3>Recommended Usage</h3>
|
|
228
|
+
<table>
|
|
229
|
+
<tbody>
|
|
230
|
+
<tr>
|
|
231
|
+
<th>Component Type</th>
|
|
232
|
+
<th>Recommendation</th>
|
|
233
|
+
</tr>
|
|
234
|
+
<tr>
|
|
235
|
+
<td>Class components</td>
|
|
236
|
+
<td>
|
|
237
|
+
You have only the <a href="#with-analytics-events">HOC option</a>
|
|
238
|
+
</td>
|
|
239
|
+
</tr>
|
|
240
|
+
<tr>
|
|
241
|
+
<td>Atlaskit function components</td>
|
|
242
|
+
<td>
|
|
243
|
+
Use{' '}
|
|
244
|
+
<a href="#use-platform-leaf-event-handlers">
|
|
245
|
+
<code>usePlatformLeafEventHandler</code>
|
|
246
|
+
</a>
|
|
247
|
+
.
|
|
248
|
+
</td>
|
|
249
|
+
</tr>
|
|
250
|
+
<tr>
|
|
251
|
+
<td>Other function components</td>
|
|
252
|
+
<td>
|
|
253
|
+
Use{' '}
|
|
254
|
+
<a href="#use-callback-with-analytics">
|
|
255
|
+
<code>useCallbackWithAnalytics</code>
|
|
256
|
+
</a>{' '}
|
|
257
|
+
if you want something basic.
|
|
258
|
+
<br />
|
|
259
|
+
Use{' '}
|
|
260
|
+
<a href="#use-analytics-events">
|
|
261
|
+
<code>useAnalyticsEvents</code>
|
|
262
|
+
</a>{' '}
|
|
263
|
+
if you want more control.
|
|
264
|
+
</td>
|
|
265
|
+
</tr>
|
|
266
|
+
</tbody>
|
|
267
|
+
</table>
|
|
268
|
+
|
|
269
|
+
<h3>Examples</h3>
|
|
270
|
+
<h4 id="use-analytics-events">
|
|
271
|
+
The <code>useAnalyticsEvents</code> hook
|
|
272
|
+
</h4>
|
|
273
|
+
<p>
|
|
274
|
+
This custom React hook provides a method <code>createAnalyticsEvent</code> for creating{' '}
|
|
275
|
+
<code>UIAnalyticsEvent</code>s.
|
|
276
|
+
</p>
|
|
277
|
+
<CodeBlock code={useAnalyticsEventsCode} />
|
|
278
|
+
<h4 id="use-callback-with-analytics">
|
|
279
|
+
The <code>useCallbackWithAnalytics</code> hook
|
|
280
|
+
</h4>
|
|
281
|
+
<p>
|
|
282
|
+
This custom React hook takes a callback function and an event payload, and channel, and
|
|
283
|
+
returns a callback to fire the event and call the provided function.
|
|
284
|
+
</p>
|
|
285
|
+
<p>The hooks stores the input and memoizes the return value to optimize performance.</p>
|
|
286
|
+
<CodeBlock code={useCallbackWithAnalyticsCode} />
|
|
287
|
+
<h4 id="with-analytics-events">
|
|
288
|
+
The <code>withAnalyticsEvents</code> HOC
|
|
289
|
+
</h4>
|
|
290
|
+
<p>
|
|
291
|
+
A HOC which provides the wrapped component with a method for creating{' '}
|
|
292
|
+
<code>UIAnalyticsEvent</code>s, via <code>props.createAnalyticsEvent</code>.
|
|
293
|
+
</p>
|
|
294
|
+
<p>The HOC supports a few ways to use it for convinvience.</p>
|
|
295
|
+
<p>
|
|
296
|
+
The first is to use the <code>createAnalyticsEvent</code> prop the HOC passes to the
|
|
297
|
+
component manually:
|
|
298
|
+
</p>
|
|
299
|
+
<CodeBlock code={hocManualCode} />
|
|
300
|
+
<p>
|
|
301
|
+
Altenatively, the HOC accepts an optional map as its first argument, which provides a
|
|
302
|
+
shortcut for passing a new analytics event as an additional parameter to the corresponding
|
|
303
|
+
callback prop.
|
|
304
|
+
</p>
|
|
305
|
+
<CodeBlock code={hocMappedCode} />
|
|
306
|
+
<p>
|
|
307
|
+
Since creating and returning an event is such a common pattern an even more concise
|
|
308
|
+
shorthand is supported:
|
|
309
|
+
</p>
|
|
310
|
+
<CodeBlock code={hocShorthandCode} />
|
|
311
|
+
<h4 id="use-platform-leaf-event-handlers">
|
|
312
|
+
The <code>usePlatformLeafEventHandler</code> and{' '}
|
|
313
|
+
<code>usePlatformLeafSyntheticEventHandler</code> hooks
|
|
314
|
+
</h4>
|
|
315
|
+
<p>These hooks were built with internal leaf node components purely in mind.</p>
|
|
316
|
+
<p>
|
|
317
|
+
They dispatch an event on the <code>atlaskit</code> channel, then pass it to the wrapped
|
|
318
|
+
function as the last argument in case you want to something additional with the event.
|
|
319
|
+
</p>
|
|
320
|
+
<p>
|
|
321
|
+
<code>usePlatformLeafEventHandler</code> takes a function with two arguments (
|
|
322
|
+
<code>value</code> and <code>analyticsEvent</code>), while{' '}
|
|
323
|
+
<code>usePlatformLeafSyntheticEventHandler</code> takes a function with just one argument
|
|
324
|
+
for the <code>analyticsEvent</code>.
|
|
325
|
+
</p>
|
|
326
|
+
<p>
|
|
327
|
+
<strong>WARNING:</strong>{' '}
|
|
328
|
+
<em>
|
|
329
|
+
These hooks make an assumption that the component being wrapped is a "leaf-node"
|
|
330
|
+
component, i.e., they have no children that require analytics themselves. This is so it
|
|
331
|
+
can include the component name, package name, and package version as context in the
|
|
332
|
+
analytics event. It assumes no children need this context and makes no attempt to pass it
|
|
333
|
+
to them. This gains us a decent performance optimization. Use the other hooks if you are
|
|
334
|
+
unsure you can use these safely.
|
|
335
|
+
</em>
|
|
336
|
+
</p>
|
|
337
|
+
<CodeBlock code={leafHandlersCode} />
|
|
338
|
+
</div>
|
|
339
|
+
);
|
|
340
|
+
}
|
|
@@ -1,64 +1,80 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable @atlaskit/design-system/use-primitives-text, @atlaskit/design-system/use-heading, @atlaskit/design-system/no-html-anchor -- Legacy analytics-next docs intentionally use plain HTML prose and links instead of ADS docs primitives. */
|
|
2
|
+
import React from 'react';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
This guide describes how to set up analytics for a React component that is a
|
|
5
|
-
container for children that fire analytics events, and where you wish to include
|
|
6
|
-
contextual information to those children about where it is embeded in the event fired.
|
|
4
|
+
import { CodeBlock } from './DocBlocks';
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const hocCode = `
|
|
7
|
+
import { withAnalyticsContext } from '@atlaskit/analytics-next';
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
const MyContainer = ({children}) => {
|
|
10
|
+
return (
|
|
11
|
+
<div>
|
|
12
|
+
{children}
|
|
13
|
+
</div>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
12
16
|
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
const AnalyticsWrappedContainer = withAnalyticsContext({
|
|
18
|
+
issueId: 'ABC-123',
|
|
19
|
+
issueType: 'bug'
|
|
20
|
+
})(MyContainer);
|
|
15
21
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
#### 1) Using the \`withAnalyticsContext\` higher order component (Recommended)
|
|
19
|
-
|
|
20
|
-
${code`
|
|
21
|
-
import { withAnalyticsContext } from '@atlaskit/analytics-next';
|
|
22
|
+
export AnalyticsWrappedContainer;
|
|
23
|
+
`;
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
<div>
|
|
26
|
-
{children}
|
|
27
|
-
</div>
|
|
28
|
-
);
|
|
29
|
-
}
|
|
25
|
+
const manualCode = `
|
|
26
|
+
import { AnalyticsContext } from '@atlaskit/analytics-next';
|
|
30
27
|
|
|
31
|
-
|
|
28
|
+
export MyContainer = ({children}) => {
|
|
29
|
+
const data = {
|
|
32
30
|
issueId: 'ABC-123',
|
|
33
31
|
issueType: 'bug'
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export AnalyticsWrappedContainer;
|
|
37
|
-
`}
|
|
38
|
-
|
|
39
|
-
#### 2) Use the \`AnalyticsContext\` component manually
|
|
32
|
+
};
|
|
40
33
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
return (
|
|
51
|
-
<div>
|
|
52
|
-
<AnalyticsContext data={data}>
|
|
53
|
-
{children}
|
|
54
|
-
</AnalyticsContext>
|
|
55
|
-
</div>
|
|
56
|
-
);
|
|
57
|
-
`}
|
|
58
|
-
|
|
59
|
-
#### Side note
|
|
60
|
-
While it is possible to use the React context from \`@atlaskit/analytics-next-stable-react-context\` directly,
|
|
61
|
-
we encourage you to use one of these two approaches instead, as they are optimised to ensure the value provided
|
|
62
|
-
to React Context is a stable reference, and won't introduce unnecessary re-renders.
|
|
34
|
+
return (
|
|
35
|
+
<div>
|
|
36
|
+
<AnalyticsContext data={data}>
|
|
37
|
+
{children}
|
|
38
|
+
</AnalyticsContext>
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
63
41
|
`;
|
|
64
|
-
|
|
42
|
+
|
|
43
|
+
export default function UsageForContainerComponents(): React.JSX.Element {
|
|
44
|
+
return (
|
|
45
|
+
<div>
|
|
46
|
+
<p>
|
|
47
|
+
This guide describes how to set up analytics for a React component that is a container for
|
|
48
|
+
children that fire analytics events, and where you wish to include contextual information to
|
|
49
|
+
those children about where it is embeded in the event fired.
|
|
50
|
+
</p>
|
|
51
|
+
<p>
|
|
52
|
+
For example, you might fire an event on a button click, but want to distinguish whether it
|
|
53
|
+
was from inside a form or on the navigation panel.
|
|
54
|
+
</p>
|
|
55
|
+
<p>
|
|
56
|
+
Analytics Contexts are the way this is achieved in <code>@atlaskit/analytics-next</code>.
|
|
57
|
+
</p>
|
|
58
|
+
<p>
|
|
59
|
+
For a conceptual overview of <code>@atlaskit/analytics-next</code>, please consult the{' '}
|
|
60
|
+
<a href="./concepts">concepts page</a>.
|
|
61
|
+
</p>
|
|
62
|
+
<p>Container components can pass this context in two key ways:</p>
|
|
63
|
+
<h4>
|
|
64
|
+
1) Using the <code>withAnalyticsContext</code> higher order component (Recommended)
|
|
65
|
+
</h4>
|
|
66
|
+
<CodeBlock code={hocCode} />
|
|
67
|
+
<h4>
|
|
68
|
+
2) Use the <code>AnalyticsContext</code> component manually
|
|
69
|
+
</h4>
|
|
70
|
+
<CodeBlock code={manualCode} />
|
|
71
|
+
<h4>Side note</h4>
|
|
72
|
+
<p>
|
|
73
|
+
While it is possible to use the React context from{' '}
|
|
74
|
+
<code>@atlaskit/analytics-next-stable-react-context</code> directly, we encourage you to use
|
|
75
|
+
one of these two approaches instead, as they are optimised to ensure the value provided to
|
|
76
|
+
React Context is a stable reference, and won't introduce unnecessary re-renders.
|
|
77
|
+
</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
package/docs/40-listeners.tsx
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
+
/* eslint-disable @atlaskit/design-system/use-primitives-text -- Legacy analytics-next docs intentionally use plain HTML prose instead of ADS docs primitives. */
|
|
1
2
|
import React from 'react';
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
+
import { CodeBlock, PropsBlock } from './DocBlocks';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
6
|
+
export default function Listeners(): React.JSX.Element {
|
|
7
|
+
return (
|
|
8
|
+
<div>
|
|
9
|
+
<CodeBlock code={`import { AnalyticsListener } from '@atlaskit/analytics-next';`} />
|
|
10
|
+
<p>
|
|
11
|
+
An <code>AnalyticsListener</code> wraps your app and listens to any events which are fired
|
|
12
|
+
within it.
|
|
13
|
+
</p>
|
|
14
|
+
<PropsBlock
|
|
15
|
+
heading="AnalyticsListener Props"
|
|
16
|
+
props={require('!!extract-react-types-loader!../src/components/AnalyticsListener/ModernAnalyticsListener')}
|
|
17
|
+
/>
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
}
|