# Rule Schema Reference

Source: https://appfig.com/docs/rule-schema

Complete reference for the AppFig rule schema (v2.0.0).

## Rule Structure

A feature can have one or more rules. Each rule has:

```json
{
  "conditions": {
    "events": { /* event conditions */ },
    "user_properties": [ /* user property conditions */ ],
    "user_properties_operator": "AND" | "OR",
    "device": [ /* device conditions */ ],
    "device_operator": "AND" | "OR"
  },
  "value": any  // What to return if conditions match
}
```

**All three condition types must pass for a rule to match** (AND logic between types).

## Event Conditions

Events can be checked in two modes: **Simple** and **Sequence**.

### Simple Mode

Check if events exist with certain criteria.

```json
{
  "mode": "simple",
  "operator": "AND" | "OR",
  "items": [
    {
      "key": "event_name",
      "count": {
        "operator": "==" | "!=" | ">" | "<" | ">=" | "<=",
        "value": number
      },
      "within_last_days": number,
      "param": {
        "param_name": {
          "operator": "==" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "not_in" | "contains",
          "value": any
        }
      },
      "not": boolean
    }
  ]
}
```

**Fields:**
- `mode` - Must be `"simple"`
- `operator` - How to combine items: `"AND"` (all must match) or `"OR"` (any can match)
- `items` - Array of event conditions

**Item fields:**
- `key` (required) - Event name to match
- `count` (optional) - How many times event must occur
- `within_last_days` (optional) - Only count events in last N days
- `param` (optional) - Match event parameters
- `not` (optional) - Negate the condition (default: false)

**Example: User completed 10+ levels AND made a purchase**

```json
{
  "mode": "simple",
  "operator": "AND",
  "items": [
    {
      "key": "level_complete",
      "count": {
        "operator": ">=",
        "value": 10
      }
    },
    {
      "key": "purchase_made",
      "count": {
        "operator": ">=",
        "value": 1
      }
    }
  ]
}
```

**Example: User opened app recently OR completed tutorial**

```json
{
  "mode": "simple",
  "operator": "OR",
  "items": [
    {
      "key": "app_open",
      "count": {
        "operator": ">=",
        "value": 1
      },
      "within_last_days": 7
    },
    {
      "key": "tutorial_complete",
      "count": {
        "operator": ">=",
        "value": 1
      }
    }
  ]
}
```

**Example: User has NOT made a purchase**

```json
{
  "mode": "simple",
  "operator": "AND",
  "items": [
    {
      "key": "purchase_made",
      "count": {
        "operator": ">=",
        "value": 1
      },
      "not": true
    }
  ]
}
```

### Sequence Mode

Check if events occurred in a specific order.

```json
{
  "mode": "sequence",
  "ordering": "direct" | "indirect",
  "steps": [
    {
      "key": "event_name",
      "count": {
        "operator": "==" | "!=" | ">" | "<" | ">=" | "<=",
        "value": number
      },
      "within_last_days": number,
      "param": {
        "param_name": {
          "operator": "==" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "not_in" | "contains",
          "value": any
        }
      }
    }
  ]
}
```

**Fields:**
- `mode` - Must be `"sequence"`
- `ordering` - Sequence type:
  - `"direct"` - Events must be consecutive (no other events between them)
  - `"indirect"` - Events must be in order but can have other events between
- `steps` - Array of events in required order

**Example: Onboarding flow (direct)**

```json
{
  "mode": "sequence",
  "ordering": "direct",
  "steps": [
    { "key": "tutorial_start" },
    { "key": "tutorial_step_1" },
    { "key": "tutorial_step_2" },
    { "key": "tutorial_complete" }
  ]
}
```

**Example: Engagement pattern (indirect)**

```json
{
  "mode": "sequence",
  "ordering": "indirect",
  "steps": [
    { "key": "app_open" },
    { "key": "level_complete" },
    { "key": "purchase_made" }
  ]
}
```

## User Property Conditions

Check attributes of the user.

```json
{
  "user_properties": [
    {
      "key": "property_name",
      "value": {
        "operator": "==" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "not_in" | "contains",
        "value": any
      },
      "not": boolean
    }
  ],
  "user_properties_operator": "AND" | "OR"
}
```

**Fields:**
- `key` (required) - Property name
- `value.operator` (required) - Comparison operator
- `value.value` (required) - Value to compare against
- `not` (optional) - Negate the condition (default: false)
- `user_properties_operator` - How to combine multiple properties (default: `"AND"`)

**Example: Premium users**

```json
{
  "user_properties": [
    {
      "key": "subscription_tier",
      "value": {
        "operator": "==",
        "value": "premium"
      }
    }
  ],
  "user_properties_operator": "AND"
}
```

**Example: Premium OR trial users**

```json
{
  "user_properties": [
    {
      "key": "subscription_tier",
      "value": {
        "operator": "==",
        "value": "premium"
      }
    },
    {
      "key": "trial_active",
      "value": {
        "operator": "==",
        "value": "true"
      }
    }
  ],
  "user_properties_operator": "OR"
}
```

**Example: Account older than 30 days**

```json
{
  "user_properties": [
    {
      "key": "account_age_days",
      "value": {
        "operator": ">=",
        "value": 30
      }
    }
  ]
}
```

## Device Conditions

Check device/platform attributes (auto-detected by SDK).

```json
{
  "device": [
    {
      "key": "property_name",
      "value": {
        "operator": "==" | "!=" | ">" | "<" | ">=" | "<=" | "in" | "not_in" | "contains",
        "value": any
      },
      "not": boolean
    }
  ],
  "device_operator": "AND" | "OR"
}
```

**Auto-detected device properties:**

| Property | Example Values | Description |
|----------|---------------|-------------|
| `platform` | `"iOS"`, `"Android"`, `"Windows"`, `"macOS"` | Operating system |
| `os_version` | `"17.0"`, `"13"`, `"11.0"` | OS version string |
| `language` | `"en-US"`, `"ja"`, `"es-ES"` | System language |
| `country` | `"US"`, `"JP"`, `"GB"` | Country code (auto- detected from CDN) |
| `timezone` | `"America/Los_Angeles"`, `"Asia/Tokyo"` | Timezone identifier |
| `device_brand` | `"Apple"`, `"Samsung"`, `"Google"` | Device manufacturer |
| `device_model` | `"iPhone 15"`, `"Galaxy S23"` | Device model |
| `app_version` | `"1.2.3"` | App version (if set manually) |

**Example: Mobile platforms only**

```json
{
  "device": [
    {
      "key": "platform",
      "value": {
        "operator": "in",
        "value": ["iOS", "Android"]
      }
    }
  ]
}
```

**Example: US users only**

```json
{
  "device": [
    {
      "key": "country",
      "value": {
        "operator": "==",
        "value": "US"
      }
    }
  ]
}
```

**Example: Exclude old OS versions**

```json
{
  "device": [
    {
      "key": "os_version",
      "value": {
        "operator": ">=",
        "value": "15.0"
      }
    }
  ]
}
```

## Operators

### Comparison Operators

| Operator | Description | Example |
|----------|-------------|---------|
| `==` | Equals | `"premium" == "premium"` → true |
| `!=` | Not equals | `"free" != "premium"` → true |
| `>` | Greater than | `10 > 5` → true |
| `<` | Less than | `5 < 10` → true |
| `>=` | Greater or equal | `10 >= 10` → true |
| `<=` | Less or equal | `5 <= 10` → true |

**Type handling:**
- Strings compared lexicographically
- Numbers parsed and compared numerically
- Booleans compared as strings

### Array Operators

| Operator | Description | Example |
|----------|-------------|---------|
| `in` | Value in array | `"iOS" in ["iOS", "Android"]` → true |
| `not_in` | Value not in array | `"Windows" not_in ["iOS", "Android"]` → true |

### String Operators

| Operator | Description | Example |
|----------|-------------|---------|
| `contains` | String contains substring | `"hello world" contains "world"` → true |

## Value Types

A value is one switch, number or text, stored as a string:

**Boolean:**
```json
{ "value": "on" }  // Checked with IsFeatureEnabled()
{ "value": "off" }
```

**String:**
```json
{ "value": "Welcome back!" }
```

**Number:**
```json
{ "value": "5" }    // Stored as string, parse in your app
{ "value": "99.99" }
```

**One value per feature.** There are no object or list values. A Remote Config parameter that holds a whole JSON object is read field by field instead: each field becomes its own feature, and the JSON answers whenever AppFig has no value (see [Guided Setup](https://appfig.com/docs/guided-setup)). Fields that change together go in one [experiment](https://appfig.com/docs/experiments).

## Complete Example

**Feature:** "premium_features"

**Rules:**
1. Show "full" premium to paying users
2. Show "trial" premium to trial users who completed onboarding
3. Show nothing to everyone else (no matching rule)

```json
{
  "premium_features": [
    {
      "conditions": {
        "events": {
          "mode": "simple",
          "operator": "AND",
          "items": []
        },
        "user_properties": [
          {
            "key": "subscription_tier",
            "value": {
              "operator": "==",
              "value": "premium"
            }
          }
        ],
        "user_properties_operator": "AND",
        "device": [],
        "device_operator": "AND"
      },
      "value": "full"
    },
    {
      "conditions": {
        "events": {
          "mode": "sequence",
          "ordering": "indirect",
          "steps": [
            { "key": "onboarding_start" },
            { "key": "onboarding_complete" }
          ]
        },
        "user_properties": [
          {
            "key": "trial_active",
            "value": {
              "operator": "==",
              "value": "true"
            }
          }
        ],
        "user_properties_operator": "AND",
        "device": [],
        "device_operator": "AND"
      },
      "value": "trial"
    }
  ]
}
```

## Empty Conditions

To match all users (always-on feature), use empty condition arrays:

```json
{
  "conditions": {
    "events": {
      "mode": "simple",
      "operator": "AND",
      "items": []
    },
    "user_properties": [],
    "user_properties_operator": "AND",
    "device": [],
    "device_operator": "AND"
  },
  "value": "on"
}
```

This rule always matches and returns `"on"`.

## Next Steps

- **[A/B Testing](https://appfig.com/docs/ab-testing)** - Use a rule's return value to run an experiment instead of a static value
- **[Rule Prioritization](https://appfig.com/docs/rule-prioritization)** - Control which RuleSet wins when several could match
- **[Testing Guide](https://appfig.com/docs/testing)** - Test your rules
- **[Unity SDK](https://appfig.com/docs/unity-integration)** - Use rules in Unity
- **[React SDK](https://appfig.com/docs/react-web-guide)** - Use rules in React
