Google Analytics 4 (GA4) Provider
The GA4 adapter in Trackkit provides a lightweight client for GA4 without requiring the gtag.js snippet. It handles queueing, consent gating, SPA navigation, and stable event dispatch.
Trackkit does not load GA4’s external script unless explicitly configured through the provider’s advanced options.
Trackkit’s GA4 adapter is Measurement-Protocol–only. No GA script is loaded unless you explicitly load it yourself.
Features
- Automatic SPA pageviews (
autoTrack: trueby default) - Custom events via
analytics.track() - Optional identification (
analytics.identify()) - Consent-aware queueing and replay
- Domain allowlist (
domains) and path exclusion (exclude) - Debug logging for lifecycle and send decisions
Configuration
Minimal
import { createAnalytics } from 'trackkit';
const analytics = createAnalytics({
provider: 'ga4',
site: 'G-XXXXXXXXXX',
});Common options
const analytics = createAnalytics({
provider: 'ga4',
site: 'G-XXXXXXXXXX',
// measurementId: 'G-XXXXXXXXXX', // Provider-specific alternative to 'site'
autoTrack: true, // SPA pageviews
doNotTrack: true, // respect DNT
includeHash: false, // omit #fragment
domains: ['example.com'], // optional allowlist
exclude: ['/admin', '/preview'], // skip certain paths
trackLocalhost: true, // enabled by default
defaultProps: { appVersion: '2.3.1' }, // merged into GA4 params
debug: false,
});Identifier: You may provide either the GA4-specific
measurementIdfield or the genericsitefield.
Environment Variables
TRACKKIT_PROVIDER=ga4
TRACKKIT_SITE=G-XXXXXXXXXX
TRACKKIT_DEBUG=falseOr via Vite-style:
VITE_TRACKKIT_PROVIDER=ga4
VITE_TRACKKIT_SITE=G-XXXXXXXXXXAPI Usage
Examples below use the singleton helpers (
track,pageview,identify,…) for compactness. To use instances instead, call the same methods on youranalyticsobject.
Pageviews
import { pageview } from 'trackkit';
history.pushState({}, '', '/thank-you');
pageview(); // current URLAutomatic when autoTrack: true.
Custom Events
import { track } from 'trackkit';
track('purchase', {
value: 29.99,
currency: 'USD',
items: [
{ item_id: 'SKU123', item_name: 'T-Shirt', price: 29.99, quantity: 1 }
]
});Identify
import { identify } from 'trackkit';
identify('user-123'); // sets GA4 user_idConsent
import { grantConsent, denyConsent } from 'trackkit';
denyConsent(); // optional initial state
grantConsent(); // queued events flushCSP
Because Trackkit sends GA4 hits directly (Measurement Protocol), you typically need only:
connect-src https://www.google-analytics.com https://region1.google-analytics.com;No script tag required unless using an advanced GA4 configuration.
Notes & Limitations
identify()sets only the GA4user_id; it does not touch GA cookies.- GA4 may still set cookies if your property is configured to do so. These cookies are set by GA4 server infrastructure based on property settings, not by Trackkit.
- Domain/exclusion filters are string-based (no wildcards).
- For server-side Measurement Protocol, set
apiSecretin provider options.
Debugging
const analytics = createAnalytics({
provider: 'ga4',
site: 'G-XXXXXXXXXX',
debug: true,
});You’ll see:
- Provider readiness
- Queue adds/replays
- Navigation triggers
- URL normalization decisions
Best Practices
- Ensure a compliant consent flow.
- Use
defaultPropsfor application metadata. - Keep GA4 event names aligned with recommended GA4 semantics.
- Consider using a proxy if you want full first-party deployment.