@unifygtm/intent-react
v1.1.3
Published
Library for using the Unify Intent JS Client in a React app.
Downloads
5,828
Readme
Unify Intent React
Library for using the Unify Intent JS Client in a React app.
Installation
npm
npm install @unifygtm/intent-react
yarn
yarn add @unifygtm/intent-react
Usage
Wrap your React app in a UnifyIntentProvider
:
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(
<UnifyIntentProvider intentClient={intentClient}>
<App />
</UnifyIntentProvider>
);
Then, any components rendered in your app can access the intent client
using the useUnifyIntent
hook:
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]);
};