Sending events

Overview

This guide explains how to format and send requests to the Google Analytics Measurement Protocol.

Sending events

To send an event, make an HTTP POST request to the following endpoint:

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

Required parameters

A valid request requires two sets of parameters that vary if you're using Firebase or gtag.js:

  1. URL parameters

    • api_secret - Required. An 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 - Required. The Firebase App ID. The identifier for a Firebase app. Found in the Firebase console under:
      Project Settings > General > Your Apps > App ID.

    See query parameters for the full reference.

  2. JSON body

    • user_id - Optional. A unique identifier for a user. See User-ID for cross-platform analysis for more information on this identifier.

    • events - Required. An array of event items.

    See JSON POST BODY for the full reference.

Send an event

To send a tutorial_begin event, make the following request:

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: {},
    }]
  })
});

Limitations

  • 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, may 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, may only contain alpha-numeric characters and underscores, and must start with an alphabetic character.
  • Parameter values (including item parameter values) must be 100 character or fewer.
  • Item parameters can have a maximum of 10 custom parameters.
  • The post body must be smaller than 130kB.