Sub topic 15
Debugging Measurement Protocol events
The Measurement Protocol (MP) lets a server, kiosk, or backend job send events to GA4 over HTTP. This chapter assumes you know what a GA4 event, parameter, and data stream are. It also assumes you have used DebugView and the Realtime report for web or app traffic, and that you have a measurement ID and an API secret for a stream.
MP is harder to debug than gtag.js or an SDK. No browser console shows the request, and the collection endpoint accepts almost any request without complaint. This chapter covers the validation endpoint, how to send MP events to DebugView, and the errors that make valid-looking requests disappear.
Why MP needs its own debugging approach
The production endpoint is https://www.google-analytics.com/mp/collect. It answers 204 No Content for nearly every request, including payloads GA4 later drops. A 204 means the request was received. It does not mean the events were accepted.
Two other tools fill the gap:
- The validation endpoint tells you whether the payload is well formed.
- The
debug_modeparameter makes a real event show up in DebugView.
They answer different questions, and you usually need both.
| Tool | Records the event? | Answers |
|---|---|---|
| `/mp/collect` | Yes | Nothing useful. It returns 204 regardless. |
| `/debug/mp/collect` | No | Is the payload structurally valid? |
| `debug_mode` on a real event | Yes | Did GA4 process it, and with what parameters? |
| Realtime report | Yes | Did it arrive in the last 30 minutes? |
Validating a payload
Send the same JSON body to /debug/mp/collect instead of /mp/collect. The endpoint returns a JSON body with a validationMessages array. It does not record the event, so you can run it as often as you like without touching your data.
curl -X POST "https://www.google-analytics.com/debug/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=YOUR_API_SECRET" \
-H "Content-Type: application/json" \
-d '{
"client_id": "test.1234567890",
"events": [{
"name": "sign_up",
"params": { "method": "email" }
}]
}'A valid payload returns an empty array:
{
"validationMessages": []
}An invalid payload returns one entry per problem. Each entry has a fieldPath, a human-readable description, and a validationCode:
{
"validationMessages": [
{
"fieldPath": "events",
"description": "Event at index: [0] has invalid name [firebase_event]. Names cannot begin with reserved prefix 'firebase_'.",
"validationCode": "NAME_RESERVED"
}
]
}Common codes and their usual causes:
| validationCode | Typical cause |
|---|---|
| `VALUE_REQUIRED` | Missing `client_id` (or `app_instance_id` for app streams), or missing `name` on an event. |
| `NAME_INVALID` | Event or parameter name with spaces, hyphens, or other disallowed characters. |
| `NAME_RESERVED` | Name uses a reserved prefix such as `firebase_`, `ga_`, or `google_`. |
| `VALUE_INVALID` | A field has the wrong type or format, for example a string where a number is expected. |
| `VALUE_OUT_OF_BOUNDS` | A value exceeds a limit, for example `timestamp_micros` outside the allowed range. |
| `EXCEEDED_MAX_ENTITIES` | Too many events in one request (the limit is 25) or too many parameters on one event. |
Treat an empty validationMessages array as "the shape is right". It does not prove the data will be reported correctly. The validation server checks structure. It does not confirm that your api_secret belongs to the stream, that the client_id matches a real browser, or that a filter will not exclude the event later.
Sending MP events to DebugView
The validation endpoint never records anything, so DebugView stays empty however many times you call it. To see an MP event in DebugView, send it to the real /mp/collect endpoint with debug_mode set in the event's params:
curl -X POST "https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=YOUR_API_SECRET" \
-H "Content-Type: application/json" \
-d '{
"client_id": "test.1234567890",
"events": [{
"name": "sign_up",
"params": {
"method": "email",
"debug_mode": 1,
"engagement_time_msec": 100
}
}]
}'Points to know:
debug_modegoes inside each event'sparams, not at the top level of the body. Each event you want to see needs its own flag.- The device in DebugView's device selector is identified by the
client_idyou sent. Use a recognizable test value so you can pick it out. - This event is real. It is stored and, unless you filter it (see the developer traffic chapter), it counts in reports. Debug traffic is not excluded from reports by the flag alone. The Developer traffic filter handles that.
engagement_time_msecis not needed for DebugView to show the event. It is needed for the event to contribute to engagement metrics and for the user to count as active in Realtime.
If you want the event to join an existing website session, send the same client_id that gtag.js uses (the value in the _ga cookie) and the current session_id as an event parameter. Without them, MP events create their own user and session and look like separate traffic.
A repeatable debugging loop
The most reliable approach is to run the validation first and the real send second, from the same payload. This script does both:
import json
import urllib.request
MEASUREMENT_ID = "G-XXXXXXXXXX"
API_SECRET = "YOUR_API_SECRET"
BASE = "https://www.google-analytics.com"
payload = {
"client_id": "test.1234567890",
"events": [
{
"name": "sign_up",
"params": {
"method": "email",
"debug_mode": 1,
"engagement_time_msec": 100,
},
}
],
}
def post(path):
url = f"{BASE}{path}?measurement_id={MEASUREMENT_ID}&api_secret={API_SECRET}"
request = urllib.request.Request(
url,
data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(request) as response:
return response.status, response.read().decode("utf-8")
status, body = post("/debug/mp/collect")
messages = json.loads(body).get("validationMessages", [])
if messages:
print("Payload rejected by the validation server:")
for message in messages:
print(f"- {message['validationCode']} at {message['fieldPath']}: {message['description']}")
else:
status, _ = post("/mp/collect")
print(f"Validated, then sent. Production endpoint returned {status}.")
print("Now open DebugView and select the device 'test.1234567890'.")When the event validates but does not appear
If validation is clean and the event is still missing from DebugView, check these causes in order:
- Wrong measurement ID or API secret. The validation server does not confirm these. The real endpoint accepts a request with a wrong secret and silently discards it. Copy both values from the stream's details page in Admin.
- Missing
debug_modeon that event. The flag is per event. It also has to be insideparams. - Wrong device selected. DebugView lists devices by
client_id. Pick the one you sent. If you reused aclient_idthat real browser traffic also uses, your MP event is mixed in with that device's stream. - Mismatched stream type. Web streams use
client_id. App streams useapp_instance_idand needfirebase_app_idin the query string instead ofmeasurement_id. - Bad timestamp. If you set
timestamp_micros, it must be in microseconds and within the allowed past window (72 hours). A value in milliseconds or seconds lands in the wrong year and the event is dropped. - Delay. DebugView usually updates within seconds, but allow a minute before concluding the event is lost.
Reading MP events in DebugView and Realtime
In DebugView, MP events look like any other event in the minute and second timelines. Open an event to inspect its parameters. Check that every parameter you sent is present and has the type you intended. A numeric value sent as a string is the most common surprise. It appears as text and cannot be summed in reports.
For Realtime, remember that MP events appear in the cards only if they are recent. They are attributed to a user, and the user counts as active only when the event carries engagement_time_msec. A server-side event without it can show in the event count card while the "users in last 30 minutes" card stays at zero.
Exercise
You send a purchase event to /debug/mp/collect and get {"validationMessages": []}. DebugView shows nothing for your test client_id. Give two separate reasons this can happen and the fix for each.
Show answer
First, the validation endpoint never records events, so nothing can appear in DebugView from it. Fix: send the same payload to /mp/collect. Second, the real event lacks debugmode inside its params, so GA4 does not route it to DebugView. Fix: add "debugmode": 1 to that event's params. Other valid answers: the measurement ID or API secret is wrong (the validation server does not check them), or a different device is selected in the device selector.
Exercise
A server-side event appears in the Realtime event count card, but the "users in last 30 minutes" card shows zero. What is the likely cause?
Show answer
The event was sent without engagementtimemsec, so the user is not counted as active. Add engagementtimemsec with a positive value to the event's params.