向 Google Analytics(分析)发送 Measurement Protocol 事件

本指南将介绍如何 Google Analytics Measurement Protocol 网站和应用数据流 事件 Google Analytics 服务器,以便您可以在以下位置查看 Measurement Protocol 事件: Google Analytics 报告

请选择您想在本指南中查看哪个平台的信息:

设置请求格式

Google Analytics Measurement Protocol 仅支持 HTTP POST 请求。

如要发送事件,请使用以下格式:

POST /mp/collect HTTP/1.1
HOST: www.google-analytics.com
Content-Type: application/json
<payload_data>

必须在请求网址中提供以下内容:

  • api_secret:Google Analytics 界面中生成的 API 密钥

    如需创建新的密钥,请依次点击管理 > 数据流 > 选择您的数据流 > Measurement Protocol > 创建

  • firebase_app_id:Firebase 应用 ID,可在 Firebase 控制台的以下位置下找到:项目设置 > 常规 > 您的应用 > 应用 ID

    firebase_app_id 不同于 app_instance_idfirebase_app_id 用于标识您的应用,而 app_instance_id 用于标识应用的单次安装。

如需获取完整的参考信息,请参阅查询参数

必须在请求正文中提供以下内容:

  • user_id:可选。用户的唯一标识符。只能包含 UTF-8 字符。如需详细了解此标识符,请参阅使用 User-ID 进行跨平台分析

  • consent:可选。了解如何设置用户意见征求设置

  • timestamp_micros:可选。Unix 纪元时间(以微秒为单位), 事件和用户属性。如果未指定,则默认为 请求的时间点

  • events:一个由多个事件项构成的数组。您可以在一个事件中包含多个事件 请求。

    若要让报告显示用户活动,例如 实时engagement_time_msecsession_id 必须作为 params,表示 eventengagement_time_msec 参数应反映 事件的互动时长(以毫秒为单位)。

    示例如下:

  {
   "app_instance_id": "12345678901234567890123456789012",
   "events": [
     {
        "name": "campaign_details",
        "params": {
          "campaign_id": "google_1234",
          "campaign": "Summer_fun",
          "source": "google",
          "medium": "cpc",
          "term": "summer+travel",
          "content": "logolink",
          "session_id": "123",
          "engagement_time_msec": "100"
        }
     }
   ]
  }
  ```



While `session_start` is a [reserved event
name](/analytics/devguides/collection/protocol/ga4/reference#reserved_names),
creating a new `session_id` creates a new session without the need to send
`session_start`. Understand how [sessions are
counted](//support.google.com/analytics/answer/9191807).

## Try it

Here's an example you can use to send a [`tutorial_begin`] event to your Google
Analytics server:



```javascript
const firebase_app_id = `1:1234567890:android:321abc456def7890`;
const api_secret = `<secret_value>`;

fetch(`https://www.google-analytics.com/mp/collect?firebase_app_id=${firebase_app_id}&api_secret=${api_secret}`, {
  method: "POST",
  body: JSON.stringify({
    app_instance_id: 'app_instance_id',
    events: [{
      name: 'tutorial_begin',
      params: {},
    }]
  })
});
```

The format of `firebase_app_id` is platform specific. See **Application ID**
under [Firebase config files and objects].



## Override timestamp

The Measurement Protocol uses the *first* timestamp it finds in the following
list for each event in the request:

1.  The `timestamp_micros` of the event.
1.  The `timestamp_micros` of the request.
1.  The time that the Measurement Protocol receives the request.

The following example sends a request-level timestamp that applies to all of
the events in the request. As a result, the Measurement Protocol assigns both the
`tutorial_begin` and `join_group` events a timestamp of
`requestUnixEpochTimeInMicros`.

```javascript
{
  "timestamp_micros": requestUnixEpochTimeInMicros,
  "events": [
    {
      "name": "tutorial_begin"
    },
    {
      "name": "join_group",
      "params": {
        "group_id": "G_12345",
      }
    }
  ]
}
```

The following example sends both a request-level timestamp and an event-level
timestamp. As a result, the Measurement Protocol assigns the `tutorial_begin`
event a timestamp of `tutorialBeginUnixEpochTimeInMicros`, and the `join_group`
event a timestamp of `requestUnixEpochTimeInMicros`.

```javascript
{
  "timestamp_micros": requestUnixEpochTimeInMicros,
  "events": [
    {
      "name": "tutorial_begin",
      "timestamp_micros": tutorialBeginUnixEpochTimeInMicros
    },
    {
      "name": "join_group",
      "params": {
        "group_id": "G_12345",
      }
    }
  ]
}
```

## Limitations

The following limitations apply to sending Measurement Protocol events to Google
Analytics:

Note: For information on the limitations of 360 features, see [Google Analytics
360](//support.google.com/analytics/answer/11202874).

*   Requests can have a maximum of 25 events.
*   Events can have a maximum of 25 parameters.
*   Events can have a maximum of 25 user properties.
*   User property names must be 24 characters or fewer.
*   User property values must be 36 characters or fewer.
*   Event names must be 40 characters or fewer, can only contain alpha-numeric
    characters and underscores, and must start with an alphabetic character.
*   Parameter names including item parameters must be 40 characters or fewer,
    can only contain alpha-numeric characters and underscores, and must start
    with an alphabetic character.
*   Parameter values including item parameter values must be 100 characters or
    fewer for a standard Google Analytics property, and 500 characters or fewer
    for a Google Analytics 360 property.
*   Item parameters can have a maximum of 10 custom parameters.
*   The post body must be smaller than 130kB.
*   App Measurement Protocol events sent to Google Analytics 4 don't populate
    Search audiences in Google Ads for app users.

For additional requirements of each use case, see [common use cases].