GA4 Measurement Protocol
Server-side events to Google Analytics 4 through the Measurement Protocol. Use it for events that happen on a server (a sign-up confirmed by your app, a payment webhook) or that should not depend on the browser; the gtag web destination stays the way to send page views and browser events.
GA4 Measurement Protocol is a server destination in the walkerOS flow:
Sends one event per request to `/mp/collect` with the measurement id and API secret in the query string, using raw HTTP (no SDK). Nothing personal is sent unless you map it: no email, IP or user agent.
Installation
npm install @walkeros/collector @walkeros/server-destination-ga4- Integrated
- Bundled
import { startFlow } from '@walkeros/collector';
import destinationGa4 from '@walkeros/server-destination-ga4';
await startFlow({
destinations: {
ga4: {
code: destinationGa4,
config: {
settings: {
measurementId: 'G-TEST123',
apiSecret: process.env.GA4_API_SECRET,
},
},
},
},
});
A complete flow.json with this destination:
{
"version": 4,
"flows": {
"default": {
"config": {
"platform": "server"
},
"destinations": {
"ga4": {
"package": "@walkeros/server-destination-ga4",
"config": {
"settings": {
"measurementId": "G-TEST123",
"apiSecret": "$secret.GA4_API_SECRET"
}
}
}
}
}
}
}Configuration
This destination uses the standard destination config wrapper (consent, data, env, id, ...). For the shared fields see destination configuration. Package-specific fields live under config.settings and are listed below.
Settings
| Property | Type | Description | More |
|---|---|---|---|
measurementId | string | GA4 measurement id of the web data stream | |
apiSecret | string | Measurement Protocol API secret: GA4 Admin > Data streams > the stream > Measurement Protocol API secrets | |
url | string | Endpoint without query string. EU collection: https://region1.google-analytics.com/mp/collect; testing: https://www.google-analytics.com/debug/mp/collect |
Mapping
This package does not define custom rule-level settings. For the standard rule fields (consent, condition, data, batch, name, policy) see mapping.
Examples
Lead with user id
A demo request is sent as generate_lead with the visitor GA4 client id and the signed-in user id.
{
"name": "demo request",
"data": {
"location": "hero"
},
"context": {
"dev": [
"test",
1
]
},
"globals": {
"ga_client_id": "1234567890.1699990000"
},
"custom": {
"completely": "random"
},
"user": {
"id": "user-42"
},
"nested": [
{
"entity": "child",
"data": {
"is": "subordinated"
}
}
],
"consent": {
"functional": true
},
"id": "ev-1700000901",
"trigger": "test",
"entity": "demo",
"action": "request",
"timestamp": 1700000901000,
"timing": 3.14,
"source": {
"count": 1,
"trace": "0a1b2c3d4e5f60718293a4b5c6d7e8f9",
"type": "collector",
"schema": "4"
}
}{
"name": "generate_lead",
"data": {
"map": {
"client_id": "globals.ga_client_id",
"user_id": "user.id",
"params": {
"map": {
"location": "data.location"
}
}
}
}
}sendServer("https://www.google-analytics.com/mp/collect?measurement_id=G-TEST123&api_secret=s3cr3t", "{\"client_id\":\"1234567890.1699990000\",\"user_id\":\"user-42\",\"consent\":{\"ad_user_data\":\"DENIED\",\"ad_personalization\":\"DENIED\"},\"events\":[{\"name\":\"generate_lead\",\"params\":{\"location\":\"hero\",\"engagement_time_msec\":1}}]}", {
"headers": {
"Content-Type": "application/json"
}
})Purchase
A completed order joins the visitor GA4 session as a purchase with value, currency and transaction id; marketing consent grants the ad signals.
{
"name": "order complete",
"data": {
"id": "ORD-300",
"total": 249.99,
"currency": "EUR"
},
"context": {
"shopping": [
"complete",
0
]
},
"globals": {
"ga_client_id": "1234567890.1699990000",
"ga_session_id": "1699999000"
},
"custom": {
"completely": "random"
},
"user": {
"id": "us3r",
"device": "c00k13",
"session": "s3ss10n"
},
"nested": [
{
"entity": "product",
"data": {
"id": "ers",
"name": "Everyday Ruck Snack",
"color": "black",
"size": "l",
"price": 420
},
"context": {
"shopping": [
"complete",
0
]
},
"nested": []
},
{
"entity": "product",
"data": {
"id": "cc",
"name": "Cool Cap",
"size": "one size",
"price": 42
},
"context": {
"shopping": [
"complete",
0
]
},
"nested": []
},
{
"entity": "gift",
"data": {
"name": "Surprise"
},
"context": {
"shopping": [
"complete",
0
]
},
"nested": []
}
],
"consent": {
"marketing": true
},
"id": "ev-1700000900",
"trigger": "load",
"entity": "order",
"action": "complete",
"timestamp": 1700000900000,
"timing": 3.14,
"source": {
"count": 1,
"trace": "0a1b2c3d4e5f60718293a4b5c6d7e8f9",
"type": "collector",
"schema": "4"
}
}{
"name": "purchase",
"data": {
"map": {
"client_id": "globals.ga_client_id",
"session_id": "globals.ga_session_id",
"params": {
"map": {
"value": "data.total",
"currency": "data.currency",
"transaction_id": "data.id"
}
}
}
}
}sendServer("https://www.google-analytics.com/mp/collect?measurement_id=G-TEST123&api_secret=s3cr3t", "{\"client_id\":\"1234567890.1699990000\",\"consent\":{\"ad_user_data\":\"GRANTED\",\"ad_personalization\":\"GRANTED\"},\"events\":[{\"name\":\"purchase\",\"params\":{\"value\":249.99,\"currency\":\"EUR\",\"transaction_id\":\"ORD-300\",\"session_id\":\"1699999000\",\"engagement_time_msec\":1}}]}", {
"headers": {
"Content-Type": "application/json"
}
})Event without GA4 client id
Without a _ga client id, a stable one is derived from the event id so the event still counts (as a new user) and a retry keeps the same id.
{
"name": "user register",
"data": {
"method": "email"
},
"context": {
"dev": [
"test",
1
]
},
"globals": {
"lang": "elb"
},
"custom": {
"completely": "random"
},
"user": {
"id": "us3r",
"device": "c00k13",
"session": "s3ss10n"
},
"nested": [
{
"entity": "child",
"data": {
"is": "subordinated"
}
}
],
"consent": {
"functional": true
},
"id": "ev-1700000902",
"trigger": "test",
"entity": "user",
"action": "register",
"timestamp": 1700000902000,
"timing": 3.14,
"source": {
"count": 1,
"trace": "0a1b2c3d4e5f60718293a4b5c6d7e8f9",
"type": "collector",
"schema": "4"
}
}{
"name": "sign_up",
"data": {
"map": {
"params": {
"map": {
"method": "data.method"
}
}
}
}
}sendServer("https://www.google-analytics.com/mp/collect?measurement_id=G-TEST123&api_secret=s3cr3t", "{\"client_id\":\"735042307.1700000902\",\"consent\":{\"ad_user_data\":\"DENIED\",\"ad_personalization\":\"DENIED\"},\"events\":[{\"name\":\"sign_up\",\"params\":{\"method\":\"email\",\"engagement_time_msec\":1}}]}", {
"headers": {
"Content-Type": "application/json"
}
})Joining the visitor's session
The Measurement Protocol attributes an event to a visitor and a session only through ids you send. Read them in the browser and carry them to the server event (for example as globals):
Mapped data | Sent as | Where it comes from |
|---|---|---|
client_id | client_id | The _ga cookie GA1.1.<random>.<seconds>: send <random>.<seconds> |
session_id | events[0].params.session_id | The _ga_<stream> cookie (digits) |
user_id | user_id | Your own user id (optional) |
params | events[0].params | Event parameters, such as value (sent as a number), currency, transaction_id |
With a valid client_id and session_id the event joins the visitor's session and its traffic source. Without a valid client_id, the destination derives a stable one from the walkerOS event id: the event still counts, as a new user, and a retry keeps the same id. engagement_time_msec: 1 is added so the event counts as engaged.
"mapping": {
"order": { "complete": {
"name": "purchase",
"data": { "map": {
"client_id": "globals.ga_client_id",
"session_id": "globals.ga_session_id",
"params": { "map": {
"value": "data.total",
"currency": "data.currency",
"transaction_id": "data.id"
} }
} }
} }
}Event name
The mapped name, with _ for spaces (order complete becomes order_complete). GA4 accepts letters, digits and _, starting with a letter, up to 40 characters: an invalid name fails with the code mapping and nothing is sent. Use GA4's recommended names (purchase, sign_up, generate_lead) for reports and key events to pick them up.
Consent
consent.ad_user_data and consent.ad_personalization are GRANTED only when the event carries marketing consent, else DENIED. Gate the destination itself on measurement consent: add consent: { analytics: true } to config, next to settings (see Gate a destination with consent).
Timestamps
timestamp_micros is the event time when the event is less than 71 hours old; GA4 refuses older ones, so an older event (a late retry) is sent without it and GA4 uses the reception time.
Testing
GA4 answers 2xx even to an invalid payload. While setting up, point url to the validation server, which answers with the problems it finds and records nothing:
"settings": {
"measurementId": "G-XXXXXXXXXX",
"apiSecret": "$secret.GA4_API_SECRET",
"url": "https://www.google-analytics.com/debug/mp/collect"
}For collection in the EU, use https://region1.google-analytics.com/mp/collect.
Failures
- A response outside the 2xx range, a network error or no answer within 5 seconds fails with
transport(with the HTTP status) ortimeout, and the event goes to the dead-letter queue. 429 and 5xx are marked retryable, other 4xx not; nothing resends the event by itself. - Without
measurementIdorapiSecret, or with a measurement id that is notG-..., the destination does not start: every push answersfailedwith the codeconfig. - A rule with
silent: truesends nothing.