import { branding } from "../../lib/branding"; import { CodeBlock } from "../CodeBlock/CodeBlock"; export const ReactManual: React.FC<{ domain: string; writeKey?: string }> = ({ domain, writeKey }) => { return (

Tracking Events with React

Setup

Start with adding dependency to your project: npm install {branding.npmNamespace}/jitsu-react. Then add{" "} {"<"}JitsuProvider{" />"} {" "} component close to the root level of your app:

{[ `import React, { Component } from 'react'`, `import { JitsuProvider } from "@jitsu/jitsu-react";`, ``, `function App() {`, ` return (`, ` `, ` `, ` `, ` );`, `}`, ].join("\n")}

Manual event tracking

Call use{branding.productName} hook whenever you need manually trigger events object:

{[ `import { use${branding.productName} } from "${branding.npmNamespace}/jitsu-react"`, ``, `function ChildComponent() {`, ` const { analytics } = use${branding.productName}()`, ` return `, `}`, ].join("\n")}

Automatic page event tracking

{branding.productName} can automatically track page events when user navigates to a new page:

With react-router:

{[ `import { use${branding.productName} } from "${branding.npmNamespace}/jitsu-react"`, `import { useLocation } from "react-router-dom"`, "", `function ChildComponent() {`, ` const { analytics } = use${branding.productName}()`, ` const location = useLocation()`, ` return useEffect(() => {`, ` analytics.page()`, ` }, [location])`, `}`, ].join("\n")}

With Next.js:

{[ `import { use${branding.productName} } from "${branding.npmNamespace}/jitsu-react"`, `import { useRouter } from "next/router"`, "", `function ChildComponent() {`, ` const { analytics } = use${branding.productName}()`, ` const router = useRouter()`, ` return useEffect(() => {`, ` analytics.page()`, ` }, [router.asPath])`, `}`, ].join("\n")}
); };