DEV Community

関聖矢
関聖矢

Posted on

Find every custom parameter your GA4 implementation actually sends (a key-inventory query)

Every GA4 implementation drifts from its documentation. Someone added a parameter for a campaign in March; a developer renamed one in June; the measurement plan says plan_type but the site sends planType. Before you write analysis SQL against custom dimensions, you need the inventory of what actually arrives — and BigQuery will just tell you.

The key inventory

event_params is a repeated record of key/value pairs. UNNEST it over a single day and count by key, including which value type each key carries:

SELECT
  ep.key,
  COUNT(*) AS occurrences,
  COUNTIF(ep.value.string_value IS NOT NULL) AS has_string,
  COUNTIF(ep.value.int_value IS NOT NULL) AS has_int,
  COUNTIF(ep.value.float_value IS NOT NULL) AS has_float
FROM `project.analytics_123456789.events_*`,
  UNNEST(event_params) AS ep
WHERE _TABLE_SUFFIX = '20260115'
GROUP BY ep.key
ORDER BY occurrences DESC;
Enter fullscreen mode Exit fullscreen mode

The same for user_properties:

SELECT
  prop.key,
  COUNT(*) AS occurrences,
  COUNTIF(prop.value.string_value IS NOT NULL) AS has_string,
  COUNTIF(prop.value.int_value IS NOT NULL) AS has_int
FROM `project.analytics_123456789.events_*`,
  UNNEST(user_properties) AS prop
WHERE _TABLE_SUFFIX = '20260115'
GROUP BY prop.key
ORDER BY occurrences DESC;
Enter fullscreen mode Exit fullscreen mode

One day is enough for the inventory (and cheap). What you're looking for:

  • Keys you didn't know existed — usually a tag someone added without updating the measurement plan.
  • Near-duplicate keys — plan_type and planType, ab_variant and variant. Each is probably half your data.
  • Type surprises — a key that's string on some events and int on others (has_string and has_int both large). Extracting with the wrong value.*_value returns NULLs, silently, which is how "the parameter stopped working" reports are born.
  • Keys with tiny occurrence counts — dead parameters still being sent, or a tag firing on one template only.

Extracting a parameter once you know it exists

The basic pattern, per type:

SELECT
  event_name,
  event_timestamp,
  (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'plan_type') AS plan_type,
  (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'item_count') AS item_count,
  (SELECT value.float_value FROM UNNEST(event_params) WHERE key = 'scroll_depth') AS scroll_depth,
  (SELECT value.double_value FROM UNNEST(event_params) WHERE key = 'score') AS score
FROM `project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20260101' AND '20260131'
  AND event_name = 'click';
Enter fullscreen mode Exit fullscreen mode

Match the extractor to the type the inventory showed. If a numeric parameter arrives as a string (common when it passes through a data layer as text), extract string_value and SAFE_CAST it — don't just switch to int_value and wonder where the rows went.

User properties work the same way against user_properties, with one twist: each event carries the property value as of that event. For "current membership tier" analysis you want the latest value per user, which is a ROW_NUMBER() over set_timestamp_micros:

WITH latest_properties AS (
  SELECT
    user_pseudo_id,
    prop.key AS property_key,
    prop.value.string_value AS property_value,
    ROW_NUMBER() OVER (
      PARTITION BY user_pseudo_id, prop.key
      ORDER BY prop.value.set_timestamp_micros DESC
    ) AS rn
  FROM `project.analytics_123456789.events_*`,
    UNNEST(user_properties) AS prop
  WHERE _TABLE_SUFFIX BETWEEN '20260101' AND '20260131'
    AND prop.key = 'membership_tier'
)
SELECT user_pseudo_id, property_value AS membership_tier
FROM latest_properties
WHERE rn = 1;
Enter fullscreen mode Exit fullscreen mode

Putting it to work: CVR by A/B variant

With the inventory confirming your variant parameter's real name and type, the analysis is a session-grain aggregation:

WITH ab_sessions AS (
  SELECT
    user_pseudo_id,
    (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'ab_variant') AS ab_variant,
    event_name
  FROM `project.analytics_123456789.events_*`
  WHERE _TABLE_SUFFIX BETWEEN '20260101' AND '20260131'
)
SELECT
  ab_variant,
  COUNT(DISTINCT CONCAT(user_pseudo_id, '.', CAST(ga_session_id AS STRING))) AS sessions,
  COUNTIF(event_name = 'purchase') AS purchases,
  ROUND(
    SAFE_DIVIDE(
      COUNTIF(event_name = 'purchase'),
      COUNT(DISTINCT CONCAT(user_pseudo_id, '.', CAST(ga_session_id AS STRING)))
    ) * 100, 2
  ) AS cvr_pct
FROM ab_sessions
WHERE ab_variant IS NOT NULL
GROUP BY ab_variant
ORDER BY cvr_pct DESC;
Enter fullscreen mode Exit fullscreen mode

(Replace ab_variant with whatever your inventory actually shows — that's the whole point of running it first.)

The short version

  1. Inventory first: UNNEST event_params / user_properties over one day, count by key and value type.
  2. Look for unknown keys, near-duplicates, mixed types, and dead parameters.
  3. Extract with the value.*_value matching the observed type; SAFE_CAST when numbers arrive as strings.
  4. User properties are as-of-event; use set_timestamp_micros + ROW_NUMBER() for latest value.
  5. Then aggregate — variant CVR, tier behaviour — on confirmed key names.

This is template 05 from my GA4 × BigQuery SQL pack. The free sample has the UNNEST, session, and funnel foundations; the full pack has all 20 templates in the same conventions.

Run the inventory on your own property and tell me the weirdest key you find in the comments — there's always one. Mine was a parameter named after the developer who added it.

Top comments (0)