{
  "name": "Survey Response Analyzer with AI",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "analyze-survey",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000001",
      "name": "Webhook - Survey Data",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "content": "=You are a survey analysis expert. Analyze the following survey responses and provide:\n\n1. **Sentiment Analysis**: For each response, classify sentiment as positive, neutral, or negative with a confidence score (0-1).\n2. **Theme Extraction**: Identify the top 5 recurring themes across all responses.\n3. **Key Insights**: Provide 3-5 actionable insights based on the data.\n4. **Overall Satisfaction Score**: Calculate an aggregate score from 1-10.\n5. **Response Categories**: Group responses into logical categories.\n\nReturn as JSON with fields: sentiments (array), themes (array), insights (array), overallScore (number), categories (object).\n\nSurvey Responses:\n{{ JSON.stringify($json.body.responses) }}"
            }
          ]
        },
        "options": {
          "temperature": 0.3,
          "maxTokens": 4096,
          "responseFormat": "json_object"
        }
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000002",
      "name": "OpenAI - Analyze Sentiment & Themes",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.4,
      "position": [
        480,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const aiResponse = $input.first().json.message.content;\nlet analysis;\ntry {\n  analysis = JSON.parse(aiResponse);\n} catch (e) {\n  analysis = { raw: aiResponse, parseError: true };\n}\nconst timestamp = new Date().toISOString();\nconst surveyId = $('Webhook - Survey Data').first().json.body.surveyId || 'unknown';\nreturn [{ json: { surveyId, timestamp, ...analysis } }];"
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000003",
      "name": "Code - Format Analysis",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Survey Analysis"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Survey ID": "={{ $json.surveyId }}",
            "Timestamp": "={{ $json.timestamp }}",
            "Overall Score": "={{ $json.overallScore }}",
            "Top Themes": "={{ $json.themes ? $json.themes.join(', ') : '' }}",
            "Key Insights": "={{ $json.insights ? $json.insights.join(' | ') : '' }}",
            "Positive Count": "={{ $json.sentiments ? $json.sentiments.filter(s => s.sentiment === 'positive').length : 0 }}",
            "Negative Count": "={{ $json.sentiments ? $json.sentiments.filter(s => s.sentiment === 'negative').length : 0 }}",
            "Neutral Count": "={{ $json.sentiments ? $json.sentiments.filter(s => s.sentiment === 'neutral').length : 0 }}"
          }
        },
        "options": {}
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000004",
      "name": "Google Sheets - Store Results",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        960,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "channel": "#survey-results",
        "text": "=:bar_chart: *Survey Analysis Complete*\n\n*Survey ID:* {{ $('Code - Format Analysis').first().json.surveyId }}\n*Overall Score:* {{ $('Code - Format Analysis').first().json.overallScore }}/10\n\n*Top Themes:*\n{{ $('Code - Format Analysis').first().json.themes ? $('Code - Format Analysis').first().json.themes.map((t, i) => `${i+1}. ${t}`).join('\\n') : 'None identified' }}\n\n*Key Insights:*\n{{ $('Code - Format Analysis').first().json.insights ? $('Code - Format Analysis').first().json.insights.map(i => `\u2022 ${i}`).join('\\n') : 'None' }}\n\n*Sentiment Breakdown:* :thumbsup: {{ $('Code - Format Analysis').first().json.sentiments ? $('Code - Format Analysis').first().json.sentiments.filter(s => s.sentiment === 'positive').length : 0 }} | :neutral_face: {{ $('Code - Format Analysis').first().json.sentiments ? $('Code - Format Analysis').first().json.sentiments.filter(s => s.sentiment === 'neutral').length : 0 }} | :thumbsdown: {{ $('Code - Format Analysis').first().json.sentiments ? $('Code - Format Analysis').first().json.sentiments.filter(s => s.sentiment === 'negative').length : 0 }}",
        "otherOptions": {}
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000005",
      "name": "Slack - Post Summary",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        1200,
        300
      ],
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, surveyId: $('Code - Format Analysis').first().json.surveyId, overallScore: $('Code - Format Analysis').first().json.overallScore, themes: $('Code - Format Analysis').first().json.themes, insights: $('Code - Format Analysis').first().json.insights }) }}"
      },
      "id": "c3d4e5f6-3333-4000-8000-000000000006",
      "name": "Respond with Analysis",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1440,
        300
      ]
    }
  ],
  "connections": {
    "Webhook - Survey Data": {
      "main": [
        [
          {
            "node": "OpenAI - Analyze Sentiment & Themes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI - Analyze Sentiment & Themes": {
      "main": [
        [
          {
            "node": "Code - Format Analysis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code - Format Analysis": {
      "main": [
        [
          {
            "node": "Google Sheets - Store Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Store Results": {
      "main": [
        [
          {
            "node": "Slack - Post Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slack - Post Summary": {
      "main": [
        [
          {
            "node": "Respond with Analysis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "ai-data"
    }
  ]
}