Inviare eventi Measurement Protocol a Google Analytics

Questa guida spiega come inviare Stream web e di app di Google Analytics Measurement Protocol eventi per un server di Google Analytics, in modo da poter visualizzare gli eventi Measurement Protocol nei report di Google Analytics.

Scegli la piattaforma che vuoi visualizzare in questa guida:

Formatta la richiesta

Measurement Protocol di Google Analytics supporta solo le richieste POST HTTP.

Per inviare un evento, utilizza il seguente formato:

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

Devi fornire quanto segue nell'URL della richiesta:

  • api_secret: l'API SECRET generata nell'interfaccia utente di Google Analytics.

    Per creare un nuovo secret, vai ad Amministrazione > Stream di dati > scegli il tuo stream > Measurement Protocol > Crea.

  • measurement_id: l'ID misurazione associato a uno stream, presente nella UI di Google Analytics in Amministrazione > Stream di dati > scegli il tuo stream > ID misurazione.

    measurement_id non è il tuo ID stream.

Per un riferimento completo, consulta i parametri di query.

Nel corpo della richiesta devi fornire quanto segue:

  • client_id: un identificatore univoco di un client. Questo è diverso da Firebase app_instance_id. Utilizza gtag.js('get').
  • user_id: facoltativo. Un identificatore univoco per un utente. Può contenere solo UTF-8 caratteri. Per saperne di più, consulta User-ID per l'analisi multipiattaforma su questo identificatore.

  • consent: facoltativo. Scopri come impostare il consenso impostazioni.

  • timestamp_micros: facoltativo. Il tempo dell'epoca Unix, in microsecondi, per ed eventi e proprietà utente nella richiesta. Se non specificato, il valore predefinito è la al momento della richiesta.

  • events: un array di elementi evento. Puoi includere più eventi in uno richiesta.

    Affinché l'attività degli utenti venga visualizzata in report quali In tempo reale, engagement_time_msec e session_id devono essere forniti nell'ambito della params per event. Il parametro engagement_time_msec deve riflettere la durata del coinvolgimento dell'evento in millisecondi.

    Ecco un esempio:

```json { "client_id": "123456.7654321", "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 measurement_id = `G-XXXXXXXXXX`; const api_secret = `<secret_value>`; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}`, { method: "POST", body: JSON.stringify({ client_id: 'XXXXXXXXXX.YYYYYYYYYY', events: [{ name: 'tutorial_begin', params: {}, }] }) }); ``` ## 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].