React consent tracking: Google Analytics and Meta
Quick answer
Ask visitors for a clear choice, store the preference, and route every optional Google Analytics or Meta event through one consent guard so nothing fires before they accept. Give each event a single meaning: an outbound click shows interest, not a sale. Then test that declining keeps every optional tracker off.
A click on a membership button shows interest. It does not prove someone paid. This guide follows the optional tracking used on this website: a clear choice, a consent guard, and separate event names for outbound clicks and completed actions.
For an example of auditing changes before measuring their effect, see the Codex internal-linking workflow and build notes. That video covers link structure; this guide supplies the separate consent-aware measurement step.

Before you start
This guide describes how this site’s consent and tracking code works, and the snippets show the pattern to adapt in your own project. The site’s source repository is private, so there is nothing to clone. You’ll need Node.js, npm, and basic JavaScript. Automated checks that use mocked providers do not send analytics or require a paid service.
Use your own provider IDs while you experiment. An analytics ID is not a secret, but sending your tests to somebody else’s property pollutes their reports.
Name the choice accurately
Store the choice before tracking
When a choice enables both Google Analytics and Meta Pixel, say “Analytics & marketing” and identify both providers. Keep Decline and Allow accessible. A separate provider that runs regardless of this preference must be described separately in the privacy notice.
Store the choice, provide a way to reopen it, and treat missing or unreadable storage as no permission for optional tracking. Avoid loading the optional scripts simply because the page mounted.
Put the guard where every event uses it
export function trackGoogleEvent(name, parameters = {}) {
if (typeof window === 'undefined'
|| getTrackingConsent() !== 'granted'
|| typeof window.gtag !== 'function') return false;
window.gtag('event', name, parameters);
return true;
}In this project, src/tracking.js owns this check. The loading function also checks consent. Repeated clicks on Allow reuse the initialized providers rather than inserting duplicate scripts.
When the choice changes to Decline, revoke consent through the provider APIs and stop sending optional application events. This does not mean a provider already loaded in the document ceases all network activity; inspect the provider’s behavior and explain the policy accurately.
Give each event one meaning
| Event | What it establishes |
|---|---|
| CTA Clicked | The visitor activated a link or action. |
| Skool Outbound Clicked | The visitor followed a membership link. |
| Call Requested | The embedded scheduler received the request. This calendar requires manual confirmation. |
| Confirmed paid membership | A trusted payment or membership record confirms payment; a click cannot supply this evidence. |
Record placement and campaign details rather than names, email addresses, or booking notes. Forward the same UTM campaign parameters to the destination so its records can be reconciled with your traffic.
Test the choices
- In a fresh local browser session, inspect network requests before making a choice. Google and Meta tracking scripts should be absent.
- Choose Decline and follow a few links. Optional Google/Meta events should remain off.
- With providers mocked or routed to your own test property, choose Allow both. Confirm one script per provider.
- Reopen Privacy choices, decline, then allow again. Confirm consent updates and that scripts are not duplicated.
- Inspect your data definitions: outbound clicks and completed bookings must remain separate counts.
The repository’s tracking tests cover loading once, restoring consent, and guarding outbound events. Browser checks are still needed because provider scripts run outside those unit tests.
When a check fails
If an optional event fires before consent, route it through the shared guard in the file that owns the check and rerun your consent tests. If scripts appear twice, check the initialization guard before adding another event listener. Test with mocked providers so you can inspect calls without sending real analytics.
Reconcile before optimizing
For a fixed date range, compare campaign clicks with actual scheduler and membership records. Deduplicate using provider transaction or booking IDs, exclude canceled/refunded outcomes according to a documented rule, and leave missing attribution labeled unknown. Never guess a match from timing alone.
Only then compare page versions. A shorter hero might increase clicks while producing the same number of paying members; the two observations answer different questions.
Choose another build or ask a question.
To make this change with a coding assistant, the Codex tutorial shows how to scope and verify one edit, and the Claude Code tutorial for beginners covers setup. The same check-first approach applies to API calls in make your first AI API request with a local test.
FAQ
What does an outbound click to a membership page prove?
Only that the visitor followed the link. It shows interest, not a sale. Payment needs a trusted payment or membership record.
Where should the consent check live?
In one shared guard that every optional Google Analytics or Meta event goes through. In this project src/tracking.js owns that check, and the loading function checks consent too.
What if the stored preference is missing or unreadable?
Treat it as no permission for optional tracking, and do not load the optional scripts just because the page mounted.
How do I test that declining works?
In a fresh browser session, confirm the Google and Meta scripts are absent before a choice, choose Decline, follow a few links and check that no optional events fire. Then reopen Privacy choices and confirm consent updates without duplicate scripts.
Sources
- This guide describes this site’s own implementation. Its source repository is private.
You may also like
Build the next one with AI Income Lab
Step-by-step courses, reusable templates, and a community that answers when you get stuck.
- Courses for builds like this one
- Copy-ready templates
- Ask questions while you build
- Weekly coaching on VIP



