{
  "name": "Arenza \u2014 Anomaly Alert (SoV drop > 10% WoW)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 */6 * * *"
            }
          ]
        }
      },
      "id": "node-cron",
      "name": "Schedule: every 6 hours",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.arenza.ai/v1/brands",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "retry": {
            "maxRetries": 3,
            "waitBetweenRetries": 5000
          }
        }
      },
      "id": "node-list-brands",
      "name": "GET /v1/brands",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "data",
        "options": {}
      },
      "id": "node-split-brands",
      "name": "Split Brands",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "url": "=https://api.arenza.ai/v1/brands/{{ $json.id }}/overview?compare=wow",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "retry": {
            "maxRetries": 3,
            "waitBetweenRetries": 5000
          }
        }
      },
      "id": "node-overview",
      "name": "GET /brands/:id/overview?compare=wow",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "drop-condition",
              "leftValue": "={{ $json.share_of_voice_delta_pct }}",
              "rightValue": -10,
              "operator": {
                "type": "number",
                "operation": "smaller"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "node-filter",
      "name": "Filter: SoV drop > 10%",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.1,
      "position": [
        1140,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Format an anomaly alert (one item per affected brand).\nconst LLMS = ['ChatGPT', 'Claude', 'Gemini', 'Perplexity', 'Copilot', 'Grok'];\n\nreturn items.map(item => {\n  const d = item.json;\n  const delta = (d.share_of_voice_delta_pct ?? 0).toFixed(1);\n  const current = (d.share_of_voice ?? 0).toFixed(1);\n  const previous = (d.share_of_voice_previous ?? 0).toFixed(1);\n  const worstLLM = (d.worst_llm_change ?? {}).llm ?? 'unknown';\n  const worstDelta = ((d.worst_llm_change ?? {}).delta_pct ?? 0).toFixed(1);\n\n  const blocks = [\n    {\n      type: 'header',\n      text: { type: 'plain_text', text: `[Alert] ${d.name} \u2014 SoV dropped ${delta}% WoW` }\n    },\n    {\n      type: 'section',\n      fields: [\n        { type: 'mrkdwn', text: `*Current SoV*\\n${current}%` },\n        { type: 'mrkdwn', text: `*Previous week*\\n${previous}%` },\n        { type: 'mrkdwn', text: `*Worst LLM*\\n${worstLLM} (${worstDelta}%)` },\n        { type: 'mrkdwn', text: `*Across*\\n${LLMS.join(' \u00b7 ')}` }\n      ]\n    },\n    {\n      type: 'actions',\n      elements: [\n        {\n          type: 'button',\n          text: { type: 'plain_text', text: 'Open in Arenza' },\n          url: `https://app.arenza.ai/brands/${d.id}`,\n          style: 'primary'\n        }\n      ]\n    }\n  ];\n\n  return { json: { channel: '#geo-alerts', blocks } };\n});\n"
      },
      "id": "node-format",
      "name": "Format Alert Blocks",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1360,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": "={{ $json.channel }}",
        "blocksUi": "={{ JSON.stringify($json.blocks) }}",
        "otherOptions": {}
      },
      "id": "node-slack",
      "name": "Slack: chat.postMessage",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        1580,
        300
      ],
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Schedule: every 6 hours": {
      "main": [
        [
          {
            "node": "GET /v1/brands",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GET /v1/brands": {
      "main": [
        [
          {
            "node": "Split Brands",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Brands": {
      "main": [
        [
          {
            "node": "GET /brands/:id/overview?compare=wow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GET /brands/:id/overview?compare=wow": {
      "main": [
        [
          {
            "node": "Filter: SoV drop > 10%",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter: SoV drop > 10%": {
      "main": [
        [
          {
            "node": "Format Alert Blocks",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Format Alert Blocks": {
      "main": [
        [
          {
            "node": "Slack: chat.postMessage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "arenza"
    },
    {
      "name": "geo"
    },
    {
      "name": "anomaly-alert"
    }
  ]
}