GA4 Measurement Protocol
Send events to Google Analytics 4 from any JavaScript environment — servers, workers, IoT devices, or anywhere the browser isn't available. A lightweight, dependency-free library built for production.
Server-side GA4, the right way
Measurement Protocol
Full implementation of the GA4 Measurement Protocol with automatic session and client management.
Lightweight
Zero dependencies. Works in Node.js, Bun, Deno, Cloudflare Workers, and any JavaScript runtime.
Session Management
Built-in session handling with automatic session IDs, session counts, and engagement tracking.
Event Validation
Debug mode with validation endpoint support to catch issues before they hit production.
Up and running in minutes
Send your first event
import { Ga4mp } from "ga4mp";
const ga4mp = Ga4mp.createInstance({
measurement_id: "G-XXXXXXXXXX",
api_secret: "your_api_secret",
});
// Set client and session
ga4mp.setClientId("my-client-id");
ga4mp.setSessionId("my-session-id");
// Send an event
const event = ga4mp.createEvent("purchase", {
currency: "USD",
value: 49.99,
transaction_id: "T-12345",
items: [{
item_id: "SKU-001",
item_name: "Premium Plan",
price: 49.99,
quantity: 1,
}],
});
await event.send();Validate before production
// Use debug mode to validate events
// before sending to production
const ga4mp = Ga4mp.createInstance({
measurement_id: "G-XXXXXXXXXX",
api_secret: "your_api_secret",
debug: true, // Uses validation endpoint
});
const event = ga4mp.createEvent("purchase", {
currency: "USD",
value: 49.99,
});
const result = await event.send();
console.log(result.validationMessages);When to use GA4MP
The Measurement Protocol shines when browser-based tracking isn't an option.
Server-side event tracking from your backend API
Offline conversion imports from CRM or POS systems
IoT and embedded device analytics
Cloudflare Workers, Lambda, or edge function tracking
Let's Talk
Whether you need an implementation from scratch, an audit of your current setup, or expert advice — we're here to help.