The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

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:

KeyTypeDescription
track(eventKey: string, overrideUserId?: string | EventTags | undefined, overrideAttributes?: UserAttributes | undefined, eventTags?: EventTags | undefined) => voidTrack method of the ReactSDKClient instance. See track event method for the JavaScript SDK for information.
clientReadyBooleanIndicates whether the ReactSDKClient instance is ready
didTimeoutBooleanIndicates 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>
  )
}