Getting Started With React.js or React Router
Set up Flowgrid analytics on your React app in just a few easy steps. The guide below shows you how to integrate Flowgrid using React Router (React 18+)
Add the tracking script to app layout
Create your new website and add the following script tag preferably in the layout.jsx section:
- Within your project's components folder create
InitialScript.jsxfile. - Open your project's
app.jsxfile then import the component above into your app router - Add the Flowgrid tracking script tag inside the
headsection:
1import { useEffect } from 'react';
2
3const InitialScript = () => {
4 useEffect(() => {
5 const script = document.createElement('script');
6 script.src = 'https://core.flow-grid.xyz/flowgrid.js';
7 script.async = true;
8 script.setAttribute('data-site', 'YOUR_WEBSITE_ID');
9 script.setAttribute('data-api-key', 'YOUR_API_KEY');
10 document.head.appendChild(script);
11
12 return () => {
13 // Optional cleanup
14 document.head.removeChild(script);
15 };
16 }, []);
17
18 return null;
19};
20
21export default InitialScript;
22Use the npm SDK + drop-in helpers
Want typed, in-app tracking instead of the script tag? Install the SDK and lean on the built-in helpers so you never re-implement consent wiring or stable IDs by hand. They import directly from flowgrid-sdk.
1. Install
1npm install flowgrid-sdk
2# or: pnpm add flowgrid-sdk / yarn add flowgrid-sdk2. Create a single setup file
Initialise FlowGrid once and re-export the helpers. setupConsent creates a consent manager andkeeps FlowGrid's consent gates in sync automatically.
1// src/lib/flowgrid.ts
2import {
3 FlowGrid,
4 setupConsent,
5 attachConsentBanner,
6 getOrCreateAnonId,
7} from "flowgrid-sdk";
8
9export const flowgrid = FlowGrid.init({
10 // Vite env vars (use process.env.REACT_APP_* for Create React App).
11 webId: import.meta.env.VITE_FLOWGRID_WEB_ID,
12 apiKey: import.meta.env.VITE_FLOWGRID_API_KEY,
13 autoTrack: { performance: true, engagement: true, replay: true },
14});
15
16export const track = flowgrid.track.bind(flowgrid);
17
18// One call creates a consent manager AND keeps FlowGrid's consent gates in sync.
19export const getConsent = () =>
20 setupConsent({ cookieName: "my_app_consent", cookieDays: 180, respectDNT: true });
21
22// Re-export the drop-in helpers so the rest of your app imports from one place.
23export { attachConsentBanner, getOrCreateAnonId };3. Cookie consent banner
Mount this component near your app root. No manual transport sync — getConsent() handled it.
1// src/components/CookieBanner.tsx
2import { useEffect, useState } from "react";
3import { getConsent } from "../lib/flowgrid";
4
5export function CookieBanner() {
6 const [manager, setManager] = useState<ReturnType<typeof getConsent>>();
7 const [visible, setVisible] = useState(false);
8
9 useEffect(() => {
10 const cm = getConsent(); // already pushes stored prefs into FlowGrid
11 setManager(cm);
12 if (!cm.hasConsented()) setVisible(true);
13 }, []);
14
15 if (!visible) return null;
16
17 return (
18 <div id="cookie-banner" className="cookie-banner">
19 <button onClick={() => { manager?.rejectNonEssential(); setVisible(false); }}>
20 Reject non-essential
21 </button>
22 <button onClick={() => { manager?.acceptAll(); setVisible(false); }}>
23 Accept all
24 </button>
25 </div>
26 );
27}Not using React for the banner?
CallattachConsentBanner() instead — it wires the#accept-btn /#reject-btn buttons inside any#cookie-banner element for you.4. Track with stable IDs
getOrCreateAnonId() persists a stable visitor id, so product and cart events from one visitor line up automatically. Revenue comes from your connected payment provider, not from browser events.
1import { track, getOrCreateAnonId } from "../lib/flowgrid";
2
3// Product view — stable anonymous id, no boilerplate.
4track("product_view", {
5 productId: p.id, name: p.name, price: p.price, currency: "EUR",
6 userId: getOrCreateAnonId(),
7});
8
9// Add to cart — a named event; build the funnel from it with no extra code.
10track("add_to_cart", {
11 productId: p.id, name: p.name, price: p.price, currency: "EUR", quantity: 1,
12 userId: getOrCreateAnonId(),
13});
14
15// Revenue is NOT tracked from the browser: connect your payment provider
16// (Website settings → Payments) and payments arrive from its webhook, already
17// matched to this visitor. See /documentation/revenue-attribution.5. Run an A/B experiment
Read an A/B test flag where the change renders. Reading it counts the visitor; the experiment compares the variants on the results you pick in the dashboard.
1// Any component — the flag splits visitors, Flowgrid counts them on read
2import { useFlagVariant } from "flowgrid-sdk/react";
3
4export function HeroCta() {
5 const variant = useFlagVariant("hero-cta"); // null while off or outside the test
6 return <button>{variant === "trial" ? "Start Free Trial" : "Book a Demo"}</button>;
7}
8
9// Create the experiment in Product Analytics → Experiments and pick what
10// decides it (a signup, a form, revenue). No conversion call needed.Verify installation
After adding the script do the following to verify that the installation was successful:
- Open your website that you are now tracking in the browser.
- Go to your dashboard, click on your website and click the
go to website's dashboardto see if the incoming traffic are being tracked.