{
  "name": "Hashtag and Topic Trend Analyzer",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [{ "field": "hours", "hoursInterval": 12 }]
        }
      },
      "id": "hta0001-0001-4000-8000-000000000001",
      "name": "Schedule Twice Daily",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "url": "=https://api.twitter.com/2/tweets/search/recent?query={{ $env.TRACK_HASHTAG }}&max_results=100&tweet.fields=public_metrics,created_at,entities",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "options": {}
      },
      "id": "hta0001-0002-4000-8000-000000000002",
      "name": "Fetch Recent Tweets",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [460, 300]
    },
    {
      "parameters": {
        "jsCode": "const tweets = $input.item.json.data || [];\nconst totalLikes = tweets.reduce((sum, t) => sum + (t.public_metrics?.like_count || 0), 0);\nconst totalRetweets = tweets.reduce((sum, t) => sum + (t.public_metrics?.retweet_count || 0), 0);\nconst avgEngagement = tweets.length > 0 ? Math.round((totalLikes + totalRetweets) / tweets.length) : 0;\nconst topTweet = tweets.sort((a, b) => (b.public_metrics?.like_count || 0) - (a.public_metrics?.like_count || 0))[0];\nconst allHashtags = tweets.flatMap(t => (t.entities?.hashtags || []).map(h => h.tag.toLowerCase()));\nconst hashtagFreq = allHashtags.reduce((acc, h) => { acc[h] = (acc[h] || 0) + 1; return acc; }, {});\nconst topHashtags = Object.entries(hashtagFreq).sort((a, b) => b[1] - a[1]).slice(0, 10).map(([tag, count]) => ({ tag, count }));\nreturn [{ json: { tweet_count: tweets.length, total_likes: totalLikes, total_retweets: totalRetweets, avg_engagement: avgEngagement, top_tweet_text: topTweet?.text || '', top_tweet_likes: topTweet?.public_metrics?.like_count || 0, top_hashtags: topHashtags, analyzed_at: new Date().toISOString() } }];"
      },
      "id": "hta0001-0003-4000-8000-000000000003",
      "name": "Compute Trend Metrics",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "resource": "text",
        "operation": "message",
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "content": "Analyze these social media trend metrics and provide a brief report (max 200 words): identify what the engagement numbers suggest about audience interest, highlight the top co-occurring hashtags and what they reveal about the conversation, and suggest 2-3 content ideas to capitalize on these trends.\n\nMetrics: {{ JSON.stringify($json) }}"
            }
          ]
        },
        "options": {
          "temperature": 0.5
        }
      },
      "id": "hta0001-0004-4000-8000-000000000004",
      "name": "Generate Trend Insights",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1,
      "position": [900, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{ $env.TREND_REPORT_SHEET_ID }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Reports",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "analyzed_at": "={{ $('Compute Trend Metrics').item.json.analyzed_at }}",
            "tweet_count": "={{ $('Compute Trend Metrics').item.json.tweet_count }}",
            "avg_engagement": "={{ $('Compute Trend Metrics').item.json.avg_engagement }}",
            "insights": "={{ $json.message.content }}"
          }
        },
        "options": {}
      },
      "id": "hta0001-0005-4000-8000-000000000005",
      "name": "Log Report to Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1120, 300]
    }
  ],
  "connections": {
    "Schedule Twice Daily": {
      "main": [[{ "node": "Fetch Recent Tweets", "type": "main", "index": 0 }]]
    },
    "Fetch Recent Tweets": {
      "main": [[{ "node": "Compute Trend Metrics", "type": "main", "index": 0 }]]
    },
    "Compute Trend Metrics": {
      "main": [[{ "node": "Generate Trend Insights", "type": "main", "index": 0 }]]
    },
    "Generate Trend Insights": {
      "main": [[{ "node": "Log Report to Sheets", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "meta": {},
  "pinData": {}
}
