{
  "name": "Campaign link sheet changelog (Skillaisle)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "15 7 * * 4"
            }
          ]
        }
      },
      "id": "e1f2a3b4-3003-4000-8000-000000000001",
      "name": "Thursdays 07:15",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        40,
        240
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e1f2a3b4-3003-4000-8000-0000000000c1",
              "name": "sheet_csv_url",
              "value": "https://example.com/growth/campaign-links.csv",
              "type": "string"
            },
            {
              "id": "e1f2a3b4-3003-4000-8000-0000000000c2",
              "name": "destination_column",
              "value": "destination_url",
              "type": "string"
            },
            {
              "id": "e1f2a3b4-3003-4000-8000-0000000000c3",
              "name": "channel_column",
              "value": "channel",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "e1f2a3b4-3003-4000-8000-000000000002",
      "name": "Sheet export config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        300,
        240
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.sheet_csv_url }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "text"
            }
          }
        }
      },
      "id": "e1f2a3b4-3003-4000-8000-000000000003",
      "name": "Fetch sheet CSV",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        560,
        240
      ]
    },
    {
      "parameters": {
        "jsCode": "const cfg = $('Sheet export config').first().json;\nconst raw = String($json.data ?? $json.body ?? '');\nconst destCol = String(cfg.destination_column || 'destination_url').trim().toLowerCase();\nconst channelCol = String(cfg.channel_column || 'channel').trim().toLowerCase();\nconst lines = raw.replace(/^\\uFEFF/, '').split(/\\r?\\n/).filter(Boolean);\nfunction split(line) {\n  const out = []; let cur = ''; let q = false;\n  for (let i = 0; i < line.length; i++) {\n    const ch = line[i];\n    if (ch === '\"') { q = !q; continue; }\n    if (ch === ',' && !q) { out.push(cur.trim()); cur = ''; continue; }\n    cur += ch;\n  }\n  out.push(cur.trim());\n  return out;\n}\nconst header = lines.length ? split(lines[0]).map(h => h.toLowerCase()) : [];\nlet di = header.indexOf(destCol); if (di < 0) di = header.findIndex(h => h.includes('url') || h.includes('destination')); if (di < 0) di = 0;\nlet ci = header.indexOf(channelCol);\nconst current = new Map(); // url -> channel\nfor (const line of lines.slice(1)) {\n  const cells = split(line);\n  const url = (cells[di] || '').trim();\n  if (!/^https?:\\/\\//i.test(url)) continue;\n  const channel = ci >= 0 ? ((cells[ci] || '').trim() || 'uncategorized') : 'uncategorized';\n  if (!current.has(url)) current.set(url, channel);\n}\nconst urls = [...current.keys()].sort();\nconst store = $getWorkflowStaticData('global');\nconst key = 'campaign-sheet:' + cfg.sheet_csv_url;\nconst prev = store[key];\nconst nowIso = new Date().toISOString();\nconst md = ['# Campaign link sheet changelog', '', `Sheet CSV: ${cfg.sheet_csv_url}`, `Checked: ${nowIso}`, `Destinations now: ${urls.length}`, ''];\nlet newcomers = [], retirees = [];\nif (!Array.isArray(prev)) {\n  md.push('Baseline captured from this activated run. Thursday changelogs start on the following run.');\n} else {\n  const before = new Set(prev);\n  const after = new Set(urls);\n  newcomers = urls.filter(u => !before.has(u));\n  retirees = prev.filter(u => !after.has(u));\n  const byChannel = {};\n  for (const u of newcomers) {\n    const ch = current.get(u) || 'uncategorized';\n    (byChannel[ch] ||= []).push(u);\n  }\n  md.push(`## New destinations (${newcomers.length})`);\n  if (!newcomers.length) md.push('- none');\n  else {\n    for (const ch of Object.keys(byChannel).sort()) {\n      md.push(`### ${ch}`);\n      for (const u of byChannel[ch]) md.push(`- ${u}`);\n    }\n  }\n  md.push('', `## Retired destinations (${retirees.length})`, ...(retirees.length ? retirees.map(u => `- ${u}`) : ['- none']));\n}\nif (!urls.length) md.push('', 'No http(s) destinations parsed \u2014 verify sheet_csv_url and destination_column.');\nelse store[key] = urls;\nreturn [{ json: { report: md.join('\\n'), total: urls.length, newcomers: newcomers.length, retirees: retirees.length } }];\n"
      },
      "id": "e1f2a3b4-3003-4000-8000-000000000004",
      "name": "Compile changelog",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        820,
        240
      ]
    },
    {
      "parameters": {
        "operation": "toText",
        "sourceProperty": "report",
        "options": {
          "fileName": "campaign-link-changelog.md"
        }
      },
      "id": "e1f2a3b4-3003-4000-8000-000000000005",
      "name": "Attach changelog file",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        1080,
        240
      ]
    }
  ],
  "connections": {
    "Thursdays 07:15": {
      "main": [
        [
          {
            "node": "Sheet export config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sheet export config": {
      "main": [
        [
          {
            "node": "Fetch sheet CSV",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch sheet CSV": {
      "main": [
        [
          {
            "node": "Compile changelog",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compile changelog": {
      "main": [
        [
          {
            "node": "Attach changelog file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
