@automattic/jetpack-shared-extension-utils 0.6.9 → 0.7.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.7.0] - 2022-12-19
9
+ ### Added
10
+ - Add new Analytics wrapper hook. [#27937]
11
+ - Add new isCurrentUserConnected utility. [#27923]
12
+
13
+ ## [0.6.10] - 2022-12-06
14
+ ### Changed
15
+ - Updated package dependencies. [#27688, #27696, #27697]]
16
+
8
17
  ## [0.6.9] - 2022-11-28
9
18
  ### Changed
10
19
  - Updated package dependencies. [#27043]
@@ -140,6 +149,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
140
149
  ### Changed
141
150
  - Core: prepare utility for release
142
151
 
152
+ [0.7.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.10...0.7.0
153
+ [0.6.10]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.9...0.6.10
143
154
  [0.6.9]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.8...0.6.9
144
155
  [0.6.8]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.7...0.6.8
145
156
  [0.6.7]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.6.6...0.6.7
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.9",
3
+ "version": "0.7.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,14 +15,17 @@
15
15
  "author": "Automattic",
16
16
  "scripts": {},
17
17
  "dependencies": {
18
- "@wordpress/compose": "5.19.0",
19
- "@wordpress/i18n": "4.21.0",
20
- "@wordpress/plugins": "4.19.0",
21
- "@wordpress/url": "3.22.0",
18
+ "@automattic/jetpack-analytics": "workspace:*",
19
+ "@automattic/jetpack-connection": "workspace:*",
20
+ "@wordpress/compose": "5.20.0",
21
+ "@wordpress/element": "4.20.0",
22
+ "@wordpress/i18n": "4.22.0",
23
+ "@wordpress/plugins": "4.20.0",
24
+ "@wordpress/url": "3.23.0",
22
25
  "lodash": "4.17.21"
23
26
  },
24
27
  "devDependencies": {
25
- "@babel/core": "7.20.2",
28
+ "@babel/core": "7.20.5",
26
29
  "@babel/preset-react": "7.18.6",
27
30
  "react": "17.0.2"
28
31
  },
@@ -0,0 +1,23 @@
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
+ ```
@@ -0,0 +1,26 @@
1
+ import jetpackAnalytics from '@automattic/jetpack-analytics';
2
+ import { useConnection } from '@automattic/jetpack-connection';
3
+ import { useEffect } from '@wordpress/element';
4
+
5
+ const useAnalytics = () => {
6
+ const { isUserConnected, userConnectionData = {} } = useConnection();
7
+ const { wpcomUser: { login, ID } = {}, blogId } = userConnectionData.currentUser || {};
8
+
9
+ /**
10
+ * Initialize tracks with user and blog data.
11
+ * This will only work if the user is connected.
12
+ */
13
+ useEffect( () => {
14
+ if ( ! isUserConnected || ! ID || ! login || ! blogId ) {
15
+ return;
16
+ }
17
+
18
+ jetpackAnalytics.initialize( ID, login, {
19
+ blog_id: blogId,
20
+ } );
21
+ }, [ blogId, ID, login, isUserConnected ] );
22
+
23
+ return jetpackAnalytics;
24
+ };
25
+
26
+ 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
+ }