{
  "name": "TrendTrack — IG Follower Tracker → Google Sheets",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 6
            }
          ]
        }
      },
      "id": "7777ae15-8ee3-488c-8637-67c048c49800",
      "name": "Every Day at 6am",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [240, 304]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "folderName",
              "value": "Demo",
              "type": "string"
            },
            {
              "id": "2",
              "name": "googleSheetId",
              "value": "YOUR_GOOGLE_SHEET_ID",
              "type": "string"
            },
            {
              "id": "3",
              "name": "googleSheetTab",
              "value": "IG Followers",
              "type": "string"
            },
            {
              "id": "4",
              "name": "slackChannelId",
              "value": "C0123456789",
              "type": "string"
            },
            {
              "id": "5",
              "name": "deltaAlertPct",
              "value": 5,
              "type": "number"
            },
            {
              "id": "6",
              "name": "maxBrandsPerRun",
              "value": 100,
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "id": "9e822032-e26a-4c23-8655-b2c835f3f073",
      "name": "Set Parameters",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [464, 304]
    },
    {
      "parameters": {
        "url": "https://api.trendtrack.io/v1/brandtrackers",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "folderNames",
              "value": "={{ $json.folderName }}"
            },
            {
              "name": "limit",
              "value": "={{ $json.maxBrandsPerRun }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {
          "queryParameterArrays": "repeat"
        }
      },
      "id": "06577dda-b381-4c5b-85da-bebe550a3d70",
      "name": "Fetch Brandtrackers In Folder",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [688, 304]
    },
    {
      "parameters": {
        "fieldToSplitOut": "data",
        "options": {}
      },
      "id": "56a405e6-9a0b-498c-a2db-daa66ec91d70",
      "name": "Split Brandtrackers",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [912, 304]
    },
    {
      "parameters": {
        "url": "https://api.trendtrack.io/v1/lookup/facebook-shop",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "facebookPageId",
              "value": "={{ $json.facebookPageId }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "f2a62c3f-0d2c-4fc3-a7e6-489002eb465d",
      "name": "Resolve Shop From BT",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [1120, 304],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "shopId",
              "value": "={{ $json.data?.primaryShop?.id || \"\" }}",
              "type": "string"
            },
            {
              "id": "2",
              "name": "shopDomain",
              "value": "={{ $json.data?.primaryShop?.domain || \"\" }}",
              "type": "string"
            },
            {
              "id": "3",
              "name": "brand",
              "value": "={{ $('Split Brandtrackers').item.json.name || \"\" }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "2ab56799-0b2a-484a-9178-bc897fd9fac4",
      "name": "Extract ShopId",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1328, 304],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "conditions": {
          "combinator": "and",
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "1",
              "leftValue": "={{ $json.shopId }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            }
          ]
        },
        "options": {}
      },
      "id": "4f2eba8a-17eb-441d-968d-b4e3c831d560",
      "name": "Has ShopId",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [1536, 304]
    },
    {
      "parameters": {
        "url": "={{ 'https://api.trendtrack.io/v1/shops/' + $json.shopId + '/socials/history' }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "period",
              "value": "day"
            },
            {
              "name": "days",
              "value": "8"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "48e45978-c917-48e8-9952-e4a3dd81934b",
      "name": "Fetch Social History",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [1760, 304]
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const item = $input.item.json;\nconst brand = $('Extract ShopId').item.json.brand;\nconst igPoints = item.data?.instagram || [];\nconst fbPoints = item.data?.facebook || [];\nconst sortByDate = (arr) => [...arr].sort((a, b) => new Date(a.period) - new Date(b.period));\nconst igSorted = sortByDate(igPoints);\nconst fbSorted = sortByDate(fbPoints);\nconst igLatest = igSorted[igSorted.length - 1]?.value ?? null;\nconst igWeekAgo = igSorted[0]?.value ?? null;\nconst fbLatest = fbSorted[fbSorted.length - 1]?.value ?? null;\nconst igDeltaPct = (igLatest != null && igWeekAgo != null && igWeekAgo > 0) ? ((igLatest - igWeekAgo) / igWeekAgo) * 100 : 0;\nreturn { json: { date: $now.toFormat('yyyy-MM-dd'), brand, ig_followers: igLatest, fb_followers: fbLatest, delta_pct: Math.round(igDeltaPct * 100) / 100 } };"
      },
      "id": "a1389c76-d0b0-4d86-aeed-fa8c4b3979e4",
      "name": "Compute Latest + Delta",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1984, 304],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Set Parameters').first().json.googleSheetId }}"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Set Parameters').first().json.googleSheetTab }}"
        },
        "columns": "{{ ({ mappingMode: 'autoMapInputData', value: null, matchingColumns: [], schema: [] }) }}",
        "options": {
          "useAppend": true
        }
      },
      "id": "3ac99d14-1e4a-49bd-9094-a985391e6e33",
      "name": "Append Sheets Row",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [2208, 304]
    },
    {
      "parameters": {
        "conditions": {
          "combinator": "and",
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "1",
              "leftValue": "={{ $json.delta_pct }}",
              "rightValue": "={{ $('Set Parameters').first().json.deltaAlertPct }}",
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ]
        },
        "options": {}
      },
      "id": "f5409f82-20dd-4c98-ab1f-5628f96f3ed4",
      "name": "Spike > Threshold ?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [2432, 304]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Set Parameters').first().json.slackChannelId }}"
        },
        "text": "={{ '🚀 IG follower spike — *' + $json.brand + '* +' + $json.delta_pct + '% over 7d (now ' + $json.ig_followers + ' followers)' }}",
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "id": "5ea9608e-3c48-4bf2-8a93-380650992df7",
      "name": "Slack Spike Alert",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.4,
      "position": [2656, 208]
    }
  ],
  "connections": {
    "Every Day at 6am": {
      "main": [
        [
          {
            "node": "Set Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Parameters": {
      "main": [
        [
          {
            "node": "Fetch Brandtrackers In Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Brandtrackers In Folder": {
      "main": [
        [
          {
            "node": "Split Brandtrackers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Brandtrackers": {
      "main": [
        [
          {
            "node": "Resolve Shop From BT",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Resolve Shop From BT": {
      "main": [
        [
          {
            "node": "Extract ShopId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract ShopId": {
      "main": [
        [
          {
            "node": "Has ShopId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has ShopId": {
      "main": [
        [
          {
            "node": "Fetch Social History",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Social History": {
      "main": [
        [
          {
            "node": "Compute Latest + Delta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compute Latest + Delta": {
      "main": [
        [
          {
            "node": "Append Sheets Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append Sheets Row": {
      "main": [
        [
          {
            "node": "Spike > Threshold ?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spike > Threshold ?": {
      "main": [
        [
          {
            "node": "Slack Spike Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": true
  },
  "tags": []
}
