{
  "id": "FXoZ1pea8oPRdkwt",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Behavior-Based Email Marketing Automation",
  "tags": [],
  "nodes": [
    {
      "id": "938f1853-3177-4516-baa0-6e692331e664",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        240,
        -32
      ],
      "parameters": {
        "width": 980,
        "height": 696,
        "content": "## Behavior-Based Email Marketing Automation\n\nThis workflow tracks user website behaviors and email engagements, segments users into behavioral cohorts, generates highly personalized email campaigns, and sends them with smart timing and tracking.\n\n### Who\u2019s it for\n\u2022 E-commerce & SaaS marketers\n\u2022 Growth teams running behavioral campaigns\n\u2022 Companies with high website traffic and email lists\n\n### How it works\n1. Captures website events & email interactions via webhooks\n2. Enriches & segments user behavior\n3. Generates personalized email content\n4. Sends targeted campaigns with optimal timing\n5. Logs outcomes and updates user profiles\n\n### Setup\n1. Import workflow\n2. Configure credentials (Webhooks, SendGrid/Mailgun, Google Sheets/Airtable, AI)\n3. Update segmentation rules & email templates\n4. Connect tracking pixels & UTM parameters\n\n### Requirements\n\u2022 Website event webhooks (GA4, custom pixel, Segment.com)\n\u2022 Email service with webhook support\n\u2022 User database / CRM sync\n\u2022 AI model for personalization"
      },
      "typeVersion": 1
    },
    {
      "id": "af37238b-8a61-4af2-a4c3-184411465ea6",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1312,
        16
      ],
      "parameters": {
        "color": 4,
        "width": 884,
        "height": 520,
        "content": "## 1. Event Triggers & Intake"
      },
      "typeVersion": 1
    },
    {
      "id": "f34c8da4-dbca-4090-9ac0-f6a9dca69933",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2288,
        -32
      ],
      "parameters": {
        "color": 3,
        "width": 908,
        "height": 680,
        "content": "## 2. Behavior Analysis & Segmentation"
      },
      "typeVersion": 1
    },
    {
      "id": "9289ac00-5a05-4275-baac-c9e7d8ed6b68",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3280,
        16
      ],
      "parameters": {
        "color": 5,
        "width": 588,
        "height": 640,
        "content": "## 3. Personalization & Campaign Execution"
      },
      "typeVersion": 1
    },
    {
      "id": "d8bf51ee-d607-4c95-adcd-3d836b3aba81",
      "name": "Webhook - Website & Email Events",
      "type": "n8n-nodes-base.webhook",
      "position": [
        1376,
        128
      ],
      "parameters": {
        "path": "behavior-event-webhook",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 1.1
    },
    {
      "id": "190ca807-0885-4f66-9293-b898ba5b683a",
      "name": "Daily Behavior Digest",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        1376,
        320
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/15 * * * *"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8bd9a0c2-c084-4b06-b03c-ffbf6e257d4b",
      "name": "Prepare Event Context",
      "type": "n8n-nodes-base.set",
      "position": [
        1632,
        224
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "name": "userId",
              "type": "string",
              "value": "={{ $json.userId || $json.body?.userId || $json.email }}"
            },
            {
              "name": "eventType",
              "type": "string",
              "value": "={{ $json.event || $json.body?.event }}"
            },
            {
              "name": "pageUrl",
              "type": "string",
              "value": "={{ $json.pageUrl || $json.body?.pageUrl }}"
            },
            {
              "name": "productViewed",
              "type": "string",
              "value": "={{ $json.product || $json.body?.product }}"
            },
            {
              "name": "emailEngagement",
              "type": "string",
              "value": "={{ $json.action || $json.body?.action }}"
            },
            {
              "name": "timestamp",
              "type": "string",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "29fdf0a7-c31c-4640-b3d0-f4bb94f52f33",
      "name": "Python - Segment Behavior",
      "type": "n8n-nodes-base.code",
      "position": [
        2016,
        224
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "language": "python"
      },
      "typeVersion": 2
    },
    {
      "id": "e64a29ed-fe9d-4332-8835-1bde2932b7ad",
      "name": "Filter High Intent",
      "type": "n8n-nodes-base.filter",
      "position": [
        2336,
        128
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "conditions": [
            {
              "value1": "={{ $json.segment }}",
              "value2": "high_intent",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "b91c8550-7ff7-4d51-a425-6d1787ceadc9",
      "name": "Filter Abandoned Cart",
      "type": "n8n-nodes-base.filter",
      "position": [
        2336,
        288
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "conditions": [
            {
              "value1": "={{ $json.segment }}",
              "value2": "abandoned_cart",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "d85911f9-33cc-4034-8a63-e82b96ba4d20",
      "name": "Filter Re-engagement",
      "type": "n8n-nodes-base.filter",
      "position": [
        2336,
        464
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "conditions": [
            {
              "value1": "={{ $json.segment }}",
              "value2": "re_engagement",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "3e1fd1b1-b7e1-4681-90b5-4f92a2ef9ad8",
      "name": "Wait - Rate Limit",
      "type": "n8n-nodes-base.wait",
      "position": [
        2576,
        224
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "cbcd1da7-17ee-4792-a3bb-d4ed65854843",
      "name": "AI - Generate Personalized Email",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        2752,
        224
      ],
      "parameters": {
        "text": "=You are an expert email marketing copywriter. Create a highly personalized, persuasive email based on the user's behavior.\n\nUser Data:\n{{ $json.userProfile || 'Name: {{ $json.userId }}' }}\n\nBehavior:\nEvent: {{ $json.eventType }}\nProduct: {{ $json.productViewed }}\nPage: {{ $json.pageUrl }}\n\nSegment: {{ $json.segment }}\n\nWrite a compelling subject line and email body under 180 words. Use friendly, brand-aligned tone. Include clear CTA.",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 1.6
    },
    {
      "id": "437c03e5-8d66-4400-967e-f11ae167acef",
      "name": "JS - Format Campaign",
      "type": "n8n-nodes-base.code",
      "position": [
        3056,
        224
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const item = $input.item.json;\nreturn {\n  json: {\n    ...item,\n    subject: item.subject || `Great to see you interested in ${item.productViewed || 'our products'}!`,\n    emailBody: item.response || item.text || 'Default personalized content...',\n    campaignType: item.segment,\n    sendTime: new Date(Date.now() + 3600000).toISOString()\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "b94f327e-55f5-4982-b931-f9cf1c31aca2",
      "name": "Wait - Send Buffer",
      "type": "n8n-nodes-base.wait",
      "position": [
        3328,
        224
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "3bbb3349-db1a-4b6c-96d6-b03b0b77c864",
      "name": "Send Personalized Email",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        3568,
        128
      ],
      "parameters": {
        "url": "https://api.sendgrid.com/v3/mail/send",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"personalizations\": [{\n    \"to\": [{\"email\": \"{{ $json.userId }}\"}],\n    \"dynamic_template_data\": {\n      \"subject\": \"{{ $json.subject }}\",\n      \"body\": \"{{ $json.emailBody }}\"\n    }\n  }],\n  \"from\": {\"email\": \"marketing@yourbrand.com\", \"name\": \"Your Brand\"},\n  \"template_id\": \"d-1234567890abcdef\"\n}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "d49d4e72-828c-4aa6-b289-6d4f7b5ecb39",
      "name": "Log Campaign to Sheet",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        3568,
        336
      ],
      "parameters": {
        "url": "https://sheets.googleapis.com/v4/spreadsheets/YOUR_CAMPAIGN_SHEET_ID/values/Behaviors!A1:append?valueInputOption=USER_ENTERED",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"values\": [[\"{{ $json.timestamp }}\", \"{{ $json.userId }}\", \"{{ $json.eventType }}\", \"{{ $json.segment }}\", \"Sent\", \"{{ $json.campaignType }}\", \"{{ new Date().toISOString() }}\"]]\n}",
        "sendBody": true,
        "specifyBody": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "92bdb58b-c207-4318-8052-b591067dedfe",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        2656,
        416
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "dc0a5821-f329-45fc-af64-17abe4b40e6d",
      "name": "Wait For Result",
      "type": "n8n-nodes-base.wait",
      "position": [
        1840,
        224
      ],
      "parameters": {},
      "typeVersion": 1.1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "560944e0-f20f-4b87-adb1-844b281a3e63",
  "connections": {
    "Wait For Result": {
      "main": [
        [
          {
            "node": "Python - Segment Behavior",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI - Generate Personalized Email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Wait - Rate Limit": {
      "main": [
        [
          {
            "node": "AI - Generate Personalized Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter High Intent": {
      "main": [
        [
          {
            "node": "Wait - Rate Limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait - Send Buffer": {
      "main": [
        [
          {
            "node": "Send Personalized Email",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log Campaign to Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Re-engagement": {
      "main": [
        [
          {
            "node": "Wait - Rate Limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "JS - Format Campaign": {
      "main": [
        [
          {
            "node": "Wait - Send Buffer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Behavior Digest": {
      "main": [
        [
          {
            "node": "Prepare Event Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Abandoned Cart": {
      "main": [
        [
          {
            "node": "Wait - Rate Limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Event Context": {
      "main": [
        [
          {
            "node": "Wait For Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Python - Segment Behavior": {
      "main": [
        [
          {
            "node": "Filter High Intent",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter Abandoned Cart",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter Re-engagement",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI - Generate Personalized Email": {
      "main": [
        [
          {
            "node": "JS - Format Campaign",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Website & Email Events": {
      "main": [
        [
          {
            "node": "Prepare Event Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}