{
  "name": "TrendTrack — Brand 360 Report → Notion + Email",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "trendtrack-brand-report",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "237cd140-9796-467c-baa4-aafe3419c74a",
      "name": "Brand Report Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [208, 608]
    },
    {
      "parameters": {
        "url": "https://api.trendtrack.io/v1/lookup",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ $json.body.brandName }}"
            },
            {
              "name": "type",
              "value": "auto"
            },
            {
              "name": "limit",
              "value": "1"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "04ba30c6-a8ea-4f6e-929f-239c4c8a7dca",
      "name": "Lookup Brand",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [400, 608]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "brandtrackerId",
              "value": "={{ $json.data?.[0]?.brandtracker?.id || \"\" }}",
              "type": "string"
            },
            {
              "id": "2",
              "name": "shopId",
              "value": "={{ $json.data?.[0]?.shop?.id || \"\" }}",
              "type": "string"
            },
            {
              "id": "3",
              "name": "brandName",
              "value": "={{ $json.data?.[0]?.brandtracker?.name || $json.data?.[0]?.shop?.name || \"\" }}",
              "type": "string"
            },
            {
              "id": "4",
              "name": "recipientEmail",
              "value": "={{ $('Brand Report Webhook').first().json.body?.recipientEmail || '' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "03aba812-87be-4e93-8f64-6cb64fad9bf1",
      "name": "Extract IDs",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [624, 608],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "url": "={{ 'https://api.trendtrack.io/v1/brandtrackers/' + $json.brandtrackerId + '/overview' }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "edb37a6a-0f38-4b15-bda4-21d9e4e13249",
      "name": "Fetch Overview",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [880, 112]
    },
    {
      "parameters": {
        "numberInputs": 6
      },
      "id": "4708243b-3a9c-4242-8bc9-2ad960258ed4",
      "name": "Wait for All",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [1152, 608]
    },
    {
      "parameters": {
        "url": "={{ 'https://api.trendtrack.io/v1/brandtrackers/' + $json.brandtrackerId + '/top-ads' }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "sortBy",
              "value": "reach"
            },
            {
              "name": "limit",
              "value": "10"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "df6d22dd-9138-4435-b272-ef6586485d47",
      "name": "Fetch Top Ads",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [880, 256]
    },
    {
      "parameters": {
        "url": "={{ 'https://api.trendtrack.io/v1/brandtrackers/' + $json.brandtrackerId + '/scaling-ads' }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "sortBy",
              "value": "rankDelta"
            },
            {
              "name": "timeWindow",
              "value": "last7d"
            },
            {
              "name": "limit",
              "value": "10"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "25d3ec9b-f3f7-4a1d-9cd7-de5936fb08a8",
      "name": "Fetch Scaling Ads",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [880, 400]
    },
    {
      "parameters": {
        "url": "={{ 'https://api.trendtrack.io/v1/brandtrackers/' + $json.brandtrackerId + '/hooks' }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "sortBy",
              "value": "totalImpressions"
            },
            {
              "name": "sortOrder",
              "value": "desc"
            },
            {
              "name": "limit",
              "value": "10"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a75531ba-f2cb-4489-8d11-a095afa2b366",
      "name": "Fetch Hooks",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [880, 560]
    },
    {
      "parameters": {
        "url": "={{ 'https://api.trendtrack.io/v1/brandtrackers/' + $json.brandtrackerId + '/landing-pages' }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "sortBy",
              "value": "totalImpressions"
            },
            {
              "name": "sortOrder",
              "value": "desc"
            },
            {
              "name": "limit",
              "value": "10"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "d793a56f-9865-47ab-a95a-8d7b68376981",
      "name": "Fetch Landing Pages",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [880, 704]
    },
    {
      "parameters": {
        "url": "={{ 'https://api.trendtrack.io/v1/brandtrackers/' + $json.brandtrackerId + '/media-mix' }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.TRENDTRACK_API_KEY }}"
            }
          ]
        },
        "options": {}
      },
      "id": "bedb7ce6-e2be-465b-aeea-39350c4b0102",
      "name": "Fetch Media Mix",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [880, 864]
    },
    {
      "parameters": {
        "jsCode": "const ids = $('Extract IDs').first().json;\nconst overview = $('Fetch Overview').first().json.data || {};\nconst topAds = $('Fetch Top Ads').first().json.data || [];\nconst scaling = $('Fetch Scaling Ads').first().json.data || [];\nconst hooks = $('Fetch Hooks').first().json.data || [];\nconst lps = $('Fetch Landing Pages').first().json.data || [];\nconst mediaMixWrap = $('Fetch Media Mix').first().json.data || {};\nconst formatCount = mediaMixWrap.formatCount || overview.mediaMix?.formatCount || {};\nconst fmt = (n) => { if (n == null) return '?'; const a = Math.abs(n); if (a >= 1e6) return (n/1e6).toFixed(1)+'M'; if (a >= 1e3) return (n/1e3).toFixed(1)+'k'; return String(n); };\nconst total = (formatCount.image || 0) + (formatCount.video || 0) + (formatCount.dco || 0) + (formatCount.other || 0);\nconst pct = (n) => total > 0 ? Math.round(((n || 0) / total) * 100) + '%' : '?';\nconst watchLink = (a) => { const u = a?.media?.mediaUrl || a?.media?.thumbnailUrl; return u ? ' [Watch ad](' + u + ')' : ''; };\nconst dateStr = $now.toFormat('yyyy-MM-dd');\nlet md = '# 🎯 Brand 360 — ' + ids.brandName + '\\n_Generated ' + dateStr + '_\\n\\n## 📊 Overview\\n';\nmd += '- Active ads: ' + (mediaMixWrap.activeAds ?? overview.mediaMix?.activeAds ?? overview.adsPreview?.pagination?.total ?? '?') + '\\n';\nmd += '- CPM benchmark: ' + (overview.graph?.cpm != null ? '$' + overview.graph.cpm.toFixed(2) : '?') + '\\n\\n';\nmd += '## 🎬 Media Mix\\n- Image: ' + pct(formatCount.image) + '  · Video: ' + pct(formatCount.video) + '  · DCO: ' + pct(formatCount.dco) + '  · Other: ' + pct(formatCount.other) + '\\n\\n';\nmd += '## 🔥 Top Ads (by reach)\\n';\ntopAds.forEach((it, i) => { md += (i+1) + '. **#' + (it.metrics?.currentRank ?? '?') + '** — reach ' + fmt(it.metrics?.totalReach) + watchLink(it.ad) + '\\n   ' + ((it.ad?.content?.body || '').slice(0, 140)) + '\\n\\n'; });\nmd += '## 🚀 Scaling Ads (7d)\\n';\nscaling.forEach((it, i) => { md += (i+1) + '. #' + (it.metrics?.currentRank ?? '?') + ' (Δ +' + (it.metrics?.rankDelta ?? '?') + ')' + watchLink(it.ad) + ' — ' + ((it.ad?.content?.body || '').slice(0, 120)) + '\\n\\n'; });\nmd += '## 🪝 Top Hooks\\n';\nhooks.forEach((h, i) => { md += (i+1) + '. _\"' + (h.hook || '').slice(0, 180) + '\"_ — ' + (h.usageCount ?? '?') + 'x · ' + fmt(h.totalImpressions) + ' impressions\\n\\n'; });\nmd += '## 🛬 Top Landing Pages\\n';\nlps.forEach((p, i) => { md += (i+1) + '. ' + (p.landingPage || '(no url)') + ' — ' + fmt(p.totalImpressions) + ' impressions · used ' + (p.usageCount ?? '?') + 'x\\n\\n'; });\nreturn [{ json: { title: 'Brand 360 — ' + ids.brandName + ' — ' + dateStr, markdown: md, brandName: ids.brandName, recipientEmail: ids.recipientEmail } }];"
      },
      "id": "66043569-6ef1-4dac-a29d-89374a4706c8",
      "name": "Build Brand 360 Markdown",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1392, 608],
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "notionDatabaseId",
              "value": "YOUR_BRAND_REPORTS_DB",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "19f0446c-953c-4636-80c0-35daa5512a26",
      "name": "Set Report Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1632, 608]
    },
    {
      "parameters": {
        "resource": "databasePage",
        "databaseId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.notionDatabaseId }}"
        },
        "title": "={{ $json.title }}",
        "propertiesUi": {
          "propertyValues": [
            {
              "key": "Brand|rich_text",
              "type": "rich_text",
              "textContent": "={{ $json.brandName }}"
            },
            {
              "key": "Body|rich_text",
              "type": "rich_text",
              "textContent": "={{ $json.markdown.slice(0, 1900) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "b3c27d49-a9e0-4625-9f3b-7705146b514e",
      "name": "Create Notion Report",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [1872, 608]
    },
    {
      "parameters": {
        "sendTo": "={{ $('Set Report Config').first().json.recipientEmail }}",
        "subject": "={{ $('Set Report Config').first().json.title }}",
        "message": "=<div><h2>{{ $('Set Report Config').first().json.title }}</h2><p>Your Brand 360 report has been generated. Notion page: <a href=\"{{ $json.url }}\">{{ $json.url }}</a></p><pre style=\"background:#f6f8fa;padding:12px;border-radius:6px;white-space:pre-wrap;font-family:ui-monospace,monospace;\">{{ $('Set Report Config').first().json.markdown }}</pre></div>",
        "options": {
          "appendAttribution": false
        }
      },
      "id": "a29af43a-8c70-482d-be56-9e2ef7c46bae",
      "name": "Email Report",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [2112, 608]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ ok: true, brand: $('Set Report Config').first().json.brandName, notionUrl: $('Create Notion Report').first().json.url }) }}",
        "options": {
          "responseCode": 200
        }
      },
      "id": "88dbe30e-2318-4927-a482-11c29f43f49d",
      "name": "Respond OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [2352, 608]
    }
  ],
  "connections": {
    "Brand Report Webhook": {
      "main": [
        [
          {
            "node": "Lookup Brand",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lookup Brand": {
      "main": [
        [
          {
            "node": "Extract IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract IDs": {
      "main": [
        [
          {
            "node": "Fetch Overview",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Top Ads",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Scaling Ads",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Hooks",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Landing Pages",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Media Mix",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Overview": {
      "main": [
        [
          {
            "node": "Wait for All",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for All": {
      "main": [
        [
          {
            "node": "Build Brand 360 Markdown",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Top Ads": {
      "main": [
        [
          {
            "node": "Wait for All",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Fetch Scaling Ads": {
      "main": [
        [
          {
            "node": "Wait for All",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Fetch Hooks": {
      "main": [
        [
          {
            "node": "Wait for All",
            "type": "main",
            "index": 3
          }
        ]
      ]
    },
    "Fetch Landing Pages": {
      "main": [
        [
          {
            "node": "Wait for All",
            "type": "main",
            "index": 4
          }
        ]
      ]
    },
    "Fetch Media Mix": {
      "main": [
        [
          {
            "node": "Wait for All",
            "type": "main",
            "index": 5
          }
        ]
      ]
    },
    "Build Brand 360 Markdown": {
      "main": [
        [
          {
            "node": "Set Report Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Report Config": {
      "main": [
        [
          {
            "node": "Create Notion Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Notion Report": {
      "main": [
        [
          {
            "node": "Email Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Report": {
      "main": [
        [
          {
            "node": "Respond OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": true
  },
  "tags": []
}
