{
  "id": "BKdO3J1idnWv0mFP",
  "name": "Customer Journey Predictor & Drop-off Analyzer",
  "tags": [],
  "nodes": [
    {
      "id": "d512ed68-a791-45c6-9fb7-5526a9836062",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -288,
        -112
      ],
      "parameters": {
        "width": 920,
        "height": 848,
        "content": "## Customer Journey Predictor & Drop-off Analyzer\n\nThis workflow automatically ingests real-time user behavior events, detects drop-off points across the customer journey, predicts churn risk using AI, and triggers targeted retention actions while logging everything for analysis.\n\n### Who\u2019s it for\n\u2022 Product teams managing high-churn SaaS products\n\u2022 E-commerce businesses with cart abandonment issues\n\u2022 Subscription services tracking user engagement\n\n### How it works / What it does\n1. Captures new user behavior events (webhook or scheduled poll)\n2. Analyzes session events, actions, and engagement metrics for drop-off signals\n3. Loads user profile, history, and preferences\n4. AI predicts real-time drop-off risk and generates personalized retention actions\n5. Sends automated re-engagement messages or campaign triggers\n6. Logs predictions, risk scores, and actions in Google Sheets\n\n### How to set up\n1. Import this workflow\n2. Set up credentials (Webhook events, Google Sheets, OpenAI/Anthropic)\n3. Update user profile defaults and retention endpoints\n4. Activate workflow\n\n### Requirements\n\u2022 Event webhook (Segment, Mixpanel, custom analytics)\n\u2022 Google Sheets\n\u2022 OpenAI / Anthropic / Grok API\n\u2022 User behavior event schema\n\n### How to customize the workflow\n\u2022 Change AI tone and action templates in the AI node\n\u2022 Modify Python detection logic\n\u2022 Update Google Sheet columns\n\u2022 Adjust retention messaging or campaign endpoints"
      },
      "typeVersion": 1
    },
    {
      "id": "9682585f-c395-4a71-83ae-18e7e1453d0d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        752,
        144
      ],
      "parameters": {
        "color": 4,
        "width": 732,
        "height": 480,
        "content": "## 1. Trigger & Intake"
      },
      "typeVersion": 1
    },
    {
      "id": "d26bfc54-de30-4251-b509-437444caf129",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1520,
        48
      ],
      "parameters": {
        "color": 4,
        "width": 780,
        "height": 732,
        "content": "## 2. Analysis & Prediction"
      },
      "typeVersion": 1
    },
    {
      "id": "64fff04e-d20c-4ab0-8a5c-5c32be980afd",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2320,
        112
      ],
      "parameters": {
        "color": 4,
        "width": 944,
        "height": 580,
        "content": "## 3. Action & Track"
      },
      "typeVersion": 1
    },
    {
      "id": "85f8f508-82b8-4f66-9c30-1792769252f2",
      "name": "Webhook - New User Behavior Event",
      "type": "n8n-nodes-base.webhook",
      "position": [
        912,
        272
      ],
      "parameters": {
        "path": "customer-journey-event",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 1.1
    },
    {
      "id": "9d152fdb-ad64-4887-95b7-c14dd8e24e03",
      "name": "Poll New Behavior Events",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        912,
        464
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/20 * * * *"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "c32e9b4b-4056-4b98-80a4-23f4e5ff1b9c",
      "name": "Prepare Event Context",
      "type": "n8n-nodes-base.set",
      "position": [
        1136,
        368
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "name": "userId",
              "type": "string",
              "value": "={{ $json.userId || $json.body?.userId }}"
            },
            {
              "name": "eventType",
              "type": "string",
              "value": "={{ $json.eventType || $json.body?.eventType }}"
            },
            {
              "name": "sessionData",
              "type": "string",
              "value": "={{ $json.sessionData || $json.body?.sessionData || $json.body }}"
            },
            {
              "name": "metrics",
              "type": "string",
              "value": "={{ $json.metrics || $json.body?.metrics }}"
            },
            {
              "name": "userEmail",
              "type": "string",
              "value": "={{ $json.userEmail || $json.body?.userEmail }}"
            },
            {
              "name": "userProfile",
              "type": "string",
              "value": "={{ $json.userProfile || $json.body?.userProfile || 'High-value user with frequent logins but recent drop in engagement...' }}"
            },
            {
              "name": "preferences",
              "type": "string",
              "value": "={{ $json.preferences || $json.body?.preferences || 'Prefers personalized discounts, email nudges, in-app messages, loyalty rewards' }}"
            },
            {
              "name": "eventId",
              "type": "string",
              "value": "={{ $json.eventId || Date.now().toString() }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "0dfb436a-e44e-4b11-8c51-b8e02bfdcff5",
      "name": "Python - Detect Drop-off Signals",
      "type": "n8n-nodes-base.code",
      "position": [
        1360,
        368
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "language": "python",
        "pythonCode": "newItems = []\nfor item in items:\n    data = item['json'].copy()\n    event_type = data.get('eventType', '')\n    engagement_score = data.get('metrics', {}).get('engagementScore', 0) if isinstance(data.get('metrics'), dict) else 0\n    is_dropoff_related = event_type in ['abandon_cart', 'page_exit', 'checkout_fail', 'session_timeout', 'low_engagement']\n    risk_score = 0.9 if is_dropoff_related else max(0.2, 1 - (engagement_score / 100))\n    dropoff_stage = 'checkout' if 'cart' in event_type.lower() else 'onboarding' if 'signup' in event_type.lower() else 'engagement' if engagement_score < 40 else 'unknown'\n    data['isDropoffRelated'] = is_dropoff_related\n    data['riskScore'] = round(risk_score, 2)\n    data['detectedDropoffStage'] = dropoff_stage\n    newItems.append({'json': data})\nreturn newItems"
      },
      "typeVersion": 2
    },
    {
      "id": "34717108-ae7e-4396-96d8-e47f48942aab",
      "name": "Filter High-Risk Drop-offs",
      "type": "n8n-nodes-base.filter",
      "position": [
        1584,
        368
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "conditions": [
            {
              "operator": {
                "type": "boolean",
                "operation": "true"
              },
              "leftValue": "={{ $json.isDropoffRelated }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "ed07a581-72b2-4939-be2d-0c9cad9dc0e9",
      "name": "AI - Predict Drop-off Risk & Recommend Actions",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1792,
        368
      ],
      "parameters": {
        "text": "=Act as an expert customer retention strategist and journey predictor. Analyze the user behavior data in real time and generate a targeted retention recommendation.\n\nUser Profile:\n{{ $json.userProfile }}\n\nPreferences:\n{{ $json.preferences }}\n\nBehavior Event:\nEvent Type: {{ $json.eventType }}\nMetrics: {{ $json.metrics }}\nSession Data: {{ $json.sessionData }}\n\nPredict the exact drop-off point, confirm/assign risk score (0.0-1.0), and recommend 1-2 immediate retention actions (personalized offer, in-app nudge, email campaign, or support outreach). Keep the full output concise, actionable, and under 170 words.",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 1.6
    },
    {
      "id": "a7235764-2d49-48e7-82d4-be3d1eb245a6",
      "name": "JS - Format Recommendation",
      "type": "n8n-nodes-base.code",
      "position": [
        2592,
        368
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const item = $input.item.json;\nreturn {\n  json: {\n    ...item,\n    recommendationText: item.response || item.text || 'Thank you for your recent activity. Here is a special offer to continue your journey!',\n    status: 'ActionRecommended',\n    analyzedDate: new Date().toISOString().split('T')[0],\n    riskScore: item.riskScore || 0.5,\n    dropoffPoint: item.detectedDropoffStage || 'unknown'\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "fc159ca5-8ecb-4cca-a64a-12b7b77aeea7",
      "name": "Send Personalized Retention Message",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        3040,
        272
      ],
      "parameters": {
        "url": "https://api.sendgrid.com/v3/mail/send",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"personalizations\": [{\"to\": [{\"email\": \"{{ $json.userEmail }}\"}]}],\n  \"from\": {\"email\": \"retention@yourcompany.com\", \"name\": \"Customer Success Team\"},\n  \"subject\": \"We noticed your recent activity \u2013 here's something just for you!\",\n  \"content\": [{\"type\": \"text/plain\", \"value\": \"{{ $json.recommendationText }}\"}]\n}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "4d437d75-5e6f-4ab6-a5bd-a34c821ec9e2",
      "name": "Update Retention Tracker",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        3040,
        464
      ],
      "parameters": {
        "url": "https://sheets.googleapis.com/v4/spreadsheets/YOUR_SHEET_ID/values/Sheet1!A1:append?valueInputOption=USER_ENTERED",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"values\": [[\"{{ $json.analyzedDate }}\", \"{{ $json.userId }}\", \"{{ $json.dropoffPoint }}\", \"{{ $json.riskScore }}\", \"{{ $json.recommendationText }}\", \"ActionRecommended\", \"{{ new Date().toISOString() }}\"]]\n}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "4601aca8-a13c-4688-95cf-9b25bc0bf09c",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1904,
        576
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "16f0694d-b092-4748-a79f-1af856b84949",
      "name": "Wait For Reply",
      "type": "n8n-nodes-base.wait",
      "position": [
        2144,
        368
      ],
      "parameters": {},
      "typeVersion": 1.1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "8fa0b4e8-2024-42bd-a06c-0fe6abef1495",
  "connections": {
    "Wait For Reply": {
      "main": [
        [
          {
            "node": "JS - Format Recommendation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI - Predict Drop-off Risk & Recommend Actions",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Event Context": {
      "main": [
        [
          {
            "node": "Python - Detect Drop-off Signals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Poll New Behavior Events": {
      "main": [
        [
          {
            "node": "Prepare Event Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter High-Risk Drop-offs": {
      "main": [
        [
          {
            "node": "AI - Predict Drop-off Risk & Recommend Actions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "JS - Format Recommendation": {
      "main": [
        [
          {
            "node": "Send Personalized Retention Message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Update Retention Tracker",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Python - Detect Drop-off Signals": {
      "main": [
        [
          {
            "node": "Filter High-Risk Drop-offs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - New User Behavior Event": {
      "main": [
        [
          {
            "node": "Prepare Event Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI - Predict Drop-off Risk & Recommend Actions": {
      "main": [
        [
          {
            "node": "Wait For Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}