{
  "id": "DxYN54X8SAzfDq3l",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI-Powered Competitor Trends & Strategy Alert System for Instagram",
  "tags": [],
  "nodes": [
    {
      "id": "f6c99e64-9618-4af5-aed8-183fd93eee6f",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.cron",
      "position": [
        -704,
        80
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "4bcd51e8-2fe4-47ec-8373-87cfa99cc51b",
      "name": "Get Competitor List",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -480,
        80
      ],
      "parameters": {
        "options": {},
        "sheetId": "your-competitors-sheet-id!A:C"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "1e12d7a7-a5e2-4346-a05d-86960a593ea7",
      "name": "Loop Over Competitors",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -256,
        80
      ],
      "parameters": {
        "options": {},
        "batchSize": 1
      },
      "typeVersion": 1
    },
    {
      "id": "cf2b7ce8-b8c2-451d-b7d1-0549f048e09b",
      "name": "Get Competitor Posts",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -32,
        80
      ],
      "parameters": {
        "url": "=https://graph.facebook.com/v12.0/{{ $json[\"competitorUserId\"] }}/media?fields=id,media_type,media_url,like_count,comments_count,caption,timestamp&access_token=YOUR_INSTAGRAM_ACCESS_TOKEN&limit=10",
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "bec4e963-91f8-485d-8175-80321f9d7f1e",
      "name": "Calculate Performance Metrics",
      "type": "n8n-nodes-base.code",
      "position": [
        192,
        80
      ],
      "parameters": {
        "jsCode": "// Calculate average engagement rate (likes + comments) / 10 posts\nconst posts = $json['data'] || [];\nlet totalLikes = 0, totalComments = 0, totalPosts = posts.length;\n\nposts.forEach(post => {\n  totalLikes += post.like_count || 0;\n  totalComments += post.comments_count || 0;\n});\n\nconst avgEngagement = totalPosts > 0 ? (totalLikes + totalComments) / totalPosts : 0;\nconst trend = avgEngagement > 50 ? 'Rising' : 'Stable'; // Simple threshold\n\nreturn {\n  json: {\n    ...$json,\n    avgEngagement,\n    trend,\n    competitorName: $input.item.json.competitorName\n  }\n};"
      },
      "typeVersion": 1
    },
    {
      "id": "28319c72-8fa6-4b3a-9f17-3b068e0351fe",
      "name": "Generate AI Insights (Claude AI)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        416,
        80
      ],
      "parameters": {
        "url": "https://api.anthropic.com/v1/messages",
        "options": {},
        "requestMethod": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "={\"model\": \"claude-3-5-sonnet-20241022\", \"max_tokens\": 1024, \"messages\": [{\"role\": \"user\", \"content\": \"Analyze these competitor Instagram trends: {{ JSON.stringify($json) }}. Provide 3 strategic insights to stay ahead, focusing on content strategy, posting frequency, and engagement tactics. Output as bullet points.\"}]}"
      },
      "typeVersion": 1
    },
    {
      "id": "965cc026-e14b-45a9-8645-e060199498c0",
      "name": "Send Email Alert",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        640,
        -112
      ],
      "parameters": {
        "options": {},
        "subject": "=Competitor Trends Alert: {{ $node[\"Loop Over Competitors\"].json[\"competitorName\"] }} - {{ $json[\"trend\"] }} Engagement",
        "toEmail": "user@example.com",
        "fromEmail": "user@example.com"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "25703d3a-af25-499f-9e69-dd5c5f5ed9a9",
      "name": "Send WhatsApp Alert (Twilio)",
      "type": "n8n-nodes-base.twilio",
      "position": [
        640,
        80
      ],
      "parameters": {
        "to": "+1234567890",
        "from": "+1234567890",
        "message": "=\ud83d\udea8 Competitor Alert: {{ $node[\"Loop Over Competitors\"].json[\"competitorName\"] }} | Engagement: {{ $json[\"avgEngagement\"] }} | Trend: {{ $json[\"trend\"] }}\n\nInsights:\n{{ $json[\"content\"][0][\"text\"].substring(0, 100) }}...",
        "options": {}
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "c51d8a17-7d50-451a-8b0c-3d9198c79fd3",
      "name": "Log Alert",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        640,
        272
      ],
      "parameters": {
        "options": {},
        "sheetId": "your-alerts-log-sheet-id!A:E",
        "operation": "append"
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "38ecc1c4-4bbe-4e32-8d2d-48e03fb50db7",
      "name": "End Workflow",
      "type": "n8n-nodes-base.set",
      "position": [
        864,
        80
      ],
      "parameters": {
        "options": {},
        "keepOnlySet": true
      },
      "typeVersion": 1
    },
    {
      "id": "3caf3759-cf89-45fa-a86e-5926540a4393",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -736,
        -80
      ],
      "parameters": {
        "width": 176,
        "height": 304,
        "content": "Runs daily at 10 AM or via webhook (/competitor-alert)\n"
      },
      "typeVersion": 1
    },
    {
      "id": "15b0107a-bdef-41ca-93e5-8698cde79fd9",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -288,
        -80
      ],
      "parameters": {
        "width": 176,
        "height": 304,
        "content": "Processes each competitor to avoid API limits\n"
      },
      "typeVersion": 1
    },
    {
      "id": "e72a0fd9-0f52-4d6f-9550-d1cb9a45dc28",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        -80
      ],
      "parameters": {
        "width": 176,
        "height": 304,
        "content": "Computes avg engagement and trend using Code node\n"
      },
      "typeVersion": 1
    },
    {
      "id": "4335d38f-6d10-4372-8a85-bff4eeb40efb",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        -80
      ],
      "parameters": {
        "width": 176,
        "height": 304,
        "content": "Fetches last 10 posts via Instagram Graph API\n"
      },
      "typeVersion": 1
    },
    {
      "id": "ab3a4d4a-785f-4357-985c-ee698393437f",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -512,
        -80
      ],
      "parameters": {
        "width": 176,
        "height": 304,
        "content": "Loads competitors from Competitors sheet\n"
      },
      "typeVersion": 1
    },
    {
      "id": "f383e5df-cc87-4244-ba1e-cf8ac4e31ea2",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        -320
      ],
      "parameters": {
        "width": 176,
        "height": 768,
        "content": "Emails detailed report to team\n\nSends concise alert via WhatsApp\n\nRecords metrics and insights in AlertsLog sheet\n"
      },
      "typeVersion": 1
    },
    {
      "id": "cb90b7f3-157d-427a-931e-178307ba6d27",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        -80
      ],
      "parameters": {
        "width": 176,
        "height": 304,
        "content": "Analyzes data for 3 strategic bullet-point insights\n"
      },
      "typeVersion": 1
    },
    {
      "id": "5a936a09-a87c-4559-ace4-052966bc3abe",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        832,
        -80
      ],
      "parameters": {
        "width": 176,
        "height": 304,
        "content": "Terminates execution\n"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "556221cf-0c2d-44ce-b34d-cead62cd5fb5",
  "connections": {
    "Log Alert": {
      "main": [
        [
          {
            "node": "End Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Competitor List",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email Alert": {
      "main": [
        [
          {
            "node": "End Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Competitor List": {
      "main": [
        [
          {
            "node": "Loop Over Competitors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Competitor Posts": {
      "main": [
        [
          {
            "node": "Calculate Performance Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Competitors": {
      "main": [
        [
          {
            "node": "Get Competitor Posts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp Alert (Twilio)": {
      "main": [
        [
          {
            "node": "End Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Performance Metrics": {
      "main": [
        [
          {
            "node": "Generate AI Insights (Claude AI)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate AI Insights (Claude AI)": {
      "main": [
        [
          {
            "node": "Send Email Alert",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send WhatsApp Alert (Twilio)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}