{
  "name": "Find duplicate CRM contacts in Google Sheets with email and domain normalisation",
  "nodes": [
    {
      "id": "4f5c6131-c3fb-47e2-a76b-1df565c6ebdc",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -780,
        -280
      ],
      "parameters": {
        "content": "## CRM duplicate finder for Google Sheets\n\n### How it works\nReads every contact from your Google Sheet and flags likely duplicates without changing or deleting a single row. Emails are normalised first: lowercased, trimmed, plus tags removed (name+news@ becomes name@), and Gmail dots ignored (googlemail.com is treated as gmail.com). Company domains come from the website column, or from the email when it is not a free mail address. Two rows are grouped when they share a normalised email (high confidence), the same company domain and name, or the same phone number (medium). Each group gets an id and a suggested master row: the most complete record, then the earliest. Results are appended to a **duplicates** tab for you to review and merge by hand.\n\n### Setup\n1. Make sure your contacts tab has a header row. Defaults: name, email, company, website, phone.\n2. Add a tab called **duplicates** with headers: group_id, confidence, match_reason, source_row, suggested_master, master_row, name, email, normalised_email, company, company_domain, phone, filled_fields, found_at\n3. Fill in **Set your settings** with your sheet ID, tab names and column names.\n4. Connect your Google Sheets credential in both Sheets nodes.\n\n### Customization\nAdd or remove match rules in **Find likely duplicates**. Each run appends a new batch stamped with found_at, so filter on the latest date.\n\nBuilt by Extol Affiliates (extol.si). Related case study: https://www.extol.si/insights-crm-deduplication-4000-records-one-source-of-truth.html",
        "width": 520,
        "height": 900
      }
    },
    {
      "id": "1a11b83a-9421-4319-a506-fc67c4c4df06",
      "name": "Section: read",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -200,
        -120
      ],
      "parameters": {
        "content": "## 1. Settings and read\nTab and column names live in one node so the template fits your existing sheet.",
        "width": 660,
        "height": 440,
        "color": 7
      }
    },
    {
      "id": "b39792fd-9a12-4a9d-ab95-8096d09d8bd7",
      "name": "Section: match",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        500,
        -120
      ],
      "parameters": {
        "content": "## 2. Match and report\nRead only. Duplicates are written to their own tab, the source rows are never touched.",
        "width": 520,
        "height": 440,
        "color": 7
      }
    },
    {
      "id": "3210dd29-1bc1-4b03-aeef-d4462dd0019a",
      "name": "Run duplicate check",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -140,
        100
      ],
      "parameters": {}
    },
    {
      "id": "bfa761a1-b0ad-4bcd-afde-319471c9a23d",
      "name": "Set your settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        80,
        100
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a7b1b92b-4b02-4372-a606-f2cd4faca05b",
              "name": "spreadsheetId",
              "value": "PASTE_YOUR_GOOGLE_SHEET_ID",
              "type": "string"
            },
            {
              "id": "dbf825c6-ebc3-447d-a68f-aa6a2bc7584a",
              "name": "contactsTab",
              "value": "Contacts",
              "type": "string"
            },
            {
              "id": "04dac7de-0de2-4fdb-a8f1-69ffb3cbb39f",
              "name": "duplicatesTab",
              "value": "duplicates",
              "type": "string"
            },
            {
              "id": "e7bfb025-900d-4bb6-ae16-3aa54724b5c7",
              "name": "emailColumn",
              "value": "email",
              "type": "string"
            },
            {
              "id": "1ec95318-d220-47d8-a101-e71f63e7d424",
              "name": "nameColumn",
              "value": "name",
              "type": "string"
            },
            {
              "id": "f911c2a3-f05d-42d4-a6ee-d2aae27f3de6",
              "name": "companyColumn",
              "value": "company",
              "type": "string"
            },
            {
              "id": "41ff7798-0d71-4ffe-ad6c-a4e0a34a8c0d",
              "name": "websiteColumn",
              "value": "website",
              "type": "string"
            },
            {
              "id": "ca3c73a1-25da-4aa2-aebc-4e76e58f8bb8",
              "name": "phoneColumn",
              "value": "phone",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "d69257a5-8de1-4aa8-a51c-7a665a3da7f6",
      "name": "Read contacts",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        300,
        100
      ],
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "={{ $('Set your settings').first().json.spreadsheetId }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "={{ $('Set your settings').first().json.contactsTab }}",
          "mode": "name"
        },
        "options": {}
      }
    },
    {
      "id": "4ae3d999-3193-421f-a4c3-9613062152c9",
      "name": "Find likely duplicates",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        560,
        100
      ],
      "parameters": {
        "jsCode": "// Finds likely duplicate contacts. Read only: it never edits or deletes the source rows.\n// Match rules (any one links two rows into the same group):\n//   1. Same normalised email (high confidence)\n//   2. Same company domain and same normalised full name (medium)\n//   3. Same phone number, last 10 digits (medium)\n// Column names come from \"Set your settings\" so you can match your own sheet.\nconst s = $('Set your settings').first().json;\nconst col = {\n  email: s.emailColumn || 'email',\n  name: s.nameColumn || 'name',\n  company: s.companyColumn || 'company',\n  website: s.websiteColumn || 'website',\n  phone: s.phoneColumn || 'phone',\n};\nconst FREE_MAIL = new Set(['gmail.com', 'googlemail.com', 'outlook.com', 'hotmail.com', 'hotmail.co.uk', 'live.com', 'live.co.uk', 'yahoo.com', 'yahoo.co.uk', 'icloud.com', 'me.com', 'aol.com', 'btinternet.com', 'sky.com', 'protonmail.com', 'proton.me']);\n\nconst normaliseEmail = (value) => {\n  const raw = String(value || '').trim().toLowerCase().replace(/^mailto:/, '');\n  const at = raw.lastIndexOf('@');\n  if (at < 1 || at === raw.length - 1) return '';\n  let local = raw.slice(0, at);\n  let domain = raw.slice(at + 1).replace(/\\.$/, '');\n  local = local.split('+')[0]; // plus addressing: name+tag@ is the same inbox\n  if (domain === 'googlemail.com') domain = 'gmail.com';\n  if (domain === 'gmail.com') local = local.replace(/\\./g, ''); // Gmail ignores dots\n  return local ? `${local}@${domain}` : '';\n};\nconst normaliseDomain = (value) => String(value || '')\n  .trim()\n  .toLowerCase()\n  .replace(/^[a-z]+:\\/\\//, '')\n  .replace(/^www\\d?\\./, '')\n  .split(/[\\/?#:\\s]/)[0]\n  .replace(/\\.$/, '');\nconst normaliseName = (value) => String(value || '')\n  .toLowerCase()\n  .normalize('NFKD').replace(/[̀-ͯ]/g, '')\n  .replace(/[^a-z\\s]/g, ' ')\n  .replace(/\\b(mr|mrs|ms|miss|dr|prof)\\b/g, ' ')\n  .replace(/\\s+/g, ' ')\n  .trim();\nconst normalisePhone = (value) => {\n  const digits = String(value || '').replace(/\\D/g, '');\n  return digits.length >= 9 ? digits.slice(-10) : '';\n};\n\nconst rows = $input.all().map((item, index) => {\n  const r = item.json;\n  const email = normaliseEmail(r[col.email]);\n  const emailDomain = email ? email.split('@')[1] : '';\n  const companyDomain = normaliseDomain(r[col.website]) || (FREE_MAIL.has(emailDomain) ? '' : emailDomain);\n  const filled = Object.values(r).filter((v) => v !== '' && v !== null && v !== undefined).length;\n  return {\n    index,\n    rowNumber: r.row_number ?? index + 2,\n    raw: r,\n    email,\n    companyDomain,\n    name: normaliseName(r[col.name]),\n    phone: normalisePhone(r[col.phone]),\n    filled,\n  };\n});\n\n// Union find keeps the grouping transitive: if A matches B and B matches C, all three share a group.\nconst parent = rows.map((_, i) => i);\nconst find = (i) => (parent[i] === i ? i : (parent[i] = find(parent[i])));\nconst reasons = rows.map(() => new Set());\nconst link = (keyMap, key, i, reason) => {\n  if (!key) return;\n  if (keyMap.has(key)) {\n    const j = keyMap.get(key);\n    parent[find(i)] = find(j);\n    reasons[i].add(reason);\n    reasons[j].add(reason);\n  } else keyMap.set(key, i);\n};\nconst byEmail = new Map();\nconst byDomainName = new Map();\nconst byPhone = new Map();\nrows.forEach((row, i) => {\n  link(byEmail, row.email, i, 'same email');\n  if (row.companyDomain && row.name) link(byDomainName, `${row.companyDomain}|${row.name}`, i, 'same company domain and name');\n  link(byPhone, row.phone, i, 'same phone');\n});\n\nconst groups = new Map();\nrows.forEach((row, i) => {\n  const root = find(i);\n  if (!groups.has(root)) groups.set(root, []);\n  groups.get(root).push(row);\n});\n\nconst runAt = new Date().toISOString();\nconst out = [];\nlet groupNumber = 0;\nfor (const members of groups.values()) {\n  if (members.length < 2) continue;\n  groupNumber += 1;\n  const groupId = `DUP-${String(groupNumber).padStart(4, '0')}`;\n  // Suggested master: the most complete row, then the earliest row in the sheet.\n  const master = [...members].sort((a, b) => b.filled - a.filled || a.rowNumber - b.rowNumber)[0];\n  const groupReasons = new Set(members.flatMap((m) => [...reasons[m.index]]));\n  const confidence = groupReasons.has('same email') ? 'high' : 'medium';\n  for (const m of members) {\n    out.push({\n      json: {\n        group_id: groupId,\n        confidence,\n        match_reason: [...reasons[m.index]].join(', '),\n        source_row: m.rowNumber,\n        suggested_master: m === master ? 'yes' : 'no',\n        master_row: master.rowNumber,\n        name: m.raw[col.name] ?? '',\n        email: m.raw[col.email] ?? '',\n        normalised_email: m.email,\n        company: m.raw[col.company] ?? '',\n        company_domain: m.companyDomain,\n        phone: m.raw[col.phone] ?? '',\n        filled_fields: m.filled,\n        found_at: runAt,\n      },\n    });\n  }\n}\nreturn out;"
      }
    },
    {
      "id": "0e841f6f-7614-4a3f-a940-ad1590b4f3e4",
      "name": "Write duplicates tab",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        800,
        100
      ],
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{ $('Set your settings').first().json.spreadsheetId }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "={{ $('Set your settings').first().json.duplicatesTab }}",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": [
            {
              "id": "group_id",
              "displayName": "group_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "confidence",
              "displayName": "confidence",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "match_reason",
              "displayName": "match_reason",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "source_row",
              "displayName": "source_row",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "suggested_master",
              "displayName": "suggested_master",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "master_row",
              "displayName": "master_row",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "name",
              "displayName": "name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "email",
              "displayName": "email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "normalised_email",
              "displayName": "normalised_email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "company",
              "displayName": "company",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "company_domain",
              "displayName": "company_domain",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "phone",
              "displayName": "phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "filled_fields",
              "displayName": "filled_fields",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "found_at",
              "displayName": "found_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Run duplicate check": {
      "main": [
        [
          {
            "node": "Set your settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set your settings": {
      "main": [
        [
          {
            "node": "Read contacts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read contacts": {
      "main": [
        [
          {
            "node": "Find likely duplicates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find likely duplicates": {
      "main": [
        [
          {
            "node": "Write duplicates tab",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
