Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
primogenial
/
wp-content-20241001222009
/
plugins
/
jetpack
/
jetpack_vendor
/
automattic
/
jetpack-backup
/
src
/
js
/
hooks
:
useAnalytics.js
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
import jetpackAnalytics from '@automattic/jetpack-analytics'; import { useConnection } from '@automattic/jetpack-connection'; import { useSelect } from '@wordpress/data'; import { useEffect } from 'react'; import { STORE_ID } from '../store'; const useAnalytics = () => { const APINonce = useSelect( select => select( STORE_ID ).getAPINonce(), [] ); const APIRoot = useSelect( select => select( STORE_ID ).getAPIRoot(), [] ); const { isUserConnected, userConnectionData = {} } = useConnection( { APIRoot, APINonce } ); const { login, ID } = userConnectionData.currentUser?.wpcomUser || {}; /** * Initialize tracks with user data. * Should run when we have a connected user. */ useEffect( () => { if ( ! ( isUserConnected && ID && login ) ) { return; } jetpackAnalytics.initialize( ID, login ); }, [ ID, isUserConnected, login ] ); return jetpackAnalytics; }; export default useAnalytics;