Sub topic 9
Creating custom metrics and choosing units
This chapter assumes you can already send an event parameter and register an event-scoped custom dimension. A custom metric uses the same mechanism, but it measures a number instead of labeling an event.
What a custom metric is
A custom metric is a numeric event parameter that you register so it shows up in reports as a metric. GA4 adds the values up across the events that carry the parameter. Examples are video length in seconds, items in a basket, or the shipping cost of an order.
Three facts shape every decision in this chapter:
- Custom metrics are event-scoped only. GA4 has no user-scoped or item-scoped custom metrics.
- The value must be numeric. Text is not a valid metric value.
- GA4 aggregates a custom metric by summing it. The sum is what you see in reports.
A dimension answers "which kind?" A metric answers "how much?" If you want to group or filter by the value, for example by plan tier, register a dimension. If you want to add the values up, register a metric. A numeric parameter such as basket_size can sometimes be both. In that case you can register it twice, once of each type, and the two registrations do not conflict.
Sending a numeric parameter
The parameter is sent like any other event parameter. This GTM dataLayer push sends a video length in seconds:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'video_complete',
video_length_seconds: 184
});In GTM, a GA4 event tag maps video_length_seconds to an event parameter with the same name. With gtag.js you pass the parameter directly:
gtag('event', 'video_complete', {
video_length_seconds: 184
});Send the value as a number, not as a string such as "184 sec". A value with units attached cannot be summed, and GA4 will not report it as a metric. Keep the units out of the value and set them in Admin instead.
The Measurement Protocol works the same way. The params object accepts numbers:
{
"client_id": "555.1234567890",
"events": [
{
"name": "offline_sale",
"params": {
"store_region": "north",
"basket_size": 4,
"shipping_cost": 6.5
}
}
]
}Check the numeric parameters in DebugView before you register them. Confirm that the value appears with the type and magnitude you expect. A value of 184000 where you expected 184 is much easier to fix before registration than after.
Registering the metric in Admin
You need the Editor role on the property to create custom definitions. The steps are:
- Open Admin and go to Data display, then Custom definitions.
- Select the Custom metrics tab.
- Select Create custom metric.
- Enter a Metric name. This is the label people see in reports.
- Optionally add a Description so other analysts know what the metric measures.
- Enter the Event parameter, spelled exactly as you send it. The match is case-sensitive.
- Choose the Unit of measurement.
- Save.
After you save, the metric is not retroactive. It only collects values from events that arrive after registration. Events that were sent earlier stay without the metric, even though the parameter was present in the raw data. Plan to register a metric as soon as you start sending the parameter. New definitions can also take up to 24 hours to appear in reports.
Once saved, the event parameter cannot be changed. If you misspelled it, archive the metric and create a new one. Archived definitions still count toward your quota for a period, so check the spelling before you save.
Choosing the unit of measurement
The unit tells GA4 how to format and label the number. It does not convert anything. If you choose Seconds and send milliseconds, the report will show a value 1000 times too large and label it as seconds.
| Unit | Use it for | Send the value as |
|---|---|---|
| Standard | Counts and plain numbers, such as basket size or number of searches | An integer or decimal |
| Currency | Money amounts, such as shipping cost or discount | A decimal in the property's currency |
| Milliseconds | Short timings, such as page render time | Milliseconds |
| Seconds | Durations such as video length | Seconds |
| Minutes | Longer durations | Minutes |
| Hours | Very long durations | Hours |
| Feet, Meters, Kilometers, Miles | Distances | A number in that distance unit |
Use these rules to choose:
- Pick the unit that matches what the sender already produces. Changing the sender is usually riskier than choosing a different unit.
- Use Standard when no other unit fits. A count of items is Standard, because "items" is not a unit in the list.
- Use Currency for money, and send values in the property's currency. GA4 does not convert between currencies for custom metrics. If your store sells in several currencies, convert before sending or use the built-in
valueandcurrencyparameters, which GA4 already treats as revenue. - Do not register a custom metric for
valueor other built-in ecommerce fields. GA4 already reports them.
What aggregation means for your numbers
Because GA4 sums custom metrics, the total in a report is the sum of the parameter over every matching event. Some consequences follow:
- Summing is correct for additive quantities, such as shipping cost, items, or total seconds watched.
- Summing is misleading for values that describe a state, such as a score or a temperature. Ten events with a score of 8 produce a total of 80, which is not a meaningful score. To get an average, divide the metric by the count of the events that carried it, for example by dividing in Looker Studio or after pulling data from the API.
- Duplicate events inflate the sum. If a tag fires twice on the same page, a
shipping_costof 6.5 is counted as 13. Verify that the event fires once per action. - Events without the parameter add nothing. They are not counted as zero, so they do not reduce an average computed from event counts of the right event.
For video length, the custom metric gives total seconds across all completions. The average length is that total divided by the event count of video_complete.
Using the metric in reports
Registered metrics are not added to standard reports automatically. You choose where they appear:
- In a standard report, select Customize report (the pencil icon), then Metrics, and add the custom metric.
- In an Exploration, add it from the Variables panel under Metrics, then drag it into Values.
- In audiences, a custom metric can be used in a condition, as in "total shipping cost greater than 50".
A metric is easiest to read next to a dimension. Pair video_length_seconds with the page path or a registered plan_tier dimension to see which pages and plans produce the longest viewing.
Querying a custom metric with the Data API
In the Data API, a custom metric uses the customEvent: prefix followed by the parameter name. This is the same prefix event-scoped dimensions use.
{
"dateRanges": [{ "startDate": "28daysAgo", "endDate": "today" }],
"dimensions": [{ "name": "pagePath" }],
"metrics": [
{ "name": "eventCount" },
{ "name": "customEvent:video_length_seconds" }
]
}The response contains the summed value for each page path, in the unit you chose in Admin. Divide the second metric by the first when you want an average per event. In the BigQuery export, the same value is in event_params, so you can compute the average there with the usual unnest pattern. The unit is not stored with it, so document it yourself.
Common errors
| Symptom | Likely cause |
|---|---|
| Metric exists but always shows zero | The parameter name is misspelled or differs in case, or no events have arrived since registration |
| Metric shows data only after a day | Normal processing delay for new definitions |
| Values are 1000 times too large | The sender uses milliseconds but the unit is Seconds |
| Totals are double what you expect | The event fires twice, or the tag and a code snippet both send it |
| The parameter is not in the metric list | The parameter is registered as a dimension only, or the metric quota is used up |
| No history before registration | Custom definitions are not retroactive |
Quotas
A standard property allows 50 event-scoped custom metrics. A GA4 360 property allows more. Because the limit is small and archiving does not free a slot immediately, keep metrics for numbers you will report on, and send other numbers only as parameters.
Exercise
Your checkout tag sends shipping_cost: 6.5 in an event called purchase_shipping. You registered a custom metric with the unit Standard. A colleague says the report should show money. What do you change, and does the old data update?
Show answer
Create the metric with the unit Currency (or edit it if your property lets you change the unit), and make sure the values are in the property's currency. The parameter name cannot change after creation. Data collected earlier is not rewritten, so only the display of the metric changes. If you need a cleaner history, archive the old metric and register a new one, accepting that it only collects data from that point on.
Exercise
A page timing tag sends load_time: 1250 meaning milliseconds. The custom metric uses Seconds, and the report shows 1,250 seconds per event. What went wrong, and what are the two ways to fix it?
Show answer
The unit does not convert values, so Seconds was applied to a number that was already milliseconds. Fix it either by changing the unit to Milliseconds, or by changing the sender to report seconds (1.25). Changing the unit is safer because the tag stays as it is. Rename the parameter to load_time_ms in new code so the unit is visible.