Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Chọn nền tảng của bạn:
Measurement Protocol của Google Analytics không trả về mã lỗi HTTP, ngay cả khi một sự kiện bị sai định dạng hoặc thiếu các tham số bắt buộc. Để đảm bảo các sự kiện của bạn hợp lệ, bạn nên thử nghiệm sự kiện với máy chủ xác thực Measurement Protocol trước khi đưa vào sử dụng chính thức.
Sau khi xác thực rằng các sự kiện của bạn được cấu trúc đúng cách, bạn nên xác minh việc triển khai để đảm bảo bạn đang sử dụng đúng khoá.
Bạn có thể gọi trực tiếp máy chủ xác thực hoặc sử dụng Trình tạo sự kiện Google Analytics. Trình tạo sự kiện của Google Analytics cho phép bạn tương tác để tạo sự kiện và sử dụng máy chủ xác thực Measurement Protocol để xác thực các sự kiện đó.
Hướng dẫn này mô tả cách gửi sự kiện đến Measurement Protocol cho máy chủ xác thực Google Analytics 4 và diễn giải phản hồi.
Gửi sự kiện để xác thực
Điểm khác biệt duy nhất trong yêu cầu đối với các sự kiện được gửi đến Measurement Protocol và máy chủ xác thực Measurement Protocol là URL.
Máy chủ
URL
Measurement Protocol
/mp/collect
Máy chủ xác thực Measurement Protocol
/debug/mp/collect
Tất cả các trường yêu cầu khác đều giống nhau. Hãy xem tài liệu tham khảo về giao thức để biết đầy đủ thông tin chi tiết về cách tạo một yêu cầu hợp lệ.
Đoạn mã sau đây cho thấy một sự kiện không hợp lệ đang được gửi đến máy chủ xác thực Measurement Protocol:
Phản hồi xác thực
Sau đây là phản hồi của máy chủ xác thực đối với sự kiện trước đó:
{"validationMessages":[{"fieldPath":"events","description":"Event at index: [0] has invalid name [_badEventName]. Names must start with an alphabetic character.","validationCode":"NAME_INVALID"}]}
Sau đây là phản hồi của máy chủ xác thực đối với một yêu cầu không có vấn đề xác thực:
[null,null,["Cập nhật lần gần đây nhất: 2025-08-26 UTC."],[[["\u003cp\u003eThe Measurement Protocol Validation Server helps ensure your Google Analytics 4 events are correctly formatted before deploying to production.\u003c/p\u003e\n"],["\u003cp\u003eThe Validation Server uses a different URL endpoint (\u003ccode\u003e/debug/mp/collect\u003c/code\u003e) than the standard Measurement Protocol (\u003ccode\u003e/mp/collect\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eValidation responses provide detailed error messages including the field path, description, and a validation code for easier debugging.\u003c/p\u003e\n"],["\u003cp\u003eEvents sent to the Validation Server are not processed and will not appear in your Google Analytics reports.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Analytics Event Builder offers an interactive way to construct and validate events before sending them to the server.\u003c/p\u003e\n"]]],["To ensure valid events, use the Measurement Protocol Validation Server by sending requests to `/debug/mp/collect` instead of `/mp/collect`. Validate events before deploying them. This server doesn't log events in reports. It returns a response with `validationMessages`, an array detailing any errors like `NAME_INVALID`, `VALUE_REQUIRED`, or `VALUE_OUT_OF_BOUNDS`, specifying the `fieldPath`, `description`, and `validationCode` of the issue. Use the Google Analytics Event Builder for interactive event construction.\n"],null,["Choose your platform: \nFirebase gtag.js\n\nThe Google Analytics Measurement Protocol does not return\n`HTTP` error codes, even if an event is malformed or missing required\nparameters. To ensure your events are valid, you should test them against the\nMeasurement Protocol validation server before deploying them to production.\nAfter you have validated that your events are structured properly, you should\n[verify your implementation](/analytics/devguides/collection/protocol/ga4/verify-implementation) to make sure you're using the correct keys.\n| **Caution:** The validation server does *not* validate the `api_secret` or Carefully review those values to make sure they are correct.\n\nYou can either call the validation server directly, or use the [Google Analytics\nEvent Builder](https://ga-dev-tools.web.app/ga4/event-builder/). The Google Analytics Event Builder lets you interactively\nconstruct events, and uses the Measurement Protocol validation server to\nvalidate them.\n\nThis guide describes how to send events to the Measurement Protocol for Google\nAnalytics 4 validation server and interpret the response.\n| **Important:** Events sent to the validation server don't show up in reports.\n\nSend events for validation\n\nThe only difference in the request for events sent to the Measurement Protocol\nand the Measurement Protocol validation server is the URL.\n\n| Server | URL |\n|----------------------------------------|-------------------------|\n| Measurement Protocol | `/mp/collect` |\n| Measurement Protocol validation server | `/`*debug*`/mp/collect` |\n\nAll other request fields are the same. See the [protocol](/analytics/devguides/collection/protocol/ga4/reference) reference for full\ndetails on constructing a valid request.\n\nThe following code shows an invalid event being sent to the Measurement Protocol\nvalidation server:\n| **Tip:** If you want your data to be collected in the EU, change the URL passed to the `fetch` method to begin with `https://region1.google-analytics.com` instead of `https://www.google-analytics.com`.\n\nValidation response\n\nHere's the validation server's response to the previous event: \n\n {\n \"validationMessages\": [\n {\n \"fieldPath\": \"events\",\n \"description\": \"Event at index: [0] has invalid name [_badEventName]. Names must start with an alphabetic character.\",\n \"validationCode\": \"NAME_INVALID\"\n }\n ]\n }\n\nHere's the validation server's response to a request with no validation issues: \n\n {\n \"validationMessages\": []\n }\n\nResponse\n\n| Key | Type | Description |\n|----------------------|---------------------------------------------------|----------------------------------|\n| `validationMessages` | Array\\\u003c[ValidationMessage](#validation_message)\\\u003e | An array of validation messages. |\n\nValidationMessage\n\n| Key | Type | Description |\n|------------------|------------------------------------|--------------------------------------------------|\n| `fieldPath` | string | The path to the field that was invalid. |\n| `description` | string | A description of the error. |\n| `validationCode` | [ValidationCode](#validation_code) | A validation code that corresponds to the error. |\n\nValidationCode\n\n| Value | Description |\n|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| `VALUE_INVALID` | The value provided for a `fieldPath` was invalid. See [limitations](/analytics/devguides/collection/protocol/ga4/sending-events#limitations). |\n| `VALUE_REQUIRED` | A required value for a `fieldPath` was not provided. |\n| `NAME_INVALID` | The name provided was invalid. See [limitations](/analytics/devguides/collection/protocol/ga4/sending-events#limitations). |\n| `NAME_RESERVED` | The name provided was one of the reserved names. See [reserved names](/analytics/devguides/collection/protocol/ga4/reference#reserved_names). |\n| `VALUE_OUT_OF_BOUNDS` | The value provided was too large. See [limitations](/analytics/devguides/collection/protocol/ga4/sending-events#limitations). |\n| `EXCEEDED_MAX_ENTITIES` | There were too many parameters in the request. See [limitations](/analytics/devguides/collection/protocol/ga4/sending-events#limitations). |\n| `NAME_DUPLICATED` | The same name was provided more than once in the request. |"]]