This guide explains how you can send Google Analytics Measurement Protocol web and app stream events to a Google Analytics server, so that you can view Measurement Protocol events in your Google Analytics reports.
Choose the platform you want to see in this guide:
Format the request
The Measurement Protocol for Google Analytics 4 only supports HTTP POST
requests.
To send an event, use the following format:
POST /mp/collect HTTP/1.1
HOST: www.google-analytics.com
Content-Type: application/json
<payload_data>
You must provide the following in the request URL:
api_secret
: The API SECRET generated in the Google Analytics UI.To create a new secret, navigate to Admin > Data Streams > choose your stream > Measurement Protocol > Create.
firebase_app_id
: The Firebase App ID, found in the Firebase console under Project Settings > General > Your Apps > App ID.The
firebase_app_id
isn't the same as theapp_instance_id
. Thefirebase_app_id
identifies your app, whereasapp_instance_id
identifies a single installation of the app.
See query parameters for the full reference.
You must provide the following in the request body:
app_instance_id
: A unique identifier for a Firebase app instance. This is different than a webclient_id
:
user_id
: Optional. A unique identifier for a user. Can only contain utf-8 characters. See User-ID for cross-platform analysis for more information about this identifier.events
: An array of event items. You can include multiple events in one request.In order for user activity to display in reports like Realtime,
engagement_time_msec
andsession_id
must be supplied as part of theparams
for anevent
.Here's an example:
{ "client_id": "x", "events": [ { "name": "offline_purchase", "params": { "engagement_time_msec": "100", "session_id": "123" } } ] }
While
session_start
is a reserved event name, creating a newsession_id
creates a new session without the need to sendsession_start
. Understand how sessions are counted.
Try it
Here's an example you can use to send a tutorial_begin
event to your
Google Analytics server:
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.
Limitations
The following limitations apply to sending Measurement Protocol events to Google Analytics:
- 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.
- Item parameters can have a maximum of 10 custom parameters.
- The post body must be smaller than 130kB.