{
  "name": "Daily TokPortal analytics digest \u2192 Slack",
  "nodes": [
    {
      "parameters": {
        "content": "## Daily analytics digest\nEvery morning: pull the TokPortal analytics dashboard + credit balance and send a digest to Slack (swap the Slack node for Gmail/SMTP if you prefer).\n\nSeries: last 7 days of gained views.",
        "height": 200,
        "width": 560,
        "color": 4
      },
      "id": "tp-026",
      "name": "Sticky Notetp-025",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -240
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * *"
            }
          ]
        }
      },
      "id": "tp-027",
      "name": "Every Day 08:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        60
      ]
    },
    {
      "parameters": {
        "resource": "analytics",
        "operation": "getDashboard",
        "filters": {
          "from": "={{ $now.minus({ days: 7 }).toFormat(\"yyyy-LL-dd\") }}",
          "to": "={{ $now.toFormat(\"yyyy-LL-dd\") }}"
        }
      },
      "id": "tp-028",
      "name": "Analytics Dashboard",
      "type": "n8n-nodes-tokportal.tokPortal",
      "typeVersion": 1,
      "position": [
        240,
        -60
      ],
      "credentials": {
        "tokPortalApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "analytics",
        "operation": "getSeries",
        "metric": "views",
        "options": {
          "granularity": "day",
          "mode": "gained",
          "from": "={{ $now.minus({ days: 7 }).toFormat(\"yyyy-LL-dd\") }}",
          "to": "={{ $now.toFormat(\"yyyy-LL-dd\") }}"
        }
      },
      "id": "tp-029",
      "name": "Views Gained (7d)",
      "type": "n8n-nodes-tokportal.tokPortal",
      "typeVersion": 1,
      "position": [
        240,
        100
      ],
      "credentials": {
        "tokPortalApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "credits",
        "operation": "getBalance"
      },
      "id": "tp-030",
      "name": "Credit Balance",
      "type": "n8n-nodes-tokportal.tokPortal",
      "typeVersion": 1,
      "position": [
        240,
        260
      ],
      "credentials": {
        "tokPortalApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "numberInputs": 3
      },
      "id": "tp-031",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        480,
        100
      ]
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all().map(i => i.json);\nconst dash = items[0] || {};\nconst series = items[1] || {};\nconst credits = items[2] || {};\nconst points = Array.isArray(series.points) ? series.points : (Array.isArray(series.series) ? series.series : []);\nconst gained = points.reduce((s, p) => s + Number(p.value ?? p.views ?? 0), 0);\nconst totals = dash.totals || dash.summary || dash;\nconst lines = [\n  ':bar_chart: *TokPortal daily digest* (' + $now.toFormat('yyyy-LL-dd') + ')',\n  '\u2022 Views gained (7d): ' + gained.toLocaleString(),\n  '\u2022 Total views: ' + Number(totals.views ?? totals.total_views ?? 0).toLocaleString(),\n  '\u2022 Followers: ' + Number(totals.followers ?? totals.total_followers ?? 0).toLocaleString(),\n  '\u2022 Accounts tracked: ' + Number(totals.accounts ?? totals.accounts_count ?? 0),\n  '\u2022 Credits left: ' + Number(credits.total_credits ?? credits.balance ?? 0),\n];\nreturn [{ json: { text: lines.join('\\n'), raw: { dash, series, credits } } }];"
      },
      "id": "tp-032",
      "name": "Build Digest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        100
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#social-ops"
        },
        "text": "={{ $json.text }}",
        "otherOptions": {}
      },
      "id": "tp-033",
      "name": "Slack: Digest",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        960,
        100
      ],
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every Day 08:00": {
      "main": [
        [
          {
            "node": "Analytics Dashboard",
            "type": "main",
            "index": 0
          },
          {
            "node": "Views Gained (7d)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Credit Balance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analytics Dashboard": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Views Gained (7d)": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Credit Balance": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Build Digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Digest": {
      "main": [
        [
          {
            "node": "Slack: Digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": [
    {
      "name": "TokPortal"
    }
  ]
}