react-mixpanel-browser
Wrapper of mixpanel-browser for use in React apps.
Provides a Mixpanel
instance via the context API.
Installation
NPM
npm install --save react-mixpanel-browser
Yarn
yarn add react-mixpanel-browser
Usage
MixpanelProvider
Wrap your application with the <MixpanelProvider />
component:
import { MixpanelProvider } from 'react-mixpanel-browser';
const MIXPANEL_TOKEN = 'MyToken';
const MIXPANEL_CONFIG = {
};
const App = (props) => (
<MixpanelProvider config={MIXPANEL_CONFIG} token={MIXPANEL_TOKEN}>
{/* ... */}
</MixpanelProvider>
);
export default App;
useMixpanel()
Call the useMixpanel()
hook when you need to interact with Mixpanel:
import { useMixpanel } from 'react-mixpanel-browser';
const Dashboard = (props) => {
const mixpanel = useMixpanel();
useEffect(() => {
if (!mixpanel) {
return;
}
mixpanel.track('DashboadView', {
my_custom_prop: 'foo',
});
}, [mixpanel]);
return <div>Dashboard</div>;
};
export default Dashboard;