{
  "name": "QRlytics Festival Visitor Monitoring",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "qrlytics-festival-scan",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-1",
      "name": "Festival Scan Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300],
      "webhookId": "qrlytics-festival"
    },
    {
      "parameters": {
        "jsCode": "// Parse QRlytics webhook data\nconst scan = $input.first().json;\nconst now = new Date(scan.scanned_at || new Date().toISOString());\n\n// Extract time components for flow analysis\nconst hour = now.getHours();\nconst dayOfWeek = now.toLocaleDateString('en-US', { weekday: 'long' });\nconst dateStr = now.toISOString().split('T')[0];\n\nreturn [{\n  json: {\n    event_id: scan.qr_code_id || 'FESTIVAL-2026',\n    event_name: scan.qr_code_name || 'Festival Entrance',\n    scanned_at: now.toISOString(),\n    date: dateStr,\n    hour: hour,\n    hour_label: `${hour}:00 - ${hour}:59`,\n    day_of_week: dayOfWeek,\n    location: scan.location ? `${scan.location.city}, ${scan.location.country}` : 'Unknown',\n    device: scan.device?.type || 'Unknown',\n    is_unique: scan.is_unique || false,\n    scan_count: 1\n  }\n}];"
      },
      "id": "code-1",
      "name": "Parse Scan Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [460, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Festival Visitors",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Event ID": "={{ $json.event_id }}",
            "Event Name": "={{ $json.event_name }}",
            "Scanned At": "={{ $json.scanned_at }}",
            "Date": "={{ $json.date }}",
            "Hour": "={{ $json.hour }}",
            "Hour Label": "={{ $json.hour_label }}",
            "Day of Week": "={{ $json.day_of_week }}",
            "Location": "={{ $json.location }}",
            "Device": "={{ $json.device }}",
            "Is Unique": "={{ $json.is_unique }}"
          }
        },
        "options": {}
      },
      "id": "sheets-1",
      "name": "Log to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [680, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Google Sheets"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, message: 'Scan logged successfully', timestamp: $('Parse Scan Data').item.json.scanned_at }) }}",
        "options": {}
      },
      "id": "respond-1",
      "name": "Respond Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [900, 300]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "triggerAtHour": 23
            }
          ]
        }
      },
      "id": "schedule-1",
      "name": "Daily Summary Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [240, 520]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Festival Visitors",
          "mode": "list"
        },
        "options": {
          "range": "A:J"
        }
      },
      "id": "sheets-2",
      "name": "Read Today's Data",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [460, 520],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Google Sheets"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Generate daily summary\nconst items = $input.all();\nconst today = new Date().toISOString().split('T')[0];\n\n// Filter today's scans\nconst todayScans = items.filter(item => item.json['Date'] === today);\n\n// Count by hour\nconst hourCounts = {};\ntodayScans.forEach(scan => {\n  const hour = scan.json['Hour Label'];\n  hourCounts[hour] = (hourCounts[hour] || 0) + 1;\n});\n\n// Find peak and low hours\nconst hours = Object.entries(hourCounts);\nconst peakHour = hours.length ? hours.reduce((a, b) => a[1] > b[1] ? a : b) : ['N/A', 0];\nconst lowHour = hours.length ? hours.reduce((a, b) => a[1] < b[1] ? a : b) : ['N/A', 0];\n\nreturn [{\n  json: {\n    date: today,\n    total_visitors: todayScans.length,\n    unique_visitors: todayScans.filter(s => s.json['Is Unique'] === true || s.json['Is Unique'] === 'true').length,\n    peak_hour: peakHour[0],\n    peak_count: peakHour[1],\n    low_hour: lowHour[0],\n    low_count: lowHour[1],\n    hourly_breakdown: JSON.stringify(hourCounts)\n  }\n}];"
      },
      "id": "code-2",
      "name": "Calculate Daily Stats",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 520]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "YOUR_SLACK_CHANNEL_ID",
          "mode": "id"
        },
        "text": "=:bar_chart: *Festival Daily Report - {{ $json.date }}*\n\n:busts_in_silhouette: *Total Visitors:* {{ $json.total_visitors }}\n:star: *Unique Visitors:* {{ $json.unique_visitors }}\n\n:chart_with_upwards_trend: *Peak Hour:* {{ $json.peak_hour }} ({{ $json.peak_count }} visitors)\n:chart_with_downwards_trend: *Low Hour:* {{ $json.low_hour }} ({{ $json.low_count }} visitors)\n\n_Generated automatically by QRlytics_",
        "otherOptions": {}
      },
      "id": "slack-1",
      "name": "Send Daily Report",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [900, 520],
      "credentials": {
        "slackApi": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Slack"
        }
      }
    }
  ],
  "connections": {
    "Festival Scan Webhook": {
      "main": [[{ "node": "Parse Scan Data", "type": "main", "index": 0 }]]
    },
    "Parse Scan Data": {
      "main": [[{ "node": "Log to Google Sheets", "type": "main", "index": 0 }]]
    },
    "Log to Google Sheets": {
      "main": [[{ "node": "Respond Success", "type": "main", "index": 0 }]]
    },
    "Daily Summary Trigger": {
      "main": [[{ "node": "Read Today's Data", "type": "main", "index": 0 }]]
    },
    "Read Today's Data": {
      "main": [[{ "node": "Calculate Daily Stats", "type": "main", "index": 0 }]]
    },
    "Calculate Daily Stats": {
      "main": [[{ "node": "Send Daily Report", "type": "main", "index": 0 }]]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "qrlytics-template"
  },
  "tags": [
    { "name": "QRlytics" },
    { "name": "Festival" },
    { "name": "Visitor Monitoring" }
  ]
}
