@automattic/jetpack-shared-extension-utils 0.6.10 → 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 CHANGED
@@ -5,6 +5,15 @@ 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
+
12
+ ## [0.7.0] - 2022-12-19
13
+ ### Added
14
+ - Add new Analytics wrapper hook. [#27937]
15
+ - Add new isCurrentUserConnected utility. [#27923]
16
+
8
17
  ## [0.6.10] - 2022-12-06
9
18
  ### Changed
10
19
  - Updated package dependencies. [#27688, #27696, #27697]]
@@ -144,6 +153,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
144
153
  ### Changed
145
154
  - Core: prepare utility for release
146
155
 
156
+ [0.8.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.7.0...0.8.0
157
+ [0.7.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.10...0.7.0
147
158
  [0.6.10]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.9...0.6.10
148
159
  [0.6.9]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.8...0.6.9
149
160
  [0.6.8]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.7...0.6.8
package/index.js CHANGED
@@ -13,3 +13,5 @@ export {
13
13
  isStillUsableWithFreePlan,
14
14
  getUsableBlockProps,
15
15
  } from './src/plan-utils';
16
+ export { default as isCurrentUserConnected } from './src/is-current-user-connected';
17
+ export { default as useAnalytics } from './src/hooks/use-analytics';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/jetpack-shared-extension-utils",
3
- "version": "0.6.10",
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": {
@@ -15,7 +15,10 @@
15
15
  "author": "Automattic",
16
16
  "scripts": {},
17
17
  "dependencies": {
18
+ "@automattic/jetpack-analytics": "workspace:*",
19
+ "@automattic/jetpack-connection": "workspace:*",
18
20
  "@wordpress/compose": "5.20.0",
21
+ "@wordpress/element": "4.20.0",
19
22
  "@wordpress/i18n": "4.22.0",
20
23
  "@wordpress/plugins": "4.20.0",
21
24
  "@wordpress/url": "3.23.0",
@@ -0,0 +1,45 @@
1
+ # useAnalytics() hook
2
+
3
+ This wrapper for @automattic/jetpack-analytics provides a way to use the analytics library (Tracks) in a React component inside your plugin.
4
+
5
+ This library is only useful when the logged in user is connected to WordPress.com, and when we have information about that connection passed to JavaScript via the Jetpack Connection package. Typically, this is done by enqueuing the connection package's initial state on the editor page:
6
+
7
+ ```php
8
+ use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
9
+ // Adds Connection package initial state.
10
+ wp_add_inline_script( 'your-app-script-handle-in-editor', Connection_Initial_State::render(), 'before' );
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```es6
16
+ const { tracks } = useAnalytics();
17
+
18
+ tracks.recordEvent( 'jetpack_editor_block_upgrade_click', {
19
+ plan,
20
+ block: blockName,
21
+ context,
22
+ } );
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
+ ```
@@ -0,0 +1,86 @@
1
+ import jetpackAnalytics from '@automattic/jetpack-analytics';
2
+ import { useConnection } from '@automattic/jetpack-connection';
3
+ import { useEffect, useState, useCallback } from '@wordpress/element';
4
+
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();
16
+ const { wpcomUser: { login, ID } = {}, blogId } = userConnectionData.currentUser || {};
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
+
33
+ /**
34
+ * Initialize tracks with user and blog data.
35
+ * This will only work if the user is connected.
36
+ */
37
+ useEffect( () => {
38
+ if ( ! isUserConnected || ! ID || ! login || ! blogId ) {
39
+ return;
40
+ }
41
+
42
+ jetpackAnalytics.initialize( ID, login, {
43
+ blog_id: blogId,
44
+ } );
45
+ }, [ blogId, ID, login, isUserConnected ] );
46
+
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
+ };
84
+ };
85
+
86
+ export default useAnalytics;
@@ -0,0 +1,10 @@
1
+ import getJetpackData from './get-jetpack-data';
2
+
3
+ /**
4
+ * Return whether the current user is connected to WP.com.
5
+ *
6
+ * @returns {boolean} Whether the current user is connected.
7
+ */
8
+ export default function isCurrentUserConnected() {
9
+ return getJetpackData()?.jetpack?.is_current_user_connected ?? false;
10
+ }