{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Plausible CE Analytics Digest",
  "tags": [],
  "nodes": [
    {
      "id": "01ad8e60-ec99-46c1-91f0-beba440eb780",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -368,
        32
      ],
      "parameters": {
        "width": 480,
        "height": 672,
        "content": "## Plausible CE Analytics Digest\n\n### How it works\n\n1. Triggers the workflow every Monday and alternately with a manual trigger.\n2. Sets necessary variables like site_id and plausible_url.\n3. Fetches top pages, referrers, and traffic data from different time periods.\n4. Processes the data to find insights like new referrers and content decay.\n5. Combines these insights and formats a message.\n6. Sends the formatted digest message via Telegram.\n\n### Setup steps\n\n- [ ] Schedule the workflow for every Monday at 8:00 AM.\n- [ ] Configure the 'Set Variables' node with correct site_id, plausible_url, and telegram_chat_id.\n- [ ] Ensure API credentials or tokens for HTTP requests are set up.\n- [ ] Set up Telegram credentials in the 'Send Digest via Telegram' node.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "f072065e-e67f-4994-9c9e-221086a249d4",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        192,
        176
      ],
      "parameters": {
        "color": 7,
        "width": 240,
        "height": 448,
        "content": "## Trigger workflow\n\nInitializes the workflow every Monday or manually."
      },
      "typeVersion": 1
    },
    {
      "id": "d9cb56d7-5dae-4afa-962c-4cfd54ddd147",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        464,
        176
      ],
      "parameters": {
        "color": 7,
        "width": 240,
        "height": 304,
        "content": "## Set variables\n\nSets critical variables for workflow operations."
      },
      "typeVersion": 1
    },
    {
      "id": "33619eaa-a111-4c61-8787-4e9dc5622aed",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        736,
        32
      ],
      "parameters": {
        "color": 7,
        "width": 624,
        "height": 592,
        "content": "## Fetch and extract data\n\nFetches webpage metrics and extracts top pages, referrers, and content decay insights."
      },
      "typeVersion": 1
    },
    {
      "id": "8e65e322-2e11-4572-bf6b-060d00751898",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1392,
        144
      ],
      "parameters": {
        "color": 7,
        "width": 240,
        "height": 352,
        "content": "## Merge insights\n\nCombines insights from all fetched data into a cohesive dataset."
      },
      "typeVersion": 1
    },
    {
      "id": "c3565340-b4bc-43f8-be3f-65d8fe00e016",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1664,
        176
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 304,
        "content": "## Format and send digest\n\nFormats the merged insights into a message and sends it via Telegram."
      },
      "typeVersion": 1
    },
    {
      "id": "752c7f55-ecea-4412-8d0f-e367b504be02",
      "name": "When Monday 8:00 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        240,
        304
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 8
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "b5b9c97d-bab9-4821-b1ba-20db3dced3a6",
      "name": "Set Workflow Variables",
      "type": "n8n-nodes-base.set",
      "position": [
        512,
        304
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "site-id",
              "name": "site_id",
              "type": "string",
              "value": "your-site.com"
            },
            {
              "id": "plausible-url",
              "name": "plausible_url",
              "type": "string",
              "value": "https://plausible.your-domain.com"
            },
            {
              "id": "telegram-chat-id",
              "name": "telegram_chat_id",
              "type": "string",
              "value": "YOUR_TELEGRAM_CHAT_ID"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "ac17f13e-b608-4421-ab89-97170760518f",
      "name": "Post Top Pages Query",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        784,
        160
      ],
      "parameters": {
        "url": "={{ $('Set Workflow Variables').first().json.plausible_url + '/api/v2/query' }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ ({ site_id: $('Set Workflow Variables').first().json.site_id, metrics: ['visitors'], date_range: '7d', dimensions: ['event:page'], order_by: [['visitors', 'desc']], pagination: { limit: 10 } }) }}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpBearerAuth"
      },
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.4
    },
    {
      "id": "5b0fc1fe-e50c-4746-aaf7-b4fd3dda3f1b",
      "name": "Prepare Top Pages Data",
      "type": "n8n-nodes-base.set",
      "position": [
        1008,
        160
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "top-pages",
              "name": "topPages",
              "type": "array",
              "value": "={{ $json.results.slice(0, 5).map(r => ({ page: r.dimensions[0], visitors: r.metrics[0] })) }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "2dc22041-e4a6-4523-b2d8-c99edb3f2287",
      "name": "Post This Week Referrers Query",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        784,
        304
      ],
      "parameters": {
        "url": "={{ $('Set Workflow Variables').first().json.plausible_url + '/api/v2/query' }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ ({ site_id: $('Set Workflow Variables').first().json.site_id, metrics: ['visitors'], date_range: '7d', dimensions: ['visit:referrer'] }) }}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth"
      },
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.4
    },
    {
      "id": "a63a3006-758e-4c3e-9353-6c8213ed4c7a",
      "name": "Post Last Week Referrers Query",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1008,
        304
      ],
      "parameters": {
        "url": "={{ $('Set Workflow Variables').first().json.plausible_url + '/api/v2/query' }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ ({ site_id: $('Set Workflow Variables').first().json.site_id, metrics: ['visitors'], date_range: [$now.minus({days: 13}).toFormat('yyyy-MM-dd'), $now.minus({days: 7}).toFormat('yyyy-MM-dd')], dimensions: ['visit:referrer'] }) }}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth"
      },
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      },
      "executeOnce": true,
      "typeVersion": 4.4
    },
    {
      "id": "88879f50-39af-4ac8-9d7d-ab3926e26ae1",
      "name": "Identify New Referrers",
      "type": "n8n-nodes-base.code",
      "position": [
        1216,
        304
      ],
      "parameters": {
        "jsCode": "const thisWeekResults = $('Post This Week Referrers Query').first().json.results || [];\nconst lastWeekResults = $input.first().json.results || [];\n\nconst lastWeekDomains = new Set(\n  lastWeekResults.map(r => r.dimensions[0]).filter(Boolean)\n);\n\nconst newReferrers = thisWeekResults\n  .filter(r => {\n    const ref = r.dimensions[0];\n    return ref && ref !== '(none)' && ref !== '' && !lastWeekDomains.has(ref);\n  })\n  .sort((a, b) => b.metrics[0] - a.metrics[0])\n  .slice(0, 5)\n  .map(r => ({ referrer: r.dimensions[0], visitors: r.metrics[0] }));\n\nreturn [{ json: { newReferrers } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "76af3569-2422-4275-9356-677fc229d085",
      "name": "Post Current Page Traffic Query",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        784,
        448
      ],
      "parameters": {
        "url": "={{ $('Set Workflow Variables').first().json.plausible_url + '/api/v2/query' }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ ({ site_id: $('Set Workflow Variables').first().json.site_id, metrics: ['visitors'], date_range: '7d', dimensions: ['event:page'] }) }}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth"
      },
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.4
    },
    {
      "id": "030ae9ce-0101-47af-bade-9bdcdd0a87e2",
      "name": "Post Historical Traffic Query",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1008,
        448
      ],
      "parameters": {
        "url": "={{ $('Set Workflow Variables').first().json.plausible_url + '/api/v2/query' }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ ({ site_id: $('Set Workflow Variables').first().json.site_id, metrics: ['visitors'], date_range: [$now.minus({days: 34}).toFormat('yyyy-MM-dd'), $now.minus({days: 28}).toFormat('yyyy-MM-dd')], dimensions: ['event:page'] }) }}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth"
      },
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      },
      "executeOnce": true,
      "typeVersion": 4.4
    },
    {
      "id": "a36720db-e76b-456b-b114-4e4458c2c645",
      "name": "Analyze Content Decay",
      "type": "n8n-nodes-base.code",
      "position": [
        1216,
        448
      ],
      "parameters": {
        "jsCode": "const currentResults = $('Post Current Page Traffic Query').first().json.results || [];\nconst historicalResults = $input.first().json.results || [];\n\nconst historicalMap = {};\nhistoricalResults.forEach(r => {\n  historicalMap[r.dimensions[0]] = r.metrics[0];\n});\n\nconst decayingPages = currentResults\n  .filter(r => {\n    const page = r.dimensions[0];\n    const current = r.metrics[0];\n    const historical = historicalMap[page] || 0;\n    if (historical < 10) return false;\n    return (historical - current) / historical > 0.3;\n  })\n  .map(r => {\n    const page = r.dimensions[0];\n    const current = r.metrics[0];\n    const historical = historicalMap[page] || 0;\n    const dropPct = Math.round(((historical - current) / historical) * 100);\n    return { page, current, historical, dropPct };\n  })\n  .sort((a, b) => b.dropPct - a.dropPct)\n  .slice(0, 5);\n\nreturn [{ json: { decayingPages } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "9d704281-db18-4db9-bcb3-9e73cb71dfee",
      "name": "Combine All Insights",
      "type": "n8n-nodes-base.merge",
      "position": [
        1440,
        304
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition",
        "numberInputs": 3
      },
      "typeVersion": 3.2
    },
    {
      "id": "2276e15c-87de-43a0-93f5-8b99aafb6584",
      "name": "Create Digest Message",
      "type": "n8n-nodes-base.code",
      "position": [
        1712,
        304
      ],
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst siteId = $('Set Workflow Variables').first().json.site_id;\nconst topPagesText = data.topPages && data.topPages.length > 0\n  ? data.topPages.map((p, i) => (i + 1) + '. ' + p.page + ' - ' + p.visitors + ' visitors').join('\\n')\n  : 'No data available';\nconst newReferrersText = data.newReferrers && data.newReferrers.length > 0\n  ? data.newReferrers.map(r => '- ' + r.referrer + ' - ' + r.visitors + ' visitors').join('\\n')\n  : 'No new referrers this week';\nconst decayText = data.decayingPages && data.decayingPages.length > 0\n  ? data.decayingPages.map(p => '- ' + p.page + ' down ' + p.dropPct + '% (' + p.historical + ' to ' + p.current + ' visitors)').join('\\n')\n  : 'No significant traffic drops this week';\nconst message = '\ud83d\udcca *Weekly Analytics Digest \u2014 ' + siteId + '*\\n\\n' +\n  '\ud83d\udd25 *Top Pages This Week*\\n' + topPagesText + '\\n\\n' +\n  '\ud83d\udd17 *New Referrers (first appeared this week)*\\n' + newReferrersText + '\\n\\n' +\n  '\ud83d\udcc9 *Content Losing Traction (vs 4 weeks ago)*\\n' + decayText;\nreturn [{ json: { message } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "37ff0033-9ef4-46b2-ae90-038596fc06da",
      "name": "Manual Workflow Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        240,
        448
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "f4df1de2-3a65-48d0-9648-a77c16d82355",
      "name": "Send Digest to Telegram Chat",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1936,
        304
      ],
      "parameters": {
        "text": "={{ $json.message }}",
        "chatId": "={{ $('Set Workflow Variables').first().json.telegram_chat_id }}",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": true,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": true,
    "executionOrder": "v1"
  },
  "connections": {
    "When Monday 8:00 AM": {
      "main": [
        [
          {
            "node": "Set Workflow Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine All Insights": {
      "main": [
        [
          {
            "node": "Create Digest Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post Top Pages Query": {
      "main": [
        [
          {
            "node": "Prepare Top Pages Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze Content Decay": {
      "main": [
        [
          {
            "node": "Combine All Insights",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Create Digest Message": {
      "main": [
        [
          {
            "node": "Send Digest to Telegram Chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Identify New Referrers": {
      "main": [
        [
          {
            "node": "Combine All Insights",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Prepare Top Pages Data": {
      "main": [
        [
          {
            "node": "Combine All Insights",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Workflow Variables": {
      "main": [
        [
          {
            "node": "Post Top Pages Query",
            "type": "main",
            "index": 0
          },
          {
            "node": "Post This Week Referrers Query",
            "type": "main",
            "index": 0
          },
          {
            "node": "Post Current Page Traffic Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Workflow Trigger": {
      "main": [
        [
          {
            "node": "Set Workflow Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post Historical Traffic Query": {
      "main": [
        [
          {
            "node": "Analyze Content Decay",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post Last Week Referrers Query": {
      "main": [
        [
          {
            "node": "Identify New Referrers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post This Week Referrers Query": {
      "main": [
        [
          {
            "node": "Post Last Week Referrers Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post Current Page Traffic Query": {
      "main": [
        [
          {
            "node": "Post Historical Traffic Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}