{
  "name": "App: Google Ads Sync",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "google-ads-sync",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "firebase",
        "firestoreCollection": "apps/2h_web_solutions_google_ads_asssitant_v1/clients",
        "documentId": "={{ $json.body.clientId }}",
        "options": {}
      },
      "name": "Get Client ID",
      "type": "n8n-nodes-base.googleFirebaseFirestore",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "notes": "Holt die Google Ads Customer ID aus Firestore"
    },
    {
      "parameters": {
        "operation": "search",
        "resource": "campaign",
        "query": "SELECT segments.date, campaign.name, campaign.id, metrics.clicks, metrics.cost_micros, metrics.conversions, metrics.ctr, metrics.average_cpc FROM campaign WHERE segments.date DURING LAST_30_DAYS",
        "limit": 1000
      },
      "name": "Fetch Campaigns",
      "type": "n8n-nodes-base.googleAds",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "credentials": {
        "googleAdsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "dailyStats",
        "fieldsToAggregate": {
          "field": [
            {
              "fromField": "metrics.clicks",
              "toField": "clicks"
            },
            {
              "fromField": "metrics.cost_micros",
              "toField": "cost"
            },
            {
              "fromField": "metrics.conversions",
              "toField": "conversions"
            },
            {
              "fromField": "segments.date",
              "toField": "date"
            }
          ]
        },
        "groupBy": {
          "field": [
            {
              "fromField": "campaign.id"
            }
          ]
        }
      },
      "name": "Aggregate Daily Stats",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "firebase",
        "operation": "update",
        "firestoreCollection": "apps/2h_web_solutions_google_ads_asssitant_v1/clients/{{ $('Webhook').item.json.body.clientId }}/campaigns",
        "documentId": "={{ $json.campaign.id }}",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "dailyStats": "={{ JSON.stringify($json.dailyStats) }}",
            "lastSync": "={{ new Date().toISOString() }}",
            "status": "synced"
          }
        }
      },
      "name": "Save to Firestore",
      "type": "n8n-nodes-base.googleFirebaseFirestore",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n \"status\": \"success\",\n \"count\": {{ $items.length }}\n}",
        "options": {}
      },
      "name": "Done",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Get Client ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Client ID": {
      "main": [
        [
          {
            "node": "Fetch Campaigns",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Campaigns": {
      "main": [
        [
          {
            "node": "Aggregate Daily Stats",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Daily Stats": {
      "main": [
        [
          {
            "node": "Save to Firestore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save to Firestore": {
      "main": [
        [
          {
            "node": "Done",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}