Weekly leads by source, compared with last week

Every Monday, see how many leads each channel brought this week versus last week, on Slack.

Easy Saves ~2 hours/month MarketingBy Flowngn team

Works with n8n · Make · Zapier

How it works

Trigger: Schedule — Mondays at 9:00
  1. Read the Leads sheet (date, source).
  2. Count this week's and last week's leads by source.
  3. Post the comparison to your marketing channel.

Diagram

Mondays 9:00Schedule TriggerGRead leadsGoogle Sheets{ }Count by sourceCodeSPost to marketingSlack
Trigger App Code

Code

JavaScript used in the Code step — already included in the template below.

Count by source · Code node
// Leads sheet columns: date (YYYY-MM-DD) | name | source (utm_source, "instagram", "referral"…) | stage
// Counts last week's leads by source, compared with the week before.
const DAY = 24 * 60 * 60 * 1000;
const day = (n) => new Date(Date.now() - n * DAY).toISOString().slice(0, 10);
const [thisStart, lastStart] = [day(7), day(14)];

const count = { now: {}, before: {} };
for (const { json: l } of $input.all()) {
  const d = String(l.date).slice(0, 10);
  const src = String(l.source || "direct").trim().toLowerCase();
  if (d >= thisStart) count.now[src] = (count.now[src] || 0) + 1;
  else if (d >= lastStart) count.before[src] = (count.before[src] || 0) + 1;
}

const sources = [...new Set([...Object.keys(count.now), ...Object.keys(count.before)])]
  .sort((a, b) => (count.now[b] || 0) - (count.now[a] || 0));
const total = Object.values(count.now).reduce((a, b) => a + b, 0);
const lines = sources.map((s) => {
  const n = count.now[s] || 0;
  const diff = n - (count.before[s] || 0);
  return `• ${s}: ${n} (${diff >= 0 ? "+" : ""}${diff} vs last week)`;
});

return [{ json: { message: `📊 Leads this week: ${total}\n${lines.join("\n") || "No leads."}` } }];

Build it

Pick your tool — same automation, step by step.

Copy and paste straight into an n8n canvas (Ctrl+V), or download and use Import from File. Then connect your own credentials and replace every YOUR_… placeholder.

Workflow JSON
Show code (140 lines)
{
  "name": "Weekly leads by source, compared with last week (Flowngn)",
  "nodes": [
    {
      "parameters": {
        "content": "## Weekly leads by source, compared with last week\nEvery Monday, see how many leads each channel brought this week versus last week, on Slack.\n\n**Setup**\n1. Open each node and connect your own credentials.\n2. Replace every YOUR_… placeholder.\n3. Test once, then activate.\n\nFree template from Flowngn",
        "height": 260,
        "width": 340
      },
      "id": "3f36aaf0-8bb5-49c1-a1a3-cee797bcfadd",
      "name": "Setup notes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -420,
        -60
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 * * 1"
            }
          ]
        }
      },
      "id": "3f36aaf0-8bb5-49c1-a1a3-cee797bcfadd",
      "name": "Mondays 9:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Leads",
          "mode": "name"
        },
        "options": {}
      },
      "id": "3f36aaf0-8bb5-49c1-a1a3-cee797bcfadd",
      "name": "Read leads",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        300,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Leads sheet columns: date (YYYY-MM-DD) | name | source (utm_source, \"instagram\", \"referral\"…) | stage\n// Counts last week's leads by source, compared with the week before.\nconst DAY = 24 * 60 * 60 * 1000;\nconst day = (n) => new Date(Date.now() - n * DAY).toISOString().slice(0, 10);\nconst [thisStart, lastStart] = [day(7), day(14)];\n\nconst count = { now: {}, before: {} };\nfor (const { json: l } of $input.all()) {\n  const d = String(l.date).slice(0, 10);\n  const src = String(l.source || \"direct\").trim().toLowerCase();\n  if (d >= thisStart) count.now[src] = (count.now[src] || 0) + 1;\n  else if (d >= lastStart) count.before[src] = (count.before[src] || 0) + 1;\n}\n\nconst sources = [...new Set([...Object.keys(count.now), ...Object.keys(count.before)])]\n  .sort((a, b) => (count.now[b] || 0) - (count.now[a] || 0));\nconst total = Object.values(count.now).reduce((a, b) => a + b, 0);\nconst lines = sources.map((s) => {\n  const n = count.now[s] || 0;\n  const diff = n - (count.before[s] || 0);\n  return `• ${s}: ${n} (${diff >= 0 ? \"+\" : \"\"}${diff} vs last week)`;\n});\n\nreturn [{ json: { message: `📊 Leads this week: ${total}\\n${lines.join(\"\\n\") || \"No leads.\"}` } }];"
      },
      "id": "3f36aaf0-8bb5-49c1-a1a3-cee797bcfadd",
      "name": "Count by source",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        0
      ]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "#marketing",
          "mode": "name"
        },
        "text": "={{ $json.message }}",
        "otherOptions": {}
      },
      "id": "3f36aaf0-8bb5-49c1-a1a3-cee797bcfadd",
      "name": "Post to marketing",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        900,
        0
      ]
    }
  ],
  "connections": {
    "Mondays 9:00": {
      "main": [
        [
          {
            "node": "Read leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read leads": {
      "main": [
        [
          {
            "node": "Count by source",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Count by source": {
      "main": [
        [
          {
            "node": "Post to marketing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}

Open in the playground to edit →

Tips

  • Save utm_source from every form so the source column fills itself.

Related flows