Win back lapsed customers

A weekly automation that finds customers who have not ordered for 90 days and tags and emails each one in a loop. A validated definition using fetch and for_each on the clock.

Every Monday, find customers who agreed to marketing and have not ordered in 90 days, tag each one and send them a library email. Trigger: schedule.fired@1. Steps: fetch (customers), for_each with add_customer_tags@1 and send_customer_email@1.

Model background: Automations.

The definition

{
  "schemaVersion": 1,
  "trigger": {
    "type": "schedule.fired@1",
    "origins": ["SYSTEM"],
    "schedule": {
      "frequency": "weekly",
      "weekday": 1,
      "time": "10:00"
    }
  },
  "steps": [
    {
      "id": "f1",
      "type": "fetch",
      "source": "customers",
      "where": {
        "lastOrderOlderThanDays": 90,
        "acceptsMarketing": true
      },
      "limit": 50
    },
    {
      "id": "l1",
      "type": "for_each",
      "items": {
        "$ref": "steps.f1.ids"
      },
      "do": [
        {
          "id": "tag",
          "type": "action",
          "contract": "add_customer_tags@1",
          "input": {
            "customerId": {
              "$ref": "loop.item"
            },
            "tagIds": ["TAG_ID_WIN_BACK"]
          }
        },
        {
          "id": "mail",
          "type": "action",
          "contract": "send_customer_email@1",
          "input": {
            "customerId": {
              "$ref": "loop.item"
            },
            "templateId": "TEMPLATE_ID",
            "kind": "marketing",
            "frequencyDays": 60
          }
        }
      ]
    }
  ]
}

Replace before saving:

  • TAG_ID_WIN_BACK: the id of an existing customer tag.
  • TEMPLATE_ID: a template in the store's campaign library.
  • lastOrderOlderThanDays (1 to 365), limit (1 to 200) and the schedule (weekday 0 is Sunday, time is in the store's time zone).

How it works

  • A customers fetch takes where of lastOrderOlderThanDays (a customer with no order never matches), signedUpOlderThanDays, tagIds (1 to 12) and acceptsMarketing, and needs at least one. It publishes steps.f1.ids, .count and .truncated but no .numbers. It returns ids, never a name, email or phone, and leaves out erased customers.
  • for_each runs its do list once per fetched id: at most 5 actions, at most 200 items, one loop per definition, clock only. Inside it the customer is always { "$ref": "loop.item" }. For customers the allowed loop actions are add_customer_tags@1, remove_customer_tags@1, send_customer_email@1 (kind marketing) and create_customer_coupon@1. A wait, a fetch, a condition, another loop, a notice, a team email, an HTTP call, cancel_order@1 and mark_order_paid@1 are never allowed inside.
  • acceptsMarketing: true narrows the list, but consent is checked again at the moment each email is sent.

How the loop ends

  • An item that no longer matches the fetch when its turn comes is skipped.
  • A customer without marketing consent, with no address, suppressed, or over the frequency cap is skipped and the loop goes on with the next one.
  • A missing template or switched-off emails stops the loop at the first item and the run ends SKIPPED.
  • A permanent failure fails the run. The items already done stay done.
  • A loop run cannot be retried. Retrying would reach the same people again. The next scheduled run starts from a fresh list.

What it does not do

  • It will pick the same customers next Monday, because they still have not ordered. frequencyDays: 60 is what stops a second email: those customers are skipped with FREQUENCY_CAP until 60 days have passed. The tag is added again harmlessly.
  • It reaches at most limit customers per run (200 at most). When steps.f1.truncated is true, the rest wait for a later run.
  • It does not put a discount code in the email. A personal code is create_customer_coupon@1 in the loop (percent or fixed, expiresInDays 1 to 365), and a marketing email may print {{coupon.code}}: add that step before the email, and read the guide topic http-and-reports.
  • It does not email without the store's postal address (NO_POSTAL_ADDRESS).

Create it

With the Admin MCP:

  1. get_automation_guide with topic: "clock".
  2. validate_automation_definition. Fix every finding.
  3. test_automation with entity: { "type": "Store", "id": "<the store id>" }. It reads the list right now and shows ONE looping step: how many customers it would reach, per action how many would change or be skipped and why, where the run would stop, and at most 10 ids. It writes and sends nothing.
  4. create_automation_draft with name: "Win back lapsed customers".
  5. Tell the merchant to approve it in the dashboard.

With the SDK:

const check = await admin.validateAutomationDefinition(storeId, { definition });
if (!check.ok) throw new Error(JSON.stringify(check.errors));

const draft = await admin.createAutomation(storeId, {
  name: 'Win back lapsed customers',
  definition,
});

What the merchant sees

A draft in Settings, then Automations. Once on, one run a week. Its detail groups the per-item results by action, status and code (with a sample of at most 20 skipped or failed items), for example (illustrative numbers) "send_customer_email: 31 sent, 12 skipped FREQUENCY_CAP, 3 skipped NO_MARKETING_CONSENT". It never lists names or addresses.