{
  "name": "07 · Data & Analytics / Product Event → Daily Facts",
  "active": false,
  "nodes": [
    {
      "id": "a9906d34-b74a-47ed-9aa8-83d8db899fe6",
      "name": "📘 Guide",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -640,
        -120
      ],
      "parameters": {
        "content": "## 07 · Data & Analytics · Product Event → Daily Facts\n\n### What this does\nIngests product events webhook, aggregates daily facts, and posts to analytics DB.\n\n### Setup checklist\n1. Event schema\n2. Aggregation rules\n3. DB write HTTP\n\n### How to test locally\nPOST events to `data-product-events`.\n\n### Tips\n- Keep credentials in n8n Credentials (never hardcode secrets).\n- Use **Test URL** on the Webhook while developing.\n- Activate only after credentials + destinations are configured.\n",
        "width": 560,
        "height": 560,
        "color": 7
      }
    },
    {
      "id": "b8dad64b-6f63-4785-9579-f4b2704306fc",
      "name": "⚙️ Config",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -640,
        480
      ],
      "parameters": {
        "content": "## Config map\n\n- **Webhook path:** `data-product-events`\n- **Replace** all `https://httpbin.org/...` calls with real integrations.\n- **Credentials:** create in n8n → Credentials, then select on HTTP nodes.\n- **Activate** workflow only after a successful Test execution.\n\n### Local files\nShared host folder is mounted at `/files` inside n8n.",
        "width": 560,
        "height": 320,
        "color": 2
      }
    },
    {
      "id": "3af27a71-da40-47e7-8bd7-a669d67dfa3c",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        200
      ],
      "webhookId": "3253fe52-30e5-41aa-a626-9edb0896980a",
      "parameters": {
        "httpMethod": "POST",
        "path": "data-product-events",
        "responseMode": "responseNode",
        "options": {}
      }
    },
    {
      "id": "45478a0f-d4e5-4f4c-ac22-0bb6ee2f2b72",
      "name": "Normalize Payload",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        280,
        200
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "receivedAt",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            },
            {
              "id": "2",
              "name": "source",
              "value": "07 · Data & Analytics",
              "type": "string"
            },
            {
              "id": "3",
              "name": "workflow",
              "value": "Product Event → Daily Facts",
              "type": "string"
            },
            {
              "id": "4",
              "name": "payload",
              "value": "={{ $json.body || $json }}",
              "type": "object"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "29f2a5e3-ab54-4cec-ada9-d685c0166ee1",
      "name": "Business Logic",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        560,
        200
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nconst payload = item.payload || item;\nconst platforms = payload.platforms || ['primary', 'secondary', 'archive'];\nconst jobs = (Array.isArray(platforms) ? platforms : [platforms]).map((p, idx) => ({\n  json: {\n    ...item,\n    channel: p,\n    jobId: `${Date.now()}-${idx}`,\n    status: 'queued',\n    transformed: {\n      title: payload.title || payload.caption || payload.subject || defTitle(),\n      body: payload.body || payload.message || payload.caption || JSON.stringify(payload),\n      meta: payload.meta || {},\n    },\n  },\n}));\nreturn jobs.length ? jobs : [{ json: { ...item, channel: 'primary', status: 'queued' } }];\nfunction defTitle(){ return 'Product Event → Daily Facts'; }\n"
      }
    },
    {
      "id": "f3404ac5-8800-453e-9f82-db19593883cc",
      "name": "Ready to Dispatch?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        840,
        200
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "384b9d76-1103-4cde-b588-5c0b05e1db38",
              "leftValue": "={{ $json.status === \"queued\" }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "a05c7f3f-403d-40f9-b7e0-167c024b96c5",
      "name": "Dispatch A (replace me)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        80
      ],
      "parameters": {
        "method": "POST",
        "url": "https://httpbin.org/post",
        "options": {},
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}"
      }
    },
    {
      "id": "625566d4-4d54-4076-86e5-405b0dbd9d58",
      "name": "Dispatch B (replace me)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        320
      ],
      "parameters": {
        "method": "POST",
        "url": "https://httpbin.org/post",
        "options": {},
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}"
      }
    },
    {
      "id": "b0bb00e4-e2d7-4db6-bf5a-d67068e6502e",
      "name": "Aggregate Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1400,
        200
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const items = $input.all().map(i => i.json);\nreturn [{ json: { ok: true, workflow: 'Product Event → Daily Facts', count: items.length, results: items } }];"
      }
    },
    {
      "id": "de968505-18a4-430f-a7cf-d81b49a33b50",
      "name": "Webhook Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1680,
        200
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {}
      }
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Normalize Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Payload": {
      "main": [
        [
          {
            "node": "Business Logic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Business Logic": {
      "main": [
        [
          {
            "node": "Ready to Dispatch?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ready to Dispatch?": {
      "main": [
        [
          {
            "node": "Dispatch A (replace me)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Dispatch B (replace me)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dispatch A (replace me)": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dispatch B (replace me)": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Results": {
      "main": [
        [
          {
            "node": "Webhook Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner"
  },
  "tags": [],
  "meta": {
    "templateCredsSetupCompleted": true,
    "description": "Ingests product events webhook, aggregates daily facts, and posts to analytics DB."
  },
  "pinData": {}
}