Measurement Protocol イベントを Google アナリティクスに送信する

このガイドでは、Google Chat のメッセージ履歴を Google アナリティクス Measurement Protocol のウェブおよびアプリ ストリーム イベントを Measurement Protocol イベントを表示するための Google アナリティクス サーバー Google アナリティクスのレポート

このガイドで説明を希望するプラットフォームを選択してください。

リクエストの形式を設定

Google アナリティクス Measurement Protocol は、HTTP POST リクエストのみをサポートしています。

イベントを送信するには、次の形式を使用してください。

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

リクエスト URL に次の情報を必ず含めてください。

  • api_secret: Google アナリティクス管理画面で生成される API Secret

    新しい Secret を作成するには、[管理] > [データ ストリーム] > ストリームを選択 > [Measurement Protocol] > [作成] の順にクリックします。

  • firebase_app_id: Firebase アプリ ID。この ID は、Firebase コンソールの [プロジェクトの設定] > [全般] > [マイアプリ] > [アプリ ID] で確認できます。

    firebase_app_idapp_instance_id とは異なります。firebase_app_id はお客様のアプリを識別し、一方の app_instance_id はそのアプリの 1 回のインストールを識別します。

詳しくは、クエリ パラメータをご覧ください。

リクエスト本文には、次の情報を必ず含めてください。

  • user_id: 省略可。ユーザーの一意の識別子。使用できるのは UTF-8 のみです あります。この識別子について詳しくは、クロスプラットフォーム分析用の User-ID をご覧ください。

  • consent: 省略可。同意設定の設定方法をご確認ください。

  • timestamp_micros: 省略可。マイクロ秒単位の Unix エポック時刻。 リクエスト内のイベントとユーザー プロパティ。指定しない場合、 リクエスト時刻

  • events: イベント項目の配列。1 つのイベントに複数のイベントを含めることができる リクエストできます。

    ユーザーアクティビティをレポートに表示する方法は リアルタイム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 4 don't populate
    Search audiences in Google Ads for app users.

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