$npx -y skills add 40RTY-ai/shopify-admin-skills --skill shopify-admin-churn-risk-scorerRead-only: scores customers by churn probability based on purchase recency, frequency decay, and expected repurchase intervals.
| 1 | ## Purpose |
| 2 | Predicts which customers are at risk of churning by analyzing their purchase patterns against their historical buying frequency. Calculates an expected next-purchase date for each repeat customer, then scores churn risk based on how overdue they are. Read-only — no mutations. |
| 3 | |
| 4 | ## Prerequisites |
| 5 | - Authenticated Shopify CLI session: `shopify store auth --store <domain> --scopes read_orders,read_customers` |
| 6 | - API scopes: `read_orders`, `read_customers` |
| 7 | |
| 8 | ## Parameters |
| 9 | |
| 10 | | Parameter | Type | Required | Default | Description | |
| 11 | |-----------|------|----------|---------|-------------| |
| 12 | | store | string | yes | — | Store domain | |
| 13 | | days_back | integer | no | 365 | Historical window for purchase pattern analysis | |
| 14 | | min_orders | integer | no | 2 | Minimum orders to calculate purchase interval (need 2+ for frequency) | |
| 15 | | risk_threshold | float | no | 1.5 | Multiplier of avg purchase interval before flagging as at-risk | |
| 16 | | format | string | no | human | Output format: `human` or `json` | |
| 17 | |
| 18 | ## Safety |
| 19 | |
| 20 | > ℹ️ Read-only skill — no mutations are executed. Safe to run at any time. |
| 21 | |
| 22 | ## Churn Risk Scoring Model |
| 23 | |
| 24 | For each customer with `min_orders` or more purchases: |
| 25 | |
| 26 | 1. **Average Purchase Interval (API)** = total days between first and last order / (order_count - 1) |
| 27 | 2. **Days Since Last Order (DSLO)** = today - last_order_date |
| 28 | 3. **Overdue Ratio** = DSLO / API |
| 29 | 4. **Churn Risk Score** (0-100): |
| 30 | - Overdue ratio ≤ 1.0 → Score 0-20 (Active) |
| 31 | - Overdue ratio 1.0–1.5 → Score 20-50 (Cooling) |
| 32 | - Overdue ratio 1.5–2.5 → Score 50-80 (At Risk) |
| 33 | - Overdue ratio > 2.5 → Score 80-100 (Likely Churned) |
| 34 | 5. **Customer Lifetime Value (CLV)** = total spend / customer age in years × expected remaining years |
| 35 | |
| 36 | ## Workflow Steps |
| 37 | |
| 38 | 1. **OPERATION:** `orders` — query |
| 39 | **Inputs:** `query: "created_at:>='<NOW - days_back days>'"`, `first: 250`, select `createdAt`, `totalPriceSet`, `customer { id, email, firstName, lastName }`, pagination cursor |
| 40 | **Expected output:** All orders with customer association |
| 41 | |
| 42 | 2. Group orders by customer, calculate per customer: |
| 43 | - Order dates (sorted chronologically) |
| 44 | - Average purchase interval |
| 45 | - Days since last order |
| 46 | - Total spend |
| 47 | - Order count |
| 48 | |
| 49 | 3. **OPERATION:** `customers` — query (enrichment) |
| 50 | **Inputs:** Customer IDs for at-risk and likely-churned segments |
| 51 | **Expected output:** Contact details, tags, total spend |
| 52 | |
| 53 | 4. Calculate churn risk score and classify into segments |
| 54 | |
| 55 | 5. Estimate revenue at risk = sum of (annual_spend × churn_probability) for at-risk customers |
| 56 | |
| 57 | ## GraphQL Operations |
| 58 | |
| 59 | ```graphql |
| 60 | # orders:query — validated against api_version 2025-01 |
| 61 | query OrdersForChurnAnalysis($query: String!, $after: String) { |
| 62 | orders(first: 250, after: $after, query: $query) { |
| 63 | edges { |
| 64 | node { |
| 65 | createdAt |
| 66 | totalPriceSet { shopMoney { amount currencyCode } } |
| 67 | customer { |
| 68 | id |
| 69 | |
| 70 | firstName |
| 71 | lastName |
| 72 | numberOfOrders |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | pageInfo { hasNextPage endCursor } |
| 77 | } |
| 78 | } |
| 79 | ``` |
| 80 | |
| 81 | ```graphql |
| 82 | # customers:query — validated against api_version 2025-01 |
| 83 | query AtRiskCustomers($ids: [ID!]!) { |
| 84 | nodes(ids: $ids) { |
| 85 | ... on Customer { |
| 86 | id |
| 87 | |
| 88 | firstName |
| 89 | lastName |
| 90 | totalSpentV2 { amount currencyCode } |
| 91 | numberOfOrders |
| 92 | tags |
| 93 | createdAt |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | ## Session Tracking |
| 100 | |
| 101 | **Claude MUST emit the following output at each stage. This is mandatory.** |
| 102 | |
| 103 | **On start**, emit: |
| 104 | ``` |
| 105 | ╔══════════════════════════════════════════════╗ |
| 106 | ║ SKILL: Churn Risk Scorer ║ |
| 107 | ║ Store: <store domain> ║ |
| 108 | ║ Started: <YYYY-MM-DD HH:MM UTC> ║ |
| 109 | ╚══════════════════════════════════════════════╝ |
| 110 | ``` |
| 111 | |
| 112 | **After each step**, emit: |
| 113 | ``` |
| 114 | [N/TOTAL] <QUERY|MUTATION> <OperationName> |
| 115 | → Params: <brief summary of key inputs> |
| 116 | → Result: <count or outcome> |
| 117 | ``` |
| 118 | |
| 119 | **On completion**, emit: |
| 120 | |
| 121 | For `format: human` (default): |
| 122 | ``` |
| 123 | ══════════════════════════════════════════════ |
| 124 | CHURN RISK REPORT (<days_back> days analyzed) |
| 125 | Repeat customers scored: <n> |
| 126 | ───────────────────────────── |
| 127 | Active (score 0-20): <n> (<pct>%) |
| 128 | Cooling (score 20-50): <n> (<pct>%) |
| 129 | At Risk (score 50-80): <n> (<pct>%) ⚠️ |
| 130 | Likely Churned (80-100): <n> (<pct>%) 🔴 |
| 131 | |
| 132 | Revenue at risk: $<amount>/year |
| 133 | |
| 134 | Top at-risk by value: |
| 135 | <name> (<email>) Score: <n> Last order: <date> Lifetime: $<n> |
| 136 | |
| 137 | Output: churn_risk_<date>.csv |
| 138 | ══════════════════════════════════════════════ |
| 139 | ``` |
| 140 | |
| 141 | ## Output Format |
| 142 | CSV file `churn_risk_<YYYY-MM-DD>.csv` with columns: |
| 143 | `customer_id`, `email`, `first_name`, |