{
  "name": "PolkuAI - Meta Ads -> Google Sheets viikkoraportti (starter)",
  "meta": {
    "polkuai_note": "Ilmainen starter-template markkinointitoimistoille. Hakee Meta Ads -kampanjadatan ja kirjaa sen Google Sheetsiin ajastetusti. Vaatii omat tunnukset (Meta Graph API + Google Sheets). Tama on tee-se-itse-versio: koonti automatisoituu, mutta kommentaari ja hyvaksynta ovat viela kasin. Kun haluat myos ne automaattisiksi (viimeinen kilometri), varaa kartoitus: https://polkuai.com/ota-yhteytta"
  },
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            { "field": "weeks", "weekday": 1, "triggerAtHour": 7 }
          ]
        }
      },
      "id": "trigger-weekly",
      "name": "Joka maanantai klo 7",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "a1", "name": "ad_account_id", "value": "act_TÄHÄN_TILISI_ID", "type": "string" },
            { "id": "a2", "name": "date_preset", "value": "last_7d", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "set-params",
      "name": "Asetukset (muokkaa)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [460, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://graph.facebook.com/v19.0/{{ $json.ad_account_id }}/insights",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            { "name": "level", "value": "campaign" },
            { "name": "fields", "value": "campaign_name,spend,impressions,clicks,ctr,cpc,actions" },
            { "name": "date_preset", "value": "={{ $json.date_preset }}" }
          ]
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "id": "meta-insights",
      "name": "Meta Ads insights",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [680, 300],
      "credentials": {
        "httpHeaderAuth": { "id": "LISAA_META_TOKEN", "name": "Meta Graph API (Bearer)" }
      },
      "notes": "Authorization: Bearer <long-lived-token>. Luo tunnus n8n:n Credentials-osiossa (httpHeaderAuth), nimi 'Meta Graph API (Bearer)'."
    },
    {
      "parameters": {
        "fieldToSplitOut": "data",
        "options": {}
      },
      "id": "split-campaigns",
      "name": "Erota kampanjat",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [900, 300]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "b1", "name": "Kampanja", "value": "={{ $json.campaign_name }}", "type": "string" },
            { "id": "b2", "name": "Kulu (EUR)", "value": "={{ $json.spend }}", "type": "string" },
            { "id": "b3", "name": "Nayttokerrat", "value": "={{ $json.impressions }}", "type": "string" },
            { "id": "b4", "name": "Klikit", "value": "={{ $json.clicks }}", "type": "string" },
            { "id": "b5", "name": "CTR (%)", "value": "={{ $json.ctr }}", "type": "string" },
            { "id": "b6", "name": "CPC (EUR)", "value": "={{ $json.cpc }}", "type": "string" },
            { "id": "b7", "name": "Viikko", "value": "={{ $now.format('yyyy-\\'W\\'WW') }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "map-row",
      "name": "Muotoile rivi",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1120, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": { "__rl": true, "mode": "id", "value": "LISAA_SHEETIN_ID" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Raportti" },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "sheets-append",
      "name": "Kirjaa Google Sheetsiin",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [1340, 300],
      "credentials": {
        "googleSheetsOAuth2Api": { "id": "LISAA_SHEETS_OAUTH", "name": "Google Sheets" }
      },
      "notes": "Luo Google Sheets OAuth2 -tunnus n8n:n Credentials-osiossa. documentId = Sheetin URL:n id-osa."
    }
  ],
  "connections": {
    "Joka maanantai klo 7": { "main": [[{ "node": "Asetukset (muokkaa)", "type": "main", "index": 0 }]] },
    "Asetukset (muokkaa)": { "main": [[{ "node": "Meta Ads insights", "type": "main", "index": 0 }]] },
    "Meta Ads insights": { "main": [[{ "node": "Erota kampanjat", "type": "main", "index": 0 }]] },
    "Erota kampanjat": { "main": [[{ "node": "Muotoile rivi", "type": "main", "index": 0 }]] },
    "Muotoile rivi": { "main": [[{ "node": "Kirjaa Google Sheetsiin", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {}
}
