How to Automate Prioritizing Leads by Activity with n8n

admin1234 Avatar

### Introduction

In sales teams, timely follow-up with leads is crucial to increase conversion rates. However, not all leads are equally valuable or engaged. Prioritizing leads by their recent activity allows salespeople to focus on the most promising prospects first, improving efficiency and deal closure rates.

This article walks you through building a lead prioritization workflow using n8n, an open-source workflow automation tool. The workflow will automatically capture lead activity data from multiple sources, score or rank leads based on their interactions, and update your CRM or sales tool accordingly. Sales teams and automation engineers will benefit from implementing this automated prioritization, enabling more efficient resource allocation and faster response times.

### Tools and Services Integrated

– **n8n:** Automation platform to build the workflow.
– **HubSpot CRM:** As the lead management system.
– **Google Sheets:** For intermediate data storage and reporting.
– **Slack:** To notify sales reps of high-priority leads.
– **Email (Gmail):** Optional email alerts.

This example uses HubSpot as the CRM, but the principles can be adapted for Salesforce, Pipedrive, or others.

### Workflow Overview

The goal is to gather recent lead activities such as website visits, email opens/clicks, meeting requests, and form submissions, score each lead based on activity parameters, update the lead score in HubSpot, and notify sales reps of leads exceeding a priority threshold.

Trigger: Scheduled interval (e.g., every hour) or webhook from external activity source.
Steps:

1. **Fetch leads from HubSpot**
2. **Retrieve recent activity data from relevant sources** (e.g., HubSpot engagements API, Google Analytics, email activity logs)
3. **Calculate lead score based on defined criteria**
4. **Update lead properties in HubSpot with new score**
5. **Filter high-priority leads**
6. **Send notifications via Slack or Email**

### Step-by-Step Technical Tutorial

#### 1. Setting Up n8n
– Deploy n8n locally, on a server, or use n8n.cloud.
– Create necessary API credentials:
– HubSpot API Key or OAuth credentials.
– Slack Bot Token.
– Google Sheets service account credentials if applicable.

#### 2. Create a New Workflow in n8n
Open the n8n editor UI and start with a new workflow.

#### 3. Add a Trigger Node
– Use the **Cron** node to run the workflow periodically (e.g., every hour).

#### 4. Fetch Leads from HubSpot
– Add a **HubSpot** node configured to **List Contacts**.
– Set filters if required (e.g., only leads created in last 30 days).
– Retrieve relevant properties like email, lead status, existing lead score.

#### 5. For Each Lead, Retrieve Activity Data
This may require multiple nodes depending on your data sources.

##### Option A: Use HubSpot Engagements API
– Add a **HTTP Request** node to query the HubSpot Engagements API for each lead’s recent activity (calls, emails, meetings).
– Use pagination to handle multiple activities.

##### Option B: Pull Email Open/Click Data
– If you use a supported email marketing tool (e.g., Mailchimp) integrated with HubSpot or standalone tools, query their APIs similarly.

##### Option C: Website Activity
– Optionally, fetch website analytics data from Google Analytics or HubSpot tracking.

Use the **SplitInBatches** node to process leads individually when querying APIs that require lead identifiers.

#### 6. Calculate Lead Scores
– Add a **Function** node.
– Inputs: compiled activities for the given lead.
– Define scoring rules, e.g.:
– +10 points for a website form submission
– +5 points for email open
– +15 points for meeting scheduled
– Subtract points for inactivity over X days
– Compute total lead score.

Example JavaScript code snippet:
“`javascript
const activities = items[0].json.activities; // Assuming array fetched
let score = 0;
activities.forEach(activity => {
if(activity.type === ‘form_submission’) score += 10;
else if(activity.type === ’email_open’) score += 5;
else if(activity.type === ‘meeting_scheduled’) score += 15;
});

if(items[0].json.days_since_last_activity > 7) score -= 5;

return [{json: {leadId: items[0].json.leadId, score}}];
“`

#### 7. Update Lead Score in HubSpot
– Using the **HubSpot** node, update the contact property “Lead Score” with the computed score.
– Use the lead ID to target the right record.

#### 8. Filter High-Priority Leads
– Use the **IF** node to check if score exceeds a defined threshold (e.g., 20).
– Route only high-scoring leads to notification steps.

#### 9. Notify Sales Team
– Add a **Slack** node:
– Post a message in a sales channel tagging the responsible rep.
– Include lead details and score.
– Optionally, add a **Gmail** node to send an email alert.

#### 10. Store or Export Data (Optional)
– Add a **Google Sheets** node to log lead activity and scores for reporting.

### Common Errors and Tips

– **API Rate Limits:** Be mindful of HubSpot and other APIs’ rate limits. Use n8n’s **SplitInBatches** and retry settings to manage this.
– **Authentication Failures:** Ensure API keys and OAuth tokens are refreshed and correctly set.
– **Data Mapping Errors:** Validate the data fields you pull and update match your service schema.
– **Handling Large Lead Volumes:** Split the workflow or paginate results to avoid timeouts.
– **Error Handling:** Add **Error Trigger** nodes or try/catch logic to catch failed API calls and alert administrators.

### Adapting and Scaling the Workflow

– **Add More Activity Sources:** Integrate email marketing platforms, call tracking systems, or social media engagement APIs.
– **Customize Scoring Models:** Refine the point assignments based on historical data and sales feedback.
– **Use Machine Learning:** Export data from this workflow to train models for predictive lead scoring.
– **Increase Frequency:** Adjust the Cron node interval for near real-time updates.
– **Multi-Rep Assignments:** Use additional logic to assign leads to sales reps based on score or territory.

### Summary

By automating the prioritization of leads by activity with n8n, your sales team gains actionable insight into which prospects deserve immediate attention. This workflow streamlines manual data collection and scoring, ensuring leads are handled timely. With integrations to HubSpot, Slack, and Google Sheets, the process becomes seamless and scalable.

**Bonus Tip:** Combine this workflow with automated follow-up sequences in your CRM or email marketing tool to nurture leads dynamically based on their score tier, creating an end-to-end automated lead management system.