Consider a hypothetical catalogue event. The task is to return the IDs of available courses. A book is a valid item but does not qualify. A course with available set to the string "false" has invalid data. Treating both cases as “skip this row” can turn incomplete data into a convincing result.
Define valid data before selecting items
For this example, items must be an array. Every item must be an object with a non-empty string item_id, a string item_category, and a Boolean available value. An array entry cannot itself be an array. The selection rule accepts only item_category equal to "course" and available equal to true.
The result has three meanings. An array of IDs means matching items were found. An empty array means the list was valid but contained no matches. undefined means the input failed validation. These are proposed rules for this example, not universal GA4 requirements.
Make each condition answer one question
Validation asks whether the item can be interpreted safely. Selection asks whether that valid item belongs in the result. Keep these decisions separate. An if statement runs its branch when the condition is truthy. MDN’s control flow guide explains the language rule. Your tracking specification must define the business rule.
An implementation that silently drops malformed entries can understate the result without revealing the problem. Rejecting the whole input is stricter. Use that policy when completeness matters, and document it. If partial results are acceptable, return rejected-row information separately instead of hiding it.
The eligibility test needs both conditions: category is "course" AND availability is true. With OR, an available book or an unavailable course could qualify. Strict equality also prevents the string "true" from being accepted as Boolean true. Guard checks must run before property access. Reject null and non-object values before reading item_id.
