Skip to content

Dynamic Segments

Segments (campaign-segment) are dynamic, rule-driven queries evaluated at dispatch time by SegmentEvaluator. Instead of manually maintaining static sub-lists, define reusable audience criteria once and attach the segment to any sendout.

Segments combine an optional base Mailing List with AND/OR rules evaluated live against Contacts and campaign SQL tables.

Each campaign-segment entry defines:

  • title & handle: Human name and machine handle (for example, aegean-vip).
  • match_mode: all (Match ALL rules / logical AND) or any (Match ANY rule / logical OR).
  • list_id (optional): Scopes evaluation to active subscribers of a specific mailing list before applying rules.
  • rules: Repeatable rule rows, each containing rule_source, rule_field, rule_operator, and rule_value.

SegmentEvaluator supports three rule sources:

Evaluates fields on the Contacts record:

  • Core identity: primary_email (or email), first_name, last_name, title, company_name (or organization)
  • Locale & geography: country_code (or country), preferred_language (or language), timezone
  • Consent & taxonomy: marketing_opt_in, tags, roles, source, created
  • Nested domain metadata: domain_meta.<key> (for example, domain_meta.charter_tier or domain_meta.preferred_marina)
  • Custom fields: Any additional custom field stored on the Contacts blueprint.

Evaluates rows in campaign_subscriptions:

  • in_list: Contact is actively subscribed to the target list handle or ID.
  • not_in_list: Contact is not subscribed to the target list.
  • subscription_status: Matches pending, subscribed, unsubscribed, bounced, or complained.
  • subscribed_at: Compares the subscription confirmation timestamp (before, after, within_days).

Evaluates delivery and tracking history in campaign_events and campaign_sendout_recipients:

  • opened_campaign: Contact opened the specified campaign handle or ID.
  • not_opened_campaign: Contact received or did not open the specified campaign.
  • clicked_campaign: Contact clicked any tracked link in the specified campaign.
  • clicked_link: Contact clicked a specific destination URL (supports equals or contains).
  • opened_within_days: Contact opened any campaign within the last N days.
  • received_sendout: Contact was targeted by a specific sendout UUID.

SegmentEvaluator implements 16 type-aware operators:

Category Operators Notes
Equality equals, not_equals Case-insensitive string comparison; supports booleans (1/0, true/false).
Substring contains, not_contains, starts_with, ends_with Works on strings and multi-value arrays such as tags or roles.
Numeric gt, gte, lt, lte Compares numeric values (e.g., booking counts or loyalty scores).
Presence is_empty, is_not_empty Checks whether a field is empty, null, or missing.
Set Membership in, not_in Matches comma-separated values (for example, DE,CH,AT for country_code).
Date & Recency before, after, within_days Parses ISO-8601 dates or Unix timestamps; within_days checks rolling windows.

Use Campaign::segments() (SegmentEvaluator) to count or preview matching contacts programmatically:

<?php
declare(strict_types=1);
namespace ProcessWire;
$campaign = \Totoglu\Campaign\Campaign::requireInstance($wire);
$segments = $campaign->segments();
$segment = $wire->entries->findOne('type=campaign-segment, handle=aegean-vip');
if ($segment !== null) {
$total = $segments->count($segment);
$matches = $segments->evaluate($segment, limit: 50, offset: 0);
foreach ($matches as $contact) {
$email = (string) $contact->get('primary_email');
// Inspect or preview matched contact
}
}

Next steps