> Part of the walkerOS documentation. Project overview and full index: <https://www.walkeros.io/llms.txt>

# GA4 Measurement Protocol

[Server](#)[ Source code](https://github.com/elbwalker/walkerOS/tree/main/packages/server/destinations/ga4)[ Package](https://www.npmjs.com/package/@walkeros/server-destination-ga4) Beta

Server-side events to Google Analytics 4 through the [Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4). Use it for events that happen on a server (a sign-up confirmed by your app, a payment webhook) or that should not depend on the browser; the [gtag web destination](https://www.walkeros.io/docs/destinations/web/gtag/ga4.md) stays the way to send page views and browser events.

Where this fits

GA4 Measurement Protocol is a **server destination** in the walkerOS flow:

Sends one event per request to \`/mp/collect\` with the measurement id and API secret in the query string, using raw HTTP (no SDK). Nothing personal is sent unless you map it: no email, IP or user agent.

## Installation[​](#installation "Direct link to Installation")

```bash
npm install @walkeros/collector @walkeros/server-destination-ga4
```

* Integrated
* Bundled

```typescript
import { startFlow } from '@walkeros/collector';
import destinationGa4 from '@walkeros/server-destination-ga4';

await startFlow({
  destinations: {
    ga4: {
      code: destinationGa4,
      config: {
        settings: {
          measurementId: 'G-TEST123',
          apiSecret: process.env.GA4_API_SECRET,
        },
      },
    },
  },
});
```

A complete `flow.json` with this destination:

```json
{
  "version": 4,
  "flows": {
    "default": {
      "config": {
        "platform": "server"
      },
      "destinations": {
        "ga4": {
          "package": "@walkeros/server-destination-ga4",
          "config": {
            "settings": {
              "measurementId": "G-TEST123",
              "apiSecret": "$secret.GA4_API_SECRET"
            }
          }
        }
      }
    }
  }
}
```

[See bundled mode setup](https://www.walkeros.io/docs/getting-started/modes/bundled.md) | [CLI reference](https://www.walkeros.io/docs/apps/cli.md)

## Configuration[​](#configuration "Direct link to Configuration")

This destination uses the standard destination config wrapper (consent, data, env, id, ...). For the shared fields see [destination configuration](https://www.walkeros.io/docs/destinations.md#configuration). Package-specific fields live under `config.settings` and are listed below.

## Settings[​](#settings "Direct link to Settings")

| Property         | Type     | Description                                                                                                                                                  | More |
| ---------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---- |
| `measurementId*` | `string` | GA4 measurement id of the web data stream                                                                                                                    |      |
| `apiSecret*`     | `string` | Measurement Protocol API secret: GA4 Admin > Data streams > the stream > Measurement Protocol API secrets                                                    |      |
| `url`            | `string` | Endpoint without query string. EU collection: https\://region1.google-analytics.com/mp/collect; testing: https\://www\.google-analytics.com/debug/mp/collect |      |

\* Required fields

## Mapping[​](#mapping "Direct link to Mapping")

This package does not define custom rule-level settings. For the standard rule fields (consent, condition, data, batch, name, policy) see [mapping](https://www.walkeros.io/docs/mapping.md).

## Examples

### Lead with user id

A demo request is sent as generate\_lead with the visitor GA4 client id and the signed-in user id.

Event

```json
{
  "name": "demo request",
  "data": {
    "location": "hero"
  },
  "context": {
    "dev": [
      "test",
      1
    ]
  },
  "globals": {
    "ga_client_id": "1234567890.1699990000"
  },
  "custom": {
    "completely": "random"
  },
  "user": {
    "id": "user-42"
  },
  "nested": [
    {
      "entity": "child",
      "data": {
        "is": "subordinated"
      }
    }
  ],
  "consent": {
    "functional": true
  },
  "id": "ev-1700000901",
  "trigger": "test",
  "entity": "demo",
  "action": "request",
  "timestamp": 1700000901000,
  "timing": 3.14,
  "source": {
    "count": 1,
    "trace": "0a1b2c3d4e5f60718293a4b5c6d7e8f9",
    "type": "collector",
    "schema": "4"
  }
}
```

Mapping

```json
{
  "name": "generate_lead",
  "data": {
    "map": {
      "client_id": "globals.ga_client_id",
      "user_id": "user.id",
      "params": {
        "map": {
          "location": "data.location"
        }
      }
    }
  }
}
```

Out

```javascript
sendServer("https://www.google-analytics.com/mp/collect?measurement_id=G-TEST123&api_secret=s3cr3t", "{\"client_id\":\"1234567890.1699990000\",\"user_id\":\"user-42\",\"consent\":{\"ad_user_data\":\"DENIED\",\"ad_personalization\":\"DENIED\"},\"events\":[{\"name\":\"generate_lead\",\"params\":{\"location\":\"hero\",\"engagement_time_msec\":1}}]}", {
  "headers": {
    "Content-Type": "application/json"
  }
})
```

### Purchase

A completed order joins the visitor GA4 session as a purchase with value, currency and transaction id; marketing consent grants the ad signals.

Event

```json
{
  "name": "order complete",
  "data": {
    "id": "ORD-300",
    "total": 249.99,
    "currency": "EUR"
  },
  "context": {
    "shopping": [
      "complete",
      0
    ]
  },
  "globals": {
    "ga_client_id": "1234567890.1699990000",
    "ga_session_id": "1699999000"
  },
  "custom": {
    "completely": "random"
  },
  "user": {
    "id": "us3r",
    "device": "c00k13",
    "session": "s3ss10n"
  },
  "nested": [
    {
      "entity": "product",
      "data": {
        "id": "ers",
        "name": "Everyday Ruck Snack",
        "color": "black",
        "size": "l",
        "price": 420
      },
      "context": {
        "shopping": [
          "complete",
          0
        ]
      },
      "nested": []
    },
    {
      "entity": "product",
      "data": {
        "id": "cc",
        "name": "Cool Cap",
        "size": "one size",
        "price": 42
      },
      "context": {
        "shopping": [
          "complete",
          0
        ]
      },
      "nested": []
    },
    {
      "entity": "gift",
      "data": {
        "name": "Surprise"
      },
      "context": {
        "shopping": [
          "complete",
          0
        ]
      },
      "nested": []
    }
  ],
  "consent": {
    "marketing": true
  },
  "id": "ev-1700000900",
  "trigger": "load",
  "entity": "order",
  "action": "complete",
  "timestamp": 1700000900000,
  "timing": 3.14,
  "source": {
    "count": 1,
    "trace": "0a1b2c3d4e5f60718293a4b5c6d7e8f9",
    "type": "collector",
    "schema": "4"
  }
}
```

Mapping

```json
{
  "name": "purchase",
  "data": {
    "map": {
      "client_id": "globals.ga_client_id",
      "session_id": "globals.ga_session_id",
      "params": {
        "map": {
          "value": "data.total",
          "currency": "data.currency",
          "transaction_id": "data.id"
        }
      }
    }
  }
}
```

Out

```javascript
sendServer("https://www.google-analytics.com/mp/collect?measurement_id=G-TEST123&api_secret=s3cr3t", "{\"client_id\":\"1234567890.1699990000\",\"consent\":{\"ad_user_data\":\"GRANTED\",\"ad_personalization\":\"GRANTED\"},\"events\":[{\"name\":\"purchase\",\"params\":{\"value\":249.99,\"currency\":\"EUR\",\"transaction_id\":\"ORD-300\",\"session_id\":\"1699999000\",\"engagement_time_msec\":1}}]}", {
  "headers": {
    "Content-Type": "application/json"
  }
})
```

### Event without GA4 client id

Without a \_ga client id, a stable one is derived from the event id so the event still counts (as a new user) and a retry keeps the same id.

Event

```json
{
  "name": "user register",
  "data": {
    "method": "email"
  },
  "context": {
    "dev": [
      "test",
      1
    ]
  },
  "globals": {
    "lang": "elb"
  },
  "custom": {
    "completely": "random"
  },
  "user": {
    "id": "us3r",
    "device": "c00k13",
    "session": "s3ss10n"
  },
  "nested": [
    {
      "entity": "child",
      "data": {
        "is": "subordinated"
      }
    }
  ],
  "consent": {
    "functional": true
  },
  "id": "ev-1700000902",
  "trigger": "test",
  "entity": "user",
  "action": "register",
  "timestamp": 1700000902000,
  "timing": 3.14,
  "source": {
    "count": 1,
    "trace": "0a1b2c3d4e5f60718293a4b5c6d7e8f9",
    "type": "collector",
    "schema": "4"
  }
}
```

Mapping

```json
{
  "name": "sign_up",
  "data": {
    "map": {
      "params": {
        "map": {
          "method": "data.method"
        }
      }
    }
  }
}
```

Out

```javascript
sendServer("https://www.google-analytics.com/mp/collect?measurement_id=G-TEST123&api_secret=s3cr3t", "{\"client_id\":\"735042307.1700000902\",\"consent\":{\"ad_user_data\":\"DENIED\",\"ad_personalization\":\"DENIED\"},\"events\":[{\"name\":\"sign_up\",\"params\":{\"method\":\"email\",\"engagement_time_msec\":1}}]}", {
  "headers": {
    "Content-Type": "application/json"
  }
})
```

## Joining the visitor's session[​](#joining-the-visitors-session "Direct link to Joining the visitor's session")

The Measurement Protocol attributes an event to a visitor and a session only through ids you send. Read them in the browser and carry them to the server event (for example as `globals`):

| Mapped `data` | Sent as                       | Where it comes from                                                                |
| ------------- | ----------------------------- | ---------------------------------------------------------------------------------- |
| `client_id`   | `client_id`                   | The `_ga` cookie `GA1.1.<random>.<seconds>`: send `<random>.<seconds>`             |
| `session_id`  | `events[0].params.session_id` | The `_ga_<stream>` cookie (digits)                                                 |
| `user_id`     | `user_id`                     | Your own user id (optional)                                                        |
| `params`      | `events[0].params`            | Event parameters, such as `value` (sent as a number), `currency`, `transaction_id` |

With a valid `client_id` and `session_id` the event joins the visitor's session and its traffic source. Without a valid `client_id`, the destination derives a stable one from the walkerOS event id: the event still counts, as a new user, and a retry keeps the same id. `engagement_time_msec: 1` is added so the event counts as engaged.

```json
"mapping": {
  "order": { "complete": {
    "name": "purchase",
    "data": { "map": {
      "client_id": "globals.ga_client_id",
      "session_id": "globals.ga_session_id",
      "params": { "map": {
        "value": "data.total",
        "currency": "data.currency",
        "transaction_id": "data.id"
      } }
    } }
  } }
}
```

## Event name[​](#event-name "Direct link to Event name")

The mapped name, with `_` for spaces (`order complete` becomes `order_complete`). GA4 accepts letters, digits and `_`, starting with a letter, up to 40 characters: an invalid name fails with the code `mapping` and nothing is sent. Use GA4's recommended names (`purchase`, `sign_up`, `generate_lead`) for reports and key events to pick them up.

## Consent[​](#consent "Direct link to Consent")

`consent.ad_user_data` and `consent.ad_personalization` are `GRANTED` only when the event carries `marketing` consent, else `DENIED`. Gate the destination itself on measurement consent: add `consent: { analytics: true }` to `config`, next to `settings` (see [Gate a destination with consent](https://www.walkeros.io/docs/guides/consent.md#gate-a-destination-with-consent)).

## Timestamps[​](#timestamps "Direct link to Timestamps")

`timestamp_micros` is the event time when the event is less than 71 hours old; GA4 refuses older ones, so an older event (a late retry) is sent without it and GA4 uses the reception time.

## Testing[​](#testing "Direct link to Testing")

GA4 answers 2xx even to an invalid payload. While setting up, point `url` to the validation server, which answers with the problems it finds and records nothing:

```json
"settings": {
  "measurementId": "G-XXXXXXXXXX",
  "apiSecret": "$secret.GA4_API_SECRET",
  "url": "https://www.google-analytics.com/debug/mp/collect"
}
```

For collection in the EU, use `https://region1.google-analytics.com/mp/collect`.

## Failures[​](#failures "Direct link to Failures")

* A response outside the 2xx range, a network error or no answer within 5 seconds fails with `transport` (with the HTTP status) or `timeout`, and the event goes to the dead-letter queue. 429 and 5xx are marked retryable, other 4xx not; nothing resends the event by itself.
* Without `measurementId` or `apiSecret`, or with a measurement id that is not `G-...`, the destination does not start: every push answers `failed` with the code `config`.
* A rule with `silent: true` sends nothing.

## Links[​](#links "Direct link to Links")

* [GA4 Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/ga4)
* [Validating events](https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events)
