Systems Library / Marketing Automation / How to Create Behavior-Triggered Email Automations
Marketing Automation email marketing

How to Create Behavior-Triggered Email Automations

Send targeted emails automatically based on website and app behavior.

Jay Banlasan

Jay Banlasan

The AI Systems Guy

I set up this behavior triggered email automation system to send targeted emails based on what people do on your site. Page visits, clicks, video watches. Every action becomes a trigger.

Behavior-triggered emails outperform scheduled sends because they reach people when engagement is highest.

What You Need Before Starting

Step 1: Set Up Event Tracking

// Track user behavior
function trackEvent(name, props) {
    fetch('/api/track', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({event: name, properties: props, email: getUserEmail(), ts: new Date().toISOString()})
    });
}
trackEvent('viewed_pricing', {plan: 'pro'});

Step 2: Build Webhook Handler

from flask import Flask, request

app = Flask(__name__)

RULES = {
    "viewed_pricing": {"delay": 30, "type": "pricing_followup"},
    "started_checkout": {"delay": 60, "type": "checkout_nudge"},
    "watched_demo": {"delay": 15, "type": "demo_followup"},
}

@app.route("/api/track", methods=["POST"])
def track():
    data = request.json
    event = data.get("event")
    if event in RULES:
        queue_email(data["email"], RULES[event]["type"], RULES[event]["delay"])
    return {"ok": True}

Step 3: Generate Context-Aware Emails

import anthropic
from dotenv import load_dotenv

load_dotenv()
client = anthropic.Anthropic()

def generate_triggered_email(email_type, event_data):
    message = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=512,
        system="Write triggered email based on behavior. Helpful, not creepy.",
        messages=[{"role": "user", "content": f"Type: {email_type}. Action: {event_data}. Write subject + body."}]
    )
    return message.content[0].text

Step 4: Queue and Send

import sqlite3
from datetime import datetime, timedelta

db = sqlite3.connect("triggers.db")

def queue_email(email, email_type, delay_min):
    send_at = datetime.now() + timedelta(minutes=delay_min)
    db.execute("INSERT INTO queue (email, type, send_at, status) VALUES (?,?,?,?)",
        (email, email_type, send_at.isoformat(), "pending"))
    db.commit()

What to Build Next

Add suppression rules. If someone already purchased, skip the checkout nudge. Layer in purchase data to prevent awkward emails.

Related Reading

Want this system built for your business?

Get a free assessment. We will map every system your business needs and show you the ROI.

Get Your Free Assessment

Related Systems