{
  "name": "TrendTrack — New Ads of Brandtracker → Discord",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 6
            }
          ]
        }
      },
      "id": "6bd7e083-e7b1-4652-a4d8-5a637321c33e",
      "name": "Every 6 Hours",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [240, 304]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "folderName",
              "value": "Demo",
              "type": "string"
            },
            {
              "id": "2",
              "name": "reachThreshold",
              "value": 5000,
              "type": "number"
            },
            {
              "id": "3",
              "name": "discordGuildId",
              "value": "0000000000000000000",
              "type": "string"
            },
            {
              "id": "4",
              "name": "discordChannelId",
              "value": "0000000000000000000",
              "type": "string"
            },
            {
              "id": "5",
              "name": "lookbackHours",
              "value": 120,
              "type": "number"
            },
            {
              "id": "6",
              "name": "maxAdsPerBrand",
              "value": 20,
              "type": "number"
            },
            {
              "id": "7",
              "name": "maxBrandsPerRun",
              "value": 100,
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "id": "a09ec96d-4fbe-4a4b-a584-202748c55fb3",
      "name": "Set Parameters",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [464, 304]
    },
    {
      "parameters": {
        "url": "https://api.trendtrack.io/v1/brandtrackers",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "folderNames",
              "value": "={{ $json.folderName }}"
            },
            {
              "name": "limit",
              "value": "={{ $json.maxBrandsPerRun }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {
          "queryParameterArrays": "repeat"
        }
      },
      "id": "f3f5f7c1-7035-4956-ae49-1732c38879a6",
      "name": "Fetch Brandtrackers In Folder",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [688, 304]
    },
    {
      "parameters": {
        "fieldToSplitOut": "data",
        "options": {}
      },
      "id": "4812d718-1c74-4b22-9b90-d6542a7070b2",
      "name": "Split Brandtrackers",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [912, 304]
    },
    {
      "parameters": {
        "url": "=https://api.trendtrack.io/v1/brandtrackers/{{ $json.id }}/ads",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "createdAfter",
              "value": "={{ $now.minus({hours: $('Set Parameters').first().json.lookbackHours}).toISO() }}"
            },
            {
              "name": "sort",
              "value": "newest"
            },
            {
              "name": "status",
              "value": "active"
            },
            {
              "name": "limit",
              "value": "={{ $('Set Parameters').first().json.maxAdsPerBrand }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "d4e6757e-18be-4118-b2d8-620cb45c0db4",
      "name": "Fetch Recent Ads For Brand",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [1120, 304]
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const ads = $input.item.json.data || [];\nconst brandName = $('Split Brandtrackers').item.json.name;\nconst brandId = $('Split Brandtrackers').item.json.id;\nreturn { json: { data: ads.map(a => ({ ...a, _brandName: brandName, _brandId: brandId })) } };"
      },
      "id": "076568a8-6c0d-43ff-ac9b-5880690a03c2",
      "name": "Attach Brand Name",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1344, 304],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "fieldToSplitOut": "data",
        "options": {}
      },
      "id": "27b3187a-6ab9-41a8-be85-59c0cbc6f7e1",
      "name": "Split Ads",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [1568, 304]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "1",
              "leftValue": "={{ $json.metrics?.reach ?? 0 }}",
              "rightValue": "={{ $('Set Parameters').first().json.reachThreshold }}",
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "f4c040de-8131-470a-9658-74b6b576aaab",
      "name": "Filter Reach > Threshold",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [1792, 304]
    },
    {
      "parameters": {
        "resource": "message",
        "guildId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Set Parameters').first().json.discordGuildId }}"
        },
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Set Parameters').first().json.discordChannelId }}"
        },
        "content": "=🆕 New ad from *{{ $json._brandName }}* — reach {{ Math.round(($json.metrics?.reach || 0) / 1000) }}k",
        "options": {},
        "embeds": {
          "values": [
            {
              "description": "={{ ($json.content?.body || \"\").slice(0, 280) }}",
              "color": "#0099ff",
              "title": "={{ $json._brandName }} — Ad #{{ ($json.id || \"\").replace(/^[a-z]+_/, \"\") }}",
              "url": "=https://app.trendtrack.io/ad/{{ ($json.id || \"\").replace(/^[a-z]+_/, \"\") }}",
              "image": "={{ $json.media?.thumbnailUrl || \"\" }}"
            }
          ]
        }
      },
      "id": "f428f9db-0753-44f9-937a-17a49d02012c",
      "name": "Post Ad to Discord",
      "type": "n8n-nodes-base.discord",
      "typeVersion": 2,
      "position": [2000, 304]
    }
  ],
  "connections": {
    "Every 6 Hours": {
      "main": [
        [
          {
            "node": "Set Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Parameters": {
      "main": [
        [
          {
            "node": "Fetch Brandtrackers In Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Brandtrackers In Folder": {
      "main": [
        [
          {
            "node": "Split Brandtrackers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Brandtrackers": {
      "main": [
        [
          {
            "node": "Fetch Recent Ads For Brand",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Recent Ads For Brand": {
      "main": [
        [
          {
            "node": "Attach Brand Name",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Attach Brand Name": {
      "main": [
        [
          {
            "node": "Split Ads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Ads": {
      "main": [
        [
          {
            "node": "Filter Reach > Threshold",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Reach > Threshold": {
      "main": [
        [
          {
            "node": "Post Ad to Discord",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": true
  },
  "tags": []
}
