{
  "name": "TrendTrack — Daily Top Viral Ads → Slack",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 9
            }
          ]
        }
      },
      "id": "3123bbbf-8917-4db2-abe9-08ee5f0b2cb6",
      "name": "Every Day at 9am",
      "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": "limit",
              "value": 10,
              "type": "number"
            },
            {
              "id": "3",
              "name": "period",
              "value": "1d",
              "type": "string"
            },
            {
              "id": "4",
              "name": "sortBy",
              "value": "reachDelta1d",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "6d05b9e8-9ff8-4745-bf5f-96d6e31399a5",
      "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": "={{ $json.sortBy }}"
            },
            {
              "name": "period",
              "value": "={{ $json.period }}"
            },
            {
              "name": "limit",
              "value": "={{ $json.limit }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "ae1c9e82-1542-49a4-a860-1f1d8cdbe56f",
      "name": "Fetch Workspace Top Ads",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [688, 304]
    },
    {
      "parameters": {
        "jsCode": "const params = $('Set Parameters').first().json;\nconst items = $input.first().json.data || [];\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); };\nconst capped = items.slice(0, 40);\nconst overflow = items.length - capped.length;\nconst blocks = [{ type: 'header', text: { type: 'plain_text', text: '🔥 Top ' + items.length + ' viral ads today' } }];\nfor (const it of capped) {\n  const brand = it.brandtracker?.name || 'Unknown';\n  const headline = (it.ad?.content?.body || '').slice(0, 140);\n  const reach = fmt(it.metrics?.totalReach);\n  const delta = fmt(it.ad?.metrics?.reachDelta1d);\n  const rank = it.metrics?.currentRank ?? '?';\n  const mediaUrl = it.ad?.media?.mediaUrl || it.ad?.media?.thumbnailUrl;\n  const thumb = it.ad?.media?.thumbnailUrl;\n  const title = mediaUrl ? '*<' + mediaUrl + '|#' + rank + ' — ' + brand + '>*' : '*#' + rank + ' — ' + brand + '*';\n  const section = { type: 'section', text: { type: 'mrkdwn', text: title + '\\n' + headline + '\\n_Reach_ *' + reach + '* (+' + delta + ' /24h)' } };\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 (lower the limit param to declutter)._' }] });\nreturn [{ json: { blocks, channelId: params.slackChannelId, text: '🔥 Top ' + items.length + ' viral ads today' } }];"
      },
      "id": "bc3c5485-09ad-411a-bea8-0d97ae6c0ce6",
      "name": "Build Slack Blocks",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [912, 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": "2e2b11fa-cef1-4082-a58b-b4fa90778665",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.4,
      "position": [1120, 304]
    }
  ],
  "connections": {
    "Every Day at 9am": {
      "main": [
        [
          {
            "node": "Set Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Parameters": {
      "main": [
        [
          {
            "node": "Fetch Workspace Top Ads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Workspace Top 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": []
}
