{
  "name": "Valkiria: alert when CPA rises",
  "nodes": [
    {
      "parameters": { "rule": { "interval": [{ "field": "cronExpression", "expression": "0 9 * * *" }] } },
      "id": "b2e1d7e3-0002-4b00-9000-000000000001",
      "name": "Every day 09:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "url": "https://valkiria.pro/api/v1/analytics/conversions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": { "parameters": [{ "name": "period", "value": "7d" }] },
        "options": {}
      },
      "id": "b2e1d7e3-0002-4b00-9000-000000000002",
      "name": "CPA last 7 days",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 0],
      "credentials": { "httpHeaderAuth": { "id": "", "name": "Valkiria API key" } }
    },
    {
      "parameters": {
        "url": "https://valkiria.pro/api/v1/analytics/conversions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": { "parameters": [{ "name": "period", "value": "30d" }] },
        "options": {}
      },
      "id": "b2e1d7e3-0002-4b00-9000-000000000003",
      "name": "CPA last 30 days",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 0],
      "credentials": { "httpHeaderAuth": { "id": "", "name": "Valkiria API key" } }
    },
    {
      "parameters": {
        "jsCode": "// Alert when the 7-day CPA is this many times the 30-day CPA, per currency.\nconst RATIO = 1.3;\nconst week = $('CPA last 7 days').first().json.byCurrency || {};\nconst month = $input.first().json.byCurrency || {};\nconst rises = Object.entries(week)\n  .filter(([c, w]) => c !== 'UNKNOWN' && w.cpa !== null && month[c] && month[c].cpa)\n  .map(([c, w]) => ({ c, now: w.cpa, before: month[c].cpa, ratio: w.cpa / month[c].cpa }))\n  .filter((r) => r.ratio >= RATIO);\nif (!rises.length) return [];\nconst text = ['*CPA is up*', ...rises.map((r) => `• ${r.c}: ${r.now.toFixed(2)} over 7 days vs ${r.before.toFixed(2)} over 30 days (x${r.ratio.toFixed(2)})`)].join(String.fromCharCode(10));\nreturn [{ json: { text } }];"
      },
      "id": "b2e1d7e3-0002-4b00-9000-000000000004",
      "name": "Compare CPA",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://hooks.slack.com/services/REPLACE/WITH/YOUR_WEBHOOK",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: $json.text }) }}",
        "options": {}
      },
      "id": "b2e1d7e3-0002-4b00-9000-000000000005",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, 0]
    }
  ],
  "connections": {
    "Every day 09:00": { "main": [[{ "node": "CPA last 7 days", "type": "main", "index": 0 }]] },
    "CPA last 7 days": { "main": [[{ "node": "CPA last 30 days", "type": "main", "index": 0 }]] },
    "CPA last 30 days": { "main": [[{ "node": "Compare CPA", "type": "main", "index": 0 }]] },
    "Compare CPA": { "main": [[{ "node": "Post to Slack", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {}
}
