useTrackEvent hook for the React SDK
Describes the useTrackEvent hook, which enables tracking events within functional components. This hook offers all the existing track event functionalities provided by the SDK.
Version
SDK v3.2.0 and higher.
useTrackEvent hook
Enables tracking events within functional components. This hook offers the existing track event functionalities provided by the SDK.
Arguments
None.
Returns
Returns the following array:
Key | Type | Description |
---|---|---|
track | (eventKey: string, overrideUserId?: string | EventTags | undefined, overrideAttributes?: UserAttributes | undefined, eventTags?: EventTags | undefined) => void | Track method of the ReactSDKClient instance. See track event method for the JavaScript SDK for information. |
clientReady | Boolean | Indicates whether the ReactSDKClient instance is ready |
didTimeout | Boolean | Indicates whether the ReactSDKClient instance became ready within the allowed timeout range. |
Note
clientReady can be true even if didTimeout is also true. This indicates that the client became ready after the timeout period.
Example
import { useTrackEvent } from '@optimizely/react-sdk';
function SignupButton() {
const [track, clientReady, didTimeout] = useTrackEvent()
const handleClick = () => {
if(clientReady) {
track('signup-clicked')
}
}
return (
<button onClick={handleClick}>Signup</button>
)
}
Updated 4 months ago