Skip to content

Lead Intelligence Agent

The Lead Intelligence Agent delivers actionable insights for SDRs & AEs to close leads with better efficiency. It aggregates visitor behavior, conversation history, and intent signals into a comprehensive intelligence report.

Status

Beta - Available via n8n integration

Overview

The Lead Intelligence Agent provides detailed insights about individual visitors who have engaged with your website. It helps sales teams understand:

  • What pages the visitor explored
  • How engaged they were (session duration, page views)
  • What topics they discussed with the Website Agent
  • Their position in the conversion funnel
  • Their calculated intent score

Data Access

Supabase RPC Function

The agent retrieves data via the get_visitor_for_lead_intelligence RPC function.

Parameters

Parameter Type Required Description
p_site_domain TEXT Yes Your site domain (e.g., "demo.userose.ai")
p_person_id TEXT Yes PostHog person_id (visitor identifier)

Example Call

SELECT * FROM get_visitor_for_lead_intelligence('demo.userose.ai', 'posthog-person-id-123');

n8n Integration

Prerequisites

  1. Database credentials - Get these from your Rose admin:
  2. PostgreSQL host: db.xxx.supabase.co
  3. Database: postgres
  4. User: n8n_service (dedicated read-only user)
  5. Password: Provided by admin
  6. Port: 5432

!!! warning "Do not use Service Role Key" The Service Role Key has full database access. Always use the dedicated n8n_service user which only has execute permission on these specific RPC functions.

  1. n8n PostgreSQL credential - In n8n, go to CredentialsAdd CredentialPostgres and enter the connection details above.

Getting the Person ID

The person_id is the PostHog identifier for the visitor. You can obtain it from:

  • PostHog webhook: When a visitor triggers an event, PostHog sends the distinct_id (same as person_id)
  • Rose webhook: Configure a webhook in Rose to receive visitor events with the person_id
  • Supabase query: Query the visitors table to find visitors by email or other criteria

Step-by-Step Workflow Setup

[PostHog Webhook] → [Supabase RPC] → [Send to Slack/Email/CRM]
  1. Add Webhook node as trigger
  2. Copy the webhook URL and add it to PostHog as a destination
  3. Configure PostHog to send events like lead_qualified or demo_booked

  4. Add Postgres node

  5. Credential: Select your n8n_service Postgres credential
  6. Operation: Execute Query
  7. Query:

    SELECT * FROM get_visitor_for_lead_intelligence(
      '{{ $json.properties.$current_url.replace("https://", "").split("/")[0] }}',
      '{{ $json.distinct_id }}'
    );
    

  8. Add output node (Slack, Email, CRM, etc.)

  9. Use the response fields to craft your message

Option 2: Scheduled Batch Processing

[Schedule Trigger] → [Postgres Query] → [Loop] → [Postgres RPC] → [Output]
  1. Schedule Trigger: Run daily/hourly
  2. Postgres node: Query visitors table for recent high-intent visitors
  3. Loop: Iterate through each visitor
  4. Postgres node: Call get_visitor_for_lead_intelligence for each

Postgres Node Configuration

-- n8n Postgres node query
SELECT * FROM get_visitor_for_lead_intelligence(
  '{{ $json.site_domain }}',
  '{{ $json.person_id }}'
);

Example: Slack Notification for High-Intent Leads

Complete workflow that sends a Slack message when a visitor has intent score ≥ 4:

[Webhook] → [Supabase RPC] → [IF intent_score >= 4] → [Slack Message]

Slack message template:

🔥 High-Intent Lead Alert

*Email:* {{ $json.email || 'Anonymous' }}
*Intent Score:* {{ $json.intent_score }}/5
*Funnel Stage:* {{ $json.funnel_stage || 'Browsing' }}
*Sessions:* {{ $json.total_sessions }}
*Time on Site:* {{ Math.round($json.total_session_duration_seconds / 60) }} minutes

*Topics Discussed:*
{{ $json.topics.join(', ') }}

*Pages Visited:*
{{ $json.pages_visited.map(p => '• ' + p.title).join('\n') }}

Response Fields

Field Type Description
visitor_id UUID Unique visitor identifier
email TEXT Visitor's email (if captured)
pages_visited JSONB Array of pages with URL and title (see below)
total_page_views BIGINT Total number of page views
total_session_duration_seconds BIGINT Total time spent on site (seconds)
total_sessions BIGINT Number of separate browsing sessions
funnel_stage TEXT Current conversion stage (see below)
conversations JSONB Full conversation transcripts (see below)
topics TEXT[] Topics discussed across all conversations
keywords TEXT[] Keywords extracted from conversations
last_activity_at TIMESTAMPTZ Most recent activity timestamp
total_messages BIGINT Total message count across conversations
intent_score INTEGER Calculated intent (1-5 scale)

Funnel Stages

Stage Description Trigger
Conversion completed Visitor booked a demo demo_booked = true
Conversion initiated Visitor clicked CTA cta_clicked = true
Conversion offered Demo was proposed to visitor demo_proposed = true
null Not yet in conversion funnel No conversion signals

Intent Score

The intent score (1-5) is calculated using:

  • 60% Behavior signals: Based on funnel progression
  • 5 = Demo booked
  • 4 = CTA clicked
  • 3 = Demo proposed
  • 2 = 2+ message turns
  • 1 = Anonymous/minimal engagement

  • 40% Interest signals: Based on conversation engagement (lifetime_interest_score)

Pages Visited JSONB Structure

[
  {
    "url": "/pricing",
    "title": "Pricing - Rose AI"
  },
  {
    "url": "/features/chatbot",
    "title": "AI Chatbot Features"
  }
]

Conversations JSONB Structure

[
  {
    "id": "conversation-uuid",
    "started_at": "2024-01-15T10:30:00Z",
    "topics": ["pricing", "enterprise"],
    "keywords": ["API", "integration"],
    "messages": [
      {
        "role": "user",
        "content": "What's your enterprise pricing?",
        "created_at": "2024-01-15T10:30:00Z"
      },
      {
        "role": "assistant",
        "content": "Our enterprise plans start at...",
        "created_at": "2024-01-15T10:30:05Z"
      }
    ]
  }
]

Security

Webhook Authentication Required

If your n8n workflow is triggered by a webhook, you MUST add authentication to prevent unauthorized access. Without authentication, anyone who discovers your webhook URL can trigger the workflow and access your data.

Options:

  • Header Auth: Require a secret token in Authorization header
  • Query Param Auth: Require a secret in the URL (less secure)
  • IP Allowlist: Restrict to known IPs (e.g., PostHog servers)

Read-Only Access

The RPC functions are read-only (STABLE in PostgreSQL terms). They cannot modify any data.

Tenant Isolation

The p_site_domain parameter ensures data isolation:

  • Each call is scoped to a specific site domain
  • Users can only access data for sites they own
  • No cross-tenant data access is possible

Supabase Role Configuration

We use a dedicated n8n_readonly role with minimal permissions. This role can only execute the lead intelligence RPC functions—no direct table access.

1. Create the Role (run once in Supabase SQL Editor)

-- Create a dedicated role for n8n integrations
CREATE ROLE n8n_readonly NOLOGIN;

-- Grant execute on lead intelligence functions only
GRANT EXECUTE ON FUNCTION get_visitor_for_lead_intelligence(TEXT, TEXT) TO n8n_readonly;
GRANT EXECUTE ON FUNCTION get_account_for_outreach_agent(TEXT, TEXT) TO n8n_readonly;

-- Create a service user that uses this role
CREATE USER n8n_service WITH PASSWORD 'your-secure-password-here';
GRANT n8n_readonly TO n8n_service;

2. Generate a JWT for n8n

Use the Supabase dashboard or API to generate a JWT for the n8n_service user, or use the service with direct PostgreSQL connection.

3. Configure n8n

In n8n, you can either:

  • Supabase node: Use the anon key (functions are granted to anon)
  • PostgreSQL node: Connect directly with n8n_service credentials (more secure, no JWT needed)

PostgreSQL node configuration:

Host: db.xxx.supabase.co
Database: postgres
User: n8n_service
Password: your-secure-password-here
Port: 5432
SSL: Require

Method Use Case Risk Level
PostgreSQL with n8n_service n8n integrations (recommended) Low - Only RPC execute permission
Supabase with Anon Key Quick setup, less secure Low-Medium
Service Role Never use externally High - Full database access

Use Cases

  1. Pre-call research: Review visitor's conversation history before a sales call
  2. Lead prioritization: Use intent score to prioritize outreach
  3. Personalized follow-up: Reference specific topics and questions in outreach
  4. Handoff notes: Provide context when passing leads between team members