# Chapter 9: Refining Rules ## The Problem Your first segment worked, but real targeting is rarely that simple. You need: "High-value customers who purchased electronics OR home goods, but exclude anyone who returned an item in the last 60 days, and also exclude people already in the VIP program." Multiple conditions. OR logic. Exclusions. Segment references. This chapter shows you how. ## The Key Idea Core concept Complex segments combine simple rules with AND/OR logic and include/exclude references to other segments. AI handles the syntax. You describe the logic in plain English, then verify the YAML captures your intent. ## AND vs. OR Logic ```mermaid flowchart LR subgraph AND["AND (all must match)"] A1[LTV > 1000] --> R1[Result] A2[Country = USA] --> R1 end ``` ```mermaid flowchart LR subgraph OR["OR (any can match)"] O1[Country = USA] --> R2[Result] O2[Country = Canada] --> R2 end ``` By default, conditions combine with AND using `type: And`: ```yaml rule: type: And conditions: - type: Value attribute: lifetime_value operator: type: Greater value: 1000 - type: Value attribute: country operator: type: Equal value: "USA" ``` This means: lifetime_value > 1000 **AND** country = USA. For OR logic, use `type: Or`: ``` > "Target customers from USA OR Canada" ``` ```yaml rule: type: Or conditions: - type: Value attribute: country operator: type: Equal value: "USA" - type: Value attribute: country operator: type: Equal value: "Canada" ``` ## Complex Combinations Real targeting often mixes AND and OR. Nest rule groups to combine them: ```mermaid flowchart TD LTV[LTV > 1000] --> AND{AND} subgraph Country["OR"] USA[USA] CA[Canada] end Country --> AND subgraph Category["OR"] Elec[Electronics] Home[Home & Garden] end Category --> AND AND --> Result[Segment] ``` ``` > "High-value customers from USA or Canada who purchased electronics or home goods" ``` ```yaml rule: type: And conditions: - type: Value attribute: lifetime_value operator: type: Greater value: 1000 - type: Or conditions: - type: Value attribute: country operator: type: Equal value: "USA" - type: Value attribute: country operator: type: Equal value: "Canada" - type: Or conditions: - type: Value attribute: last_purchase_category operator: type: Equal value: "Electronics" - type: Value attribute: last_purchase_category operator: type: Equal value: "Home & Garden" ``` Reading this: LTV > 1000 AND (USA OR Canada) AND (Electronics OR Home & Garden). ## Using the In Operator For multiple values in the same field, `In` is cleaner than multiple ORs: ``` > "Target customers in USA, Canada, or UK" ``` ```yaml rule: type: Value attribute: country operator: type: In value: ["USA", "Canada", "UK"] ``` This is equivalent to: country = USA OR country = Canada OR country = UK. ## Excluding Audiences ```mermaid flowchart LR A[High Value Customers] --> M{MINUS} B[Recent Returners] --> M M --> R[Final Segment] ``` To exclude customers, use `type: exclude` with a segment reference: ``` > "Exclude customers in the Returns segment" ``` ```yaml rule: type: And conditions: - type: Value attribute: lifetime_value operator: type: Greater value: 1000 - type: exclude segment: recent-returners ``` The `type: exclude` removes anyone in the referenced segment from your audience. ## Referencing Other Segments Instead of duplicating rules, reference existing segments: ``` > "Target high-value customers but exclude the VIP segment" ``` ```yaml rule: type: And conditions: - type: Value attribute: lifetime_value operator: type: Greater value: 1000 - type: exclude segment: vip-customers ``` This includes customers matching your rules, minus anyone already in "vip-customers." You can also include other segments: ```yaml rule: type: And conditions: - type: include segment: engaged-email-subscribers - type: Value attribute: lifetime_value operator: type: Greater value: 500 ``` This targets people in "engaged-email-subscribers" AND with LTV > 500. ## Available Operators Ask AI what operators work for different field types: **Comparison (numbers, dates):** - `Equal`, `NotEqual` - `Greater`, `GreaterEqual` - `Less`, `LessEqual` - `Between` **Lists:** - `In`, `NotIn` **Strings:** - `Contain`, `StartWith`, `EndWith` - `Regexp` (pattern matching) **Time/Behaviors:** - `TimeWithinPast`, `TimeWithinNext` - `TimeToday`, `TimeThis`, `TimeNext` - `TimeRange`, `TimeInBetweenNext` **Null checks:** - `IsNull` (field has no value) ## Mental Model: Venn Diagrams Think of each rule as a circle of customers: ```mermaid flowchart TD A[LTV > $1000] -->|AND| B[USA or Canada] B -->|AND NOT| C[Returned] C --> D[Final Segment] ``` Your segment is the intersection of the circles you want, minus the circles you exclude. ## Building Complex Segments Step by Step For complicated logic, build incrementally: ``` > "Start with high-value customers" ``` Review the audience size. ``` > "Add: must have purchased in last 90 days" ``` Review again. ``` > "Add: exclude the Churned segment" ``` Final review. This iterative approach helps you understand how each rule affects the audience. ## Verifying Complex Logic After AI generates the YAML, verify by asking: ``` > "Explain this segment's targeting logic in plain English" ``` AI reads the YAML and explains: ``` This segment includes customers where: 1. Lifetime value is greater than $1000 2. AND country is either USA or Canada 3. AND they have NOT made a return in the last 60 days 4. AND they are NOT in the VIP Customers segment Estimated audience: 2,847 customers ``` If the explanation doesn't match your intent, refine the rules. ## Pitfalls **"The OR logic isn't working as expected."** Check the nesting. OR groups need to be explicitly defined: ``` > "Show me the rule structure as a logic tree" ``` **"The exclusion excluded too many people."** Exclusions might be broader than intended. Preview the segment: ``` > "How many customers are being excluded by the return rule?" ``` **"I referenced a segment that doesn't exist."** Validation catches this: ``` ✗ Segment 'VIP Customers' not found Available segments: VIP Members, VIP Program, ... ``` ## What You've Learned - Conditions combine with `type: And` or `type: Or` - Nest `And`/`Or` groups for complex logic - `In` operator handles multiple values cleanly - `type: exclude` removes segment members - `type: include` adds segment members - Build complex logic incrementally - Verify by asking AI to explain the rules ## Next Step You can target precisely. [Chapter 10](/treasure-code/book/10-activations) shows you how to send these audiences to marketing channels—Salesforce, Google Ads, email platforms, and more. *You've mastered targeting logic. Next, you'll connect it to the outside world.*