GA4 in BigQuery · Working with the export
Query GA4 user properties in BigQuery
GA4 exports user properties in the repeated user_properties record on each event, with key, value slots and set_timestamp_micros. List the keys with UNNEST, read one with a scalar subquery, and keep the latest value per user with ARRAY_AGG ordered by set_timestamp_micros.
The query
-- Which user properties are set, and for how many users
SELECT
up.key,
COUNT(*) AS events,
COUNT(DISTINCT user_pseudo_id) AS users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`,
UNNEST(user_properties) AS up
WHERE _TABLE_SUFFIX BETWEEN '20201101' AND '20201130'
GROUP BY up.key
ORDER BY events DESC;How it works
User properties describe the user rather than the event: a membership tier, a preferred language, an A/B test group. The export stores them in user_properties, a repeated record shaped like event_params: key, then value with string_value, int_value, double_value and float_value, plus set_timestamp_micros, the time the property was last set. The query above is the inventory: which keys exist and how many users carry them.
To read one property per event, use the same scalar subquery pattern as for event parameters: (SELECT value.string_value FROM UNNEST(user_properties) WHERE key = 'customer_tier'). To get one value per user, aggregate across events and keep the most recently set value, as in the second variation.
The sample is a poor place to see real values. Its user_properties is empty on all but 134 of 4,295,584 events, and those 134 entries have NULL keys and values. Obfuscation also changed the column types: in the sample user_properties.key and the value slots are INTEGER, while Google's schema (and your own export) has key as STRING. The variations therefore wrap key and string_value in CAST(... AS STRING) so they run on the sample; on your own export the casts do nothing and you can drop them.
| Column | Meaning |
|---|---|
key | Name of the user property. NULL in the sample, because of obfuscation. |
events | Events that carry the property. |
users | Distinct user_pseudo_id values that carry it. |
Notes and cost
- Cost:
user_propertiesmeasures 0 bytes in the sample because it is nearly empty. In a real export it repeats on every event of the user, so it can be one of the larger columns. If you need properties per user, compute them once into a small user table instead of unnesting them in every report. ARRAY_AGGfails with array cannot have a NULL element when a value is NULL. UseIGNORE NULLS, as the variation does.- A property can change during your date range. Decide whether a report needs the value at the time of each event (read it per event) or the current value (latest
set_timestamp_microsper user).
Variations
Read one user property per event
SELECT
event_date,
event_name,
user_pseudo_id,
(SELECT CAST(value.string_value AS STRING)
FROM UNNEST(user_properties)
WHERE CAST(key AS STRING) = 'customer_tier') AS customer_tier
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX = '20201106';Latest value per user
Orders the values by set_timestamp_micros and keeps the newest. IGNORE NULLS skips events where the value is missing.
SELECT
user_pseudo_id,
ARRAY_AGG(CAST(up.value.string_value AS STRING) IGNORE NULLS
ORDER BY up.value.set_timestamp_micros DESC
LIMIT 1)[SAFE_OFFSET(0)] AS customer_tier
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`,
UNNEST(user_properties) AS up
WHERE _TABLE_SUFFIX BETWEEN '20201101' AND '20201130'
AND CAST(up.key AS STRING) = 'customer_tier'
GROUP BY user_pseudo_id;FAQ
Why are user properties empty in the GA4 sample dataset?
The public sample is obfuscated. Only 134 events carry user property entries, and their keys and values are NULL. The SQL pattern is the same on a real export, where keys are strings such as customer_tier.
What is set_timestamp_micros?
The time, in microseconds since the epoch, when the user property was last set. Use it to pick the current value when a user's property changed during the date range.
Why do I get 'No matching signature for operator =' on the sample?
In the sample, user_properties.key is INTEGER, so comparing it with a string literal fails (no matching signature for operator). Cast it with CAST(key AS STRING); in a real export the key is already a STRING.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
