# Marketing as Code

tdx enables **Marketing as Code**—a paradigm shift from manual UI operations to AI-friendly marketing configurations.

Learn with AI
New to Marketing as Code? Read the [AI-Native Marketing Book](/treasure-code/book/) for a step-by-step guide from setup to segments, journeys, and workflows.

## The Problem

Traditional marketing operations rely on clicking through UIs:

- **Manual and error-prone** - Complex sequences of clicks that are hard to reproduce
- **Invisible to AI** - AI assistants cannot navigate UIs or understand click-based workflows
- **Hard to scale** - Cannot easily replicate configurations across brands or environments
- **No version control** - Difficult to track changes, review history, or roll back mistakes


## The Solution

tdx treats marketing logic as code:

| Traditional "Click Ops" | Marketing as Code |
|  --- | --- |
| Configure in UI | Define in YAML files |
| Manual testing | Local validation with `tdx` |
| Direct production changes | Test locally, then deploy |
| Hard to automate | AI agents can read/write |


## YAML as Source of Truth

Instead of navigating complex UIs, define your marketing logic in simple YAML files:


```yaml
name: High-Value Customers
description: Customers with lifetime value > $1000
rules:
  - attribute: lifetime_value
    operator: greater_than
    value: 1000
activations:
  - connection: salesforce-prod
    enabled: true
```


```yaml
type: journey
name: Welcome Series
reentry: no_reentry

stages:
  - name: Onboarding
    steps:
      - type: activation
        name: Send Welcome Email
        with:
          activation: welcome-email
        next: wait-for-engagement

      - type: wait
        name: Wait for Engagement
        id: wait-for-engagement
        with:
          condition:
            segment: engaged-users
            timeout:
              duration: 7
              unit: day
        next: follow-up

      - type: activation
        name: Follow Up
        id: follow-up
        with:
          activation: follow-up-email

activations:
  welcome-email:
    connection: email-provider
  follow-up-email:
    connection: email-provider

segments:
  engaged-users:
    rules:
      - attribute: email_opened
        operator: is_true
```

**Why YAML?**

- AI excels at generating structured text like YAML
- Human-readable and easy to review
- Standard format with excellent tooling support
- Avoids hallucinations from complex JSON APIs


See [Segment YAML Schema](/treasure-code/commands/segment#segment-yaml-schema) and [Journey YAML Schema](/treasure-code/commands/journey#journey-yaml-schema) for full specifications.

## CDP Architecture

CDP resources follow a hierarchical structure:


```mermaid
flowchart TB
    subgraph Parent["Parent Segment (tdx ps)"]
        Master[(Master Table)]
        Attrs[Attributes]
        Behaviors[Behaviors]
        Master --> Attrs
        Master --> Behaviors
    end

    subgraph Children["Child Segments (tdx sg)"]
        Seg1[Segment A<br/>Rules + Activations]
        Seg2[Segment B<br/>Rules + Activations]
    end

    subgraph Journeys["Journeys (tdx journey)"]
        J1[Welcome Series]
        J2[Re-engagement]
    end

    Parent --> Seg1
    Parent --> Seg2
    Parent --> J1
    Parent --> J2

    Seg1 --> Act1[Salesforce]
    Seg2 --> Act2[Facebook]
    J1 --> Act3[Email Sequence]
    J2 --> Act4[Push Notifications]
```

**Parent Segment** defines the data foundation:

- **Master Table** - Source of customer records
- **Attributes** - Customer properties (e.g., lifetime_value, signup_date)
- **Behaviors** - Customer actions (e.g., purchases, page_views)


**Child Segments** define targeting rules and activations:

- Filter customers using attributes and behaviors
- Activate to marketing channels (Salesforce, Google Ads, etc.)


**Journeys** orchestrate multi-step customer experiences:

- Define stages with sequential or conditional steps
- Wait for time delays or segment conditions
- Branch based on A/B tests or decision points
- Activate to channels at each step


## The Lifecycle

Marketing as Code follows a familiar development workflow:


```mermaid
flowchart LR
    Define["1. Define<br/>(Code)"] --> Verify["2. Verify<br/>(Test)"] --> Deploy["3. Deploy<br/>(Push)"]
```

Use these commands to manage CDP resources as code:

- [`tdx ps`](/treasure-code/commands/parent-segment) (parent-segment) - Pull, edit, and push parent segment YAML files
- [`tdx sg`](/treasure-code/commands/segment) (segment) - Pull, edit, and push child segment YAML files
- [`tdx journey`](/treasure-code/commands/journey) - Pull, edit, and push journey YAML files


### 1. Define

AI generates or you write marketing definitions locally:


```bash
# Pull existing resources to YAML
tdx sg pull "My Segment"
tdx journey pull "My Journey"

# AI can modify the YAML files
# or create new definitions from scratch
```

### 2. Verify

Test your changes locally before touching production:


```bash
# Validate definitions
tdx sg validate
tdx journey validate

# Preview sample data
tdx ps preview
tdx sg show

# Check what would change
tdx sg push --dry-run
tdx journey push --dry-run
```

### 3. Deploy

Push verified changes to production:


```bash
# Deploy segment changes
tdx sg push

# Deploy journey changes
tdx journey push

# Deploy parent segment
tdx ps push
```

## Benefits

### Standardization

Code is standard. One team's segment definition looks the same as another's—solving scattered, inconsistent configurations.

### Version Control (Optional)

Store YAML files in Git to track changes over time. If an AI agent makes a mistake, you can `git revert`. You cannot easily revert a sequence of UI clicks.

### Scalability

Copy and adapt YAML files across 100 brands. You cannot copy-paste UI clicks.

### Production Safety

Test locally before deploying. Marketing as Code means you don't break production—you validate first.

### AI-Native

AI agents can read, write, and test marketing configurations safely. The complexity stays in tdx, not in your prompts.

Use [tdx claude](/treasure-code/commands/claude) to launch Claude Code with TD's [AI Foundry](https://docs.treasuredata.com/products/customer-data-platform/ai-agent-foundry) backend—AI can edit your YAML files, validate changes, and deploy segments directly.

## Getting Started


```bash
# Install tdx
npm install -g @treasuredata/tdx

# Authenticate
tdx auth setup

# Pull resources to start working locally
tdx sg pull "My Segment" -u
tdx journey pull "My Journey" -u

# Make changes to the YAML files, then push
tdx sg push
tdx journey push
```

## Learn More

- [AI-Native Marketing Book](/treasure-code/book/) - Step-by-step guide to Marketing as Code with AI
- [Parent Segment Commands](/treasure-code/commands/parent-segment) - Manage parent segments as YAML
- [Segment Commands](/treasure-code/commands/segment) - Manage child segments as YAML
- [Journey Commands](/treasure-code/commands/journey) - Manage journeys as YAML
- [TD Skills](/treasure-code/guide/td-skills) - AI skills for CDP management