{
  "name": "New Facebook ads for a brand, posted to Slack",
  "nodes": [
    {
      "id": "3a22eca5-bcbe-5a34-ba29-b122cabb0161",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -720,
        0
      ],
      "parameters": {
        "content": "## New Facebook ads for a brand, posted to Slack\n\n### How it works\nFor marketers and agencies who want to know the day a brand starts a new ad. Every day at 07:00, the workflow runs the [Facebook Ad Library Scraper](https://apify.com/aurelix/facebook-ad-library-scraper) Actor on Apify and searches the Facebook Ad Library for a brand's name or page ID with new-ads mode on, and posts each ad the brand started since the last run (page, headline, start date, platforms, landing page, Ad Library link) to Slack; nothing is posted when there are no new ads.\n\n### Setup\n1. Install the **Apify** node (verified community node: on n8n Cloud search *Apify* in the nodes panel and click Install; self-hosted: Settings > Community nodes > `@apify/n8n-nodes-apify`). Create an **Apify API** credential with your API token (Apify Console > Settings > API & Integrations) and select it on the Apify node. Tested with `@apify/n8n-nodes-apify` 0.9.0 on n8n 2.46; install guide: https://docs.apify.com/platform/integrations/n8n\n2. Create a **Slack** credential for a bot with `chat:write`, invite the bot to the channel set in the Set node, and select it on **Post to Slack**.\n3. Open **Set search** and change the values: what to search, the Slack target, and `maxChargeUsd`, the most one run may cost.\n4. Run once by hand, check the result, then activate.\n\n### Cost\nAbout $0.037 per run at most on Apify's free plan for up to 50 ads (the first run returns all active ads); `maxChargeUsd` caps every run.\n\n### Limitations\nNeeds an Apify account (Apify bills the Actor's results to it) and the Apify community node. Runs that fail or hit `maxChargeUsd` stop the workflow at the Apify node; check the run in Apify Console.\n\n### Customization\nUse the brand's Facebook page ID (from its Ad Library URL) in `brand` for an exact match instead of a keyword, or set `country` to a two-letter code.",
        "height": 860,
        "width": 640
      }
    },
    {
      "id": "916621d2-1062-5cbc-b619-b7716d538053",
      "name": "Run the Actor",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        220,
        140
      ],
      "parameters": {
        "content": "## Run the Actor\nEdit the search and the spend cap in **Set search**. The Apify node waits for the run and returns its results.",
        "height": 340,
        "width": 540,
        "color": 7
      }
    },
    {
      "id": "08cd26b1-95ef-57fe-ae96-93331d84a9ee",
      "name": "Deliver",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        740,
        140
      ],
      "parameters": {
        "content": "## Deliver\nKeep result rows, shape them and send them to Slack. Nothing is sent when there is nothing new.",
        "height": 340,
        "width": 540,
        "color": 7
      }
    },
    {
      "id": "743d461c-3ee1-5a8f-92d8-a74d18a320b5",
      "name": "Every day at 07:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 7
            }
          ]
        }
      }
    },
    {
      "id": "a539394c-abe9-5bc2-a006-7283604a4581",
      "name": "Set search",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        260,
        300
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "f48862da-fe1d-5469-8b48-0e4b5469a5df",
              "name": "brand",
              "value": "gymshark",
              "type": "string"
            },
            {
              "id": "6a8ba766-5919-5ebb-892d-b29cc05cf28a",
              "name": "country",
              "value": "ALL",
              "type": "string"
            },
            {
              "id": "e42fde29-2d47-5a44-88ad-0ed371f9f2b2",
              "name": "maxAdsPerRun",
              "value": 50,
              "type": "number"
            },
            {
              "id": "87ff0028-786d-5da3-8099-52aa1d3be2df",
              "name": "maxChargeUsd",
              "value": 0.1,
              "type": "number"
            },
            {
              "id": "267fb14b-0dfe-5901-af42-c9a5be16493f",
              "name": "slackChannel",
              "value": "#alerts",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "2ad0661c-24d7-5532-9e12-a5b0523ec6a6",
      "name": "Run Facebook Ad Library Scraper",
      "type": "@apify/n8n-nodes-apify.apify",
      "typeVersion": 1,
      "position": [
        520,
        300
      ],
      "parameters": {
        "resource": "Actors",
        "operation": "Run actor and get dataset",
        "actorSource": "store",
        "actorId": {
          "__rl": true,
          "mode": "list",
          "value": "WrqEXM79iCQCXEYvq",
          "cachedResultName": "Facebook Ad Library Scraper (aurelix/facebook-ad-library-scraper)",
          "cachedResultUrl": "https://console.apify.com/actors/WrqEXM79iCQCXEYvq/input"
        },
        "actorInputMode": "json",
        "customBody": "={{ JSON.stringify({ queries: [$json.brand], country: $json.country, activeStatus: 'active', mediaType: 'all', maxResultsPerQuery: $json.maxAdsPerRun, incremental: true, stateKey: 'n8n-brand-' + $json.brand }) }}",
        "memory": 256,
        "timeout": 900,
        "maxTotalChargeUsd": "={{ $json.maxChargeUsd }}"
      }
    },
    {
      "id": "e2bdd787-486a-56c7-917b-2a4b1dfb7900",
      "name": "Shape results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        780,
        300
      ],
      "parameters": {
        "jsCode": "const rows = $input.all().map(i => i.json).filter(p => p.ad_id);\nif (!rows.length) return [];\nconst lines = rows.map(p => `*${p.page_name}* | ${p.title || (p.ad_text || '').slice(0, 80)} | since ${p.start_date ?? '?'} | ${(p.platforms || []).join(', ')} | <${p.ad_library_url}|ad> ${p.link_url ? '| <' + p.link_url + '|landing page>' : ''}`);\nreturn [{ json: { text: `*New ads* (${rows.length})\\n` + lines.join('\\n') } }];"
      }
    },
    {
      "id": "f845d228-0405-5a7d-a9ed-cf547899809a",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Set search').first().json.slackChannel }}"
        },
        "text": "={{ $json.text }}",
        "otherOptions": {
          "unfurl_links": false
        }
      },
      "position": [
        1040,
        300
      ]
    }
  ],
  "connections": {
    "Every day at 07:00": {
      "main": [
        [
          {
            "node": "Set search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set search": {
      "main": [
        [
          {
            "node": "Run Facebook Ad Library Scraper",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Run Facebook Ad Library Scraper": {
      "main": [
        [
          {
            "node": "Shape results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Shape results": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "tags": []
}
