Library for using the Unify Intent JS Client in a React app.
Library for using the Unify Intent JS Client in a React app.
``Shell`
npm install @unifygtm/intent-react
`Shell`
yarn add @unifygtm/intent-react
Wrap your React app in a UnifyIntentProvider:
`TSX
import {
UnifyIntentClient,
UnifyIntentClientConfig,
UnifyIntentProvider
} from '@unifygtm/intent-react';
const writeKey = 'YOUR_PUBLIC_API_KEY';
const config: UnifyIntentClientConfig = {
autoPage: true,
autoIdentify: false,
};
const intentClient = new UnifyIntentClient(writeKey, config);
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement,
);
root.render(
);
`
Then, any components rendered in your app can access the intent client
using the useUnifyIntent hook:
`TSX
import { useUnifyIntent } from '@unifygtm/intent-react';
const SomeComponent = () => {
// However you access the current user...
const currentUser = useCurrentUser();
const unify = useUnifyIntent();
useEffect(() => {
// Log an identify event for the current user
unify.identify(currentUser.emailAddress);
}, [currentUser.emailAddress]);
};
``