{
  "name": "Data Analytics Pipeline Automation",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hour",
              "value": 9
            }
          ]
        }
      },
      "id": "daily-trigger",
      "name": "Daily Trigger (9 AM)",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "resource": "report",
        "operation": "get",
        "additionalFields": {
          "dateRange": "yesterday",
          "metrics": "impressions,clicks,conversions,cost"
        }
      },
      "id": "fetch-google-ads",
      "name": "Fetch Google Ads Data",
      "type": "n8n-nodes-base.googleAds",
      "typeVersion": 1,
      "position": [460, 200],
      "credentials": {
        "googleAdsOAuth2Api": {
          "id": "google-ads-credentials",
          "name": "Google Ads OAuth2"
        }
      }
    },
    {
      "parameters": {
        "resource": "report",
        "operation": "get",
        "additionalFields": {
          "dateRange": "yesterday",
          "metrics": "sessions,users,pageviews,bounceRate"
        }
      },
      "id": "fetch-google-analytics",
      "name": "Fetch Google Analytics Data",
      "type": "n8n-nodes-base.googleAnalytics",
      "typeVersion": 1,
      "position": [460, 400],
      "credentials": {
        "googleAnalyticsOAuth2Api": {
          "id": "google-analytics-credentials",
          "name": "Google Analytics OAuth2"
        }
      }
    },
    {
      "parameters": {
        "resource": "report",
        "operation": "get",
        "additionalFields": {
          "dateRange": "yesterday",
          "metrics": "reach,impressions,engagement"
        }
      },
      "id": "fetch-facebook-ads",
      "name": "Fetch Facebook Ads Data",
      "type": "n8n-nodes-base.facebook",
      "typeVersion": 1,
      "position": [460, 600],
      "credentials": {
        "facebookApi": {
          "id": "facebook-credentials",
          "name": "Facebook API"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Combine all data sources\nconst combinedData = {\n  date: new Date().toISOString().split('T')[0],\n  googleAds: $input.all()[0].json,\n  googleAnalytics: $input.all()[1].json,\n  facebookAds: $input.all()[2].json\n};\n\n// Calculate KPIs\nconst kpis = {\n  totalImpressions: (combinedData.googleAds?.impressions || 0) + (combinedData.facebookAds?.impressions || 0),\n  totalClicks: (combinedData.googleAds?.clicks || 0),\n  totalConversions: (combinedData.googleAds?.conversions || 0),\n  totalCost: combinedData.googleAds?.cost || 0,\n  websiteSessions: combinedData.googleAnalytics?.sessions || 0,\n  ctr: combinedData.googleAds?.clicks / combinedData.googleAds?.impressions || 0,\n  cpa: combinedData.googleAds?.cost / combinedData.googleAds?.conversions || 0\n};\n\nreturn {\n  combinedData,\n  kpis,\n  timestamp: new Date().toISOString()\n};"
      },
      "id": "process-data",
      "name": "Process & Combine Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "resource": "spreadsheet",
        "operation": "append",
        "documentId": "analytics-dashboard",
        "sheetName": "Daily Data",
        "additionalFields": {
          "values": "={{ [\n  $json.date,\n  $json.kpis.totalImpressions,\n  $json.kpis.totalClicks,\n  $json.kpis.totalConversions,\n  $json.kpis.totalCost,\n  $json.kpis.websiteSessions,\n  $json.kpis.ctr,\n  $json.kpis.cpa\n] }}"
        }
      },
      "id": "save-to-google-sheets",
      "name": "Save to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 2,
      "position": [900, 200],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "google-sheets-credentials",
          "name": "Google Sheets OAuth2"
        }
      }
    },
    {
      "parameters": {
        "resource": "database",
        "operation": "insert",
        "table": "daily_analytics",
        "columns": "date,impressions,clicks,conversions,cost,sessions,ctr,cpa",
        "values": "={{ [\n  $json.date,\n  $json.kpis.totalImpressions,\n  $json.kpis.totalClicks,\n  $json.kpis.totalConversions,\n  $json.kpis.totalCost,\n  $json.kpis.websiteSessions,\n  $json.kpis.ctr,\n  $json.kpis.cpa\n] }}"
      },
      "id": "save-to-database",
      "name": "Save to Database",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [900, 400],
      "credentials": {
        "postgres": {
          "id": "postgres-credentials",
          "name": "PostgreSQL"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.kpis.ctr }}",
              "value2": 0.02,
              "operation": "smaller"
            }
          ]
        }
      },
      "id": "check-performance",
      "name": "Check Performance Thresholds",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [1120, 300]
    },
    {
      "parameters": {
        "resource": "email",
        "operation": "send",
        "additionalFields": {
          "to": "marketing-team@company.com",
          "subject": "Performance Alert - Low CTR Detected",
          "html": "<h2>Performance Alert</h2><p>CTR is below 2% threshold.</p><p>Current CTR: {{ $json.kpis.ctr }}%</p><p>Date: {{ $json.date }}</p>",
          "from": "alerts@company.com"
        }
      },
      "id": "send-alert-email",
      "name": "Send Alert Email",
      "type": "n8n-nodes-base.sendGrid",
      "typeVersion": 1,
      "position": [1340, 200],
      "credentials": {
        "sendGridApi": {
          "id": "sendgrid-credentials",
          "name": "SendGrid API"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "send",
        "channel": "analytics",
        "additionalFields": {
          "text": "Daily Analytics Report Generated\nDate: {{ $json.date }}\nImpressions: {{ $json.kpis.totalImpressions }}\nClicks: {{ $json.kpis.totalClicks }}\nCTR: {{ $json.kpis.ctr }}%\nCost: ${{ $json.kpis.totalCost }}"
        }
      },
      "id": "notify-slack",
      "name": "Notify Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [1340, 400],
      "credentials": {
        "slackApi": {
          "id": "slack-credentials",
          "name": "Slack API"
        }
      }
    },
    {
      "parameters": {
        "resource": "dashboard",
        "operation": "update",
        "dashboardId": "marketing-dashboard",
        "additionalFields": {
          "data": "={{ $json.kpis }}"
        }
      },
      "id": "update-dashboard",
      "name": "Update Dashboard",
      "type": "n8n-nodes-base.grafana",
      "typeVersion": 1,
      "position": [1340, 600],
      "credentials": {
        "grafanaApi": {
          "id": "grafana-credentials",
          "name": "Grafana API"
        }
      }
    }
  ],
  "connections": {
    "Daily Trigger (9 AM)": {
      "main": [
        [
          {
            "node": "Fetch Google Ads Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Google Analytics Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Facebook Ads Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Google Ads Data": {
      "main": [
        [
          {
            "node": "Process & Combine Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Google Analytics Data": {
      "main": [
        [
          {
            "node": "Process & Combine Data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Fetch Facebook Ads Data": {
      "main": [
        [
          {
            "node": "Process & Combine Data",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Process & Combine Data": {
      "main": [
        [
          {
            "node": "Save to Google Sheets",
            "type": "main",
            "index": 0
          },
          {
            "node": "Save to Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save to Google Sheets": {
      "main": [
        [
          {
            "node": "Check Performance Thresholds",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save to Database": {
      "main": [
        [
          {
            "node": "Check Performance Thresholds",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Performance Thresholds": {
      "main": [
        [
          {
            "node": "Send Alert Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Alert Email": {
      "main": [
        [
          {
            "node": "Update Dashboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Notify Slack": {
      "main": [
        [
          {
            "node": "Update Dashboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "createdAt": "2025-01-01T00:00:00.000Z",
      "updatedAt": "2025-01-01T00:00:00.000Z",
      "id": "data-analytics",
      "name": "Data Analytics"
    }
  ],
  "triggerCount": 1,
  "updatedAt": "2025-01-01T00:00:00.000Z",
  "versionId": "1.0.0"
}

