{
  "name": "TrendTrack — Scaling Ads Alert → Slack",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 10
            }
          ]
        }
      },
      "id": "68661ad7-55e0-4085-b538-88863581997d",
      "name": "Every Day at 10am",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [240, 304]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "slackChannelId",
              "value": "C0123456789",
              "type": "string"
            },
            {
              "id": "2",
              "name": "minRankDelta",
              "value": 10,
              "type": "number"
            },
            {
              "id": "3",
              "name": "topRankThreshold",
              "value": 10,
              "type": "number"
            },
            {
              "id": "4",
              "name": "limit",
              "value": 20,
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "id": "abb517dc-42f1-49f0-8a06-86e54b88e40d",
      "name": "Set Parameters",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [464, 304]
    },
    {
      "parameters": {
        "url": "https://api.trendtrack.io/v1/workspace/top-ads",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "sortBy",
              "value": "rankDelta7d"
            },
            {
              "name": "period",
              "value": "7d"
            },
            {
              "name": "limit",
              "value": "={{ $json.limit }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "1e0da432-c523-4bec-9e9a-9e1b541f4642",
      "name": "Fetch Top Ads (rankDelta7d)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [688, 304]
    },
    {
      "parameters": {
        "fieldToSplitOut": "data",
        "options": {}
      },
      "id": "08daeaa7-25f3-4dd8-b73e-746125feba4b",
      "name": "Split Ads",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [912, 304]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "1",
              "leftValue": "={{ $json.ad?.rank?.rankDelta ?? 0 }}",
              "rightValue": "={{ $('Set Parameters').first().json.minRankDelta }}",
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            },
            {
              "id": "2",
              "leftValue": "={{ $json.metrics?.currentRank ?? 9999 }}",
              "rightValue": "={{ $('Set Parameters').first().json.topRankThreshold }}",
              "operator": {
                "type": "number",
                "operation": "lte"
              }
            }
          ],
          "combinator": "or"
        },
        "options": {}
      },
      "id": "1214ce19-36a5-4cd7-a67c-e53ec22584ca",
      "name": "Filter Scaling Ads",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [1120, 304]
    },
    {
      "parameters": {
        "jsCode": "const params = $('Set Parameters').first().json;\nconst items = $input.all().map(i => i.json);\nconst fmt = (n) => { if (n == null) return '?'; const a = Math.abs(n); if (a >= 1e6) return (n/1e6).toFixed(1)+'M'; if (a >= 1e3) return (n/1e3).toFixed(1)+'k'; return String(n); };\nif (items.length === 0) return [{ json: { skip: true, blocks: [], text: 'No scaling ads today', channelId: params.slackChannelId } }];\nitems.sort((a, b) => (b.ad?.rank?.rankDelta ?? 0) - (a.ad?.rank?.rankDelta ?? 0));\nconst capped = items.slice(0, 40);\nconst overflow = items.length - capped.length;\nconst blocks = [{ type: 'header', text: { type: 'plain_text', text: '🚀 ' + items.length + ' scaling ads' } }];\nfor (const it of capped) {\n  const brand = it.brandtracker?.name || 'Unknown';\n  const headline = (it.ad?.content?.body || '').slice(0, 140);\n  const mediaUrl = it.ad?.media?.mediaUrl || it.ad?.media?.thumbnailUrl;\n  const rank = it.metrics?.currentRank ?? '?';\n  const rankDelta = it.ad?.rank?.rankDelta ?? 0;\n  const reach = fmt(it.metrics?.totalReach);\n  const thumb = it.ad?.media?.thumbnailUrl;\n  const title = mediaUrl ? '*<' + mediaUrl + '|#' + rank + ' — ' + brand + '>* (Δ rank +' + rankDelta + ')' : '*#' + rank + ' — ' + brand + '* (Δ rank +' + rankDelta + ')';\n  const section = { type: 'section', text: { type: 'mrkdwn', text: title + '\\n' + headline + '\\n_Reach_ *' + reach + '*' } };\n  if (thumb) section.accessory = { type: 'image', image_url: thumb, alt_text: brand };\n  blocks.push(section);\n}\nif (overflow > 0) blocks.push({ type: 'context', elements: [{ type: 'mrkdwn', text: '_+' + overflow + ' more — tighten thresholds to filter._' }] });\nreturn [{ json: { skip: false, blocks, channelId: params.slackChannelId, text: '🚀 ' + items.length + ' scaling ads' } }];"
      },
      "id": "84903512-2156-41fd-bdb9-abbb78a8740f",
      "name": "Build Slack Blocks",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1344, 304],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.channelId }}"
        },
        "messageType": "block",
        "blocksUi": "={{ JSON.stringify({ blocks: $json.blocks }) }}",
        "text": "={{ $json.text }}",
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "id": "c61bdf3d-a3e9-41a1-a434-83343bc09f95",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.4,
      "position": [1568, 304]
    }
  ],
  "connections": {
    "Every Day at 10am": {
      "main": [
        [
          {
            "node": "Set Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Parameters": {
      "main": [
        [
          {
            "node": "Fetch Top Ads (rankDelta7d)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Top Ads (rankDelta7d)": {
      "main": [
        [
          {
            "node": "Split Ads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Ads": {
      "main": [
        [
          {
            "node": "Filter Scaling Ads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Scaling Ads": {
      "main": [
        [
          {
            "node": "Build Slack Blocks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Slack Blocks": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": true
  },
  "tags": []
}
