將 Measurement Protocol 事件傳送至 Google Analytics (分析)

本指南將說明傳送方式 Google Analytics Measurement Protocol 網站和應用程式串流 將「事件」套用至 Google Analytics 伺服器,方便你在 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 UI 中產生的 API SECRET

    如要建立新的密鑰,請依序前往「管理」>「資料串流」>選擇 你的直播 >Measurement Protocol >建立

  • firebase_app_id:Firebase 應用程式 ID,可在 Firebase 控制台前往 專案設定 >一般 >您的應用程式 >應用程式 ID

    firebase_app_idapp_instance_id 不同。 firebase_app_id 可用來識別您的應用程式,app_instance_id 則會識別 安裝應用程式的次數

如需完整參考資料,請參閱查詢參數

您必須在要求主體中提供以下內容:

  • user_id:選用。使用者的專屬 ID。只能包含 UTF-8 字元。詳情請參閱「使用 User-ID 進行跨平台分析」一文 關於這個識別碼

  • consent:選用。瞭解如何設定同意聲明 設定

  • timestamp_micros:選用。Unix 紀元時間 (以微秒為單位), 以及事件和使用者屬性如未指定,則預設為 要求時間

  • events:事件項目的陣列。您可在單一活動中加入多個活動 請求。

    如要在類似報表內顯示使用者活動, 即時engagement_time_msecsession_id 必須做為 eventparamsengagement_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 don't populate
    Search audiences in Google Ads for app users.

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