@automattic/jetpack-shared-extension-utils 0.7.0 → 0.8.0
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 +5 -0
- package/package.json +1 -1
- package/src/hooks/readme.md +22 -0
- package/src/hooks/use-analytics.js +64 -4
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.8.0] - 2023-01-02
|
|
9
|
+
### Added
|
|
10
|
+
- Add additional methods to useAnalytics hook, allow for view event by passing initial props [#28072]
|
|
11
|
+
|
|
8
12
|
## [0.7.0] - 2022-12-19
|
|
9
13
|
### Added
|
|
10
14
|
- Add new Analytics wrapper hook. [#27937]
|
|
@@ -149,6 +153,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
149
153
|
### Changed
|
|
150
154
|
- Core: prepare utility for release
|
|
151
155
|
|
|
156
|
+
[0.8.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.7.0...0.8.0
|
|
152
157
|
[0.7.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.10...0.7.0
|
|
153
158
|
[0.6.10]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.9...0.6.10
|
|
154
159
|
[0.6.9]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.8...0.6.9
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automattic/jetpack-shared-extension-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Utility functions used by the block editor extensions",
|
|
5
5
|
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
|
|
6
6
|
"bugs": {
|
package/src/hooks/readme.md
CHANGED
|
@@ -21,3 +21,25 @@ tracks.recordEvent( 'jetpack_editor_block_upgrade_click', {
|
|
|
21
21
|
context,
|
|
22
22
|
} );
|
|
23
23
|
```
|
|
24
|
+
|
|
25
|
+
The hook function also accepts parameters to record a "page view" event when the component renders.
|
|
26
|
+
You can also import a wrapped version of `recordEvent` that checks for a Jetpack connected user before actually recording the event.
|
|
27
|
+
|
|
28
|
+
```es6
|
|
29
|
+
const Component = () => {
|
|
30
|
+
const { recordEvent } = useAnalytics( {
|
|
31
|
+
pageViewEventName: 'view_event_name',
|
|
32
|
+
pageViewNamespace: 'jetpack',
|
|
33
|
+
pageViewSuffix: '',
|
|
34
|
+
} );
|
|
35
|
+
const recordClick = useCallback( () => { recordEvent( 'event_name', {} ) }, [] );
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
<Button
|
|
39
|
+
onClick={ recordClick }
|
|
40
|
+
>
|
|
41
|
+
{ __( 'Action Button', 'jetpack' ) }
|
|
42
|
+
</Button>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
```
|
|
@@ -1,11 +1,35 @@
|
|
|
1
1
|
import jetpackAnalytics from '@automattic/jetpack-analytics';
|
|
2
2
|
import { useConnection } from '@automattic/jetpack-connection';
|
|
3
|
-
import { useEffect } from '@wordpress/element';
|
|
3
|
+
import { useEffect, useState, useCallback } from '@wordpress/element';
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
5
|
+
const { tracks } = jetpackAnalytics;
|
|
6
|
+
const { recordEvent } = tracks;
|
|
7
|
+
|
|
8
|
+
const useAnalytics = ( {
|
|
9
|
+
pageViewEventName = null,
|
|
10
|
+
pageViewNamespace = 'jetpack',
|
|
11
|
+
pageViewSuffix = 'page_view',
|
|
12
|
+
pageViewEventProperties = {},
|
|
13
|
+
} = {} ) => {
|
|
14
|
+
const [ pageViewRecorded, setPageViewRecorded ] = useState( false );
|
|
15
|
+
const { isUserConnected, isRegistered, userConnectionData = {} } = useConnection();
|
|
7
16
|
const { wpcomUser: { login, ID } = {}, blogId } = userConnectionData.currentUser || {};
|
|
8
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Record an event async
|
|
20
|
+
* Check to ensure there is a connected user first
|
|
21
|
+
*/
|
|
22
|
+
const recordEventAsync = useCallback(
|
|
23
|
+
async ( event, properties = {} ) => {
|
|
24
|
+
// Do nothing if there is not a connected user.
|
|
25
|
+
if ( ! ( isUserConnected && ID && login ) ) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
recordEvent( event, properties );
|
|
29
|
+
},
|
|
30
|
+
[ isUserConnected, ID, login ]
|
|
31
|
+
);
|
|
32
|
+
|
|
9
33
|
/**
|
|
10
34
|
* Initialize tracks with user and blog data.
|
|
11
35
|
* This will only work if the user is connected.
|
|
@@ -20,7 +44,43 @@ const useAnalytics = () => {
|
|
|
20
44
|
} );
|
|
21
45
|
}, [ blogId, ID, login, isUserConnected ] );
|
|
22
46
|
|
|
23
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Track a page-view type event.
|
|
49
|
+
* It's considered a page view event when the component is mounted.
|
|
50
|
+
*/
|
|
51
|
+
useEffect( () => {
|
|
52
|
+
const pageViewEvent = pageViewEventName
|
|
53
|
+
? `${ pageViewNamespace }_${ pageViewEventName }_${ pageViewSuffix }`
|
|
54
|
+
: null;
|
|
55
|
+
|
|
56
|
+
// Also, only run if the site is registered.
|
|
57
|
+
if ( ! isRegistered ) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if ( ! pageViewEvent ) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Ensuring we only record the view event once
|
|
66
|
+
if ( ! pageViewRecorded ) {
|
|
67
|
+
recordEventAsync( pageViewEvent, pageViewEventProperties );
|
|
68
|
+
setPageViewRecorded( true );
|
|
69
|
+
}
|
|
70
|
+
}, [
|
|
71
|
+
pageViewRecorded,
|
|
72
|
+
pageViewNamespace,
|
|
73
|
+
pageViewEventName,
|
|
74
|
+
pageViewSuffix,
|
|
75
|
+
isRegistered,
|
|
76
|
+
pageViewEventProperties,
|
|
77
|
+
recordEventAsync,
|
|
78
|
+
] );
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
recordEvent: recordEventAsync,
|
|
82
|
+
tracks: tracks,
|
|
83
|
+
};
|
|
24
84
|
};
|
|
25
85
|
|
|
26
86
|
export default useAnalytics;
|