<%# Shared statistics body for the admin Stats page (views/stats.ejs) and the public page (views/public_stats.ejs). Two views (?v=community | streams) x periods (?p=stream|week|month|year|all). Community: supporters, raids, leaderboards, community ranks, emotes, commands, rewards. Streams: numbers, one activity chart (pick what to show), games played, the streams list; one stream: viewer graph. Locals: s = periodstats.summary(), q = rangeQuery helper, base = link prefix ('/stats', '/public' or '/'), view = 'community' | 'streams', mode = 'admin' | 'public', viewerHref = viewer link prefix or null, extraBoards = [{ title, rows, level, wide }], ranks = [{ login, name, level }] (Regular and up, all time). Colours come from the page: --muted, --line, --accent, --accent-hover, --card (and --card-solid for popups). A new section goes here once and appears on both pages; wrap it in `if (admin)` when it is not for the public. %> <% const admin = locals.mode === 'admin'; const esc = v => String(v ?? '').replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); const num = n => Number(n || 0).toLocaleString('en-US'); const hm = m => m >= 60 ? `${Math.floor(m / 60)} h ${String(Math.round(m % 60)).padStart(2, '0')} m` : `${Math.round(m)} m`; const r = s.range, T = s.totals, C = s.channel || {}, TW = s.twitch; const v = locals.view === 'streams' ? 'streams' : 'community'; const cur = { p: r.p, d: r.from || undefined, s: r.p === 'stream' ? r.stream.id : undefined }; const link = (rq, view = v) => base + q({ ...rq, v: view === 'community' ? undefined : view }); const TZ = locals.tz || 'UTC'; // Settings → General → Time zone const day = iso => new Date(iso).toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', timeZone: TZ }); const time = iso => new Date(iso).toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit', timeZone: TZ }); const emoteSrc = e => e.source === 'twitch' && e.id ? `https://static-cdn.jtvnw.net/emoticons/v2/${e.id}/default/dark/1.0` : e.source === '7tv' && e.id ? `https://cdn.7tv.app/emote/${e.id}/1x.webp` : null; // ⓘ with an instant tooltip (hover, or tap / keyboard focus) const info = text => text ? ` ⓘ` : ''; const person = x => { const n = esc(x.name || x.login || '?'); return viewerHref && x.login ? `${n}` : n; }; const since = TW && TW.streamedDays ? TW.cutoff : null; // period reaches back before HeyStreamer counted %>
Community Streams
<% for (const [p, label] of [['stream', 'Stream'], ['week', 'Week'], ['month', 'Month'], ['year', 'Year'], ['all', 'All time']]) { %> <%= label %> <% } %>
<% if (r.p !== 'all') { %> <% if (r.prev) { %>‹<% } else { %>‹<% } %> <% } %> <%= r.label %><% if (r.sub) { %> · <%= r.sub %><% } %> <% if (r.p !== 'all') { %> <% if (r.next) { %>›<% } else { %>›<% } %> <% } %>
<% // ---- number tiles. Public: a short, friendly set; admin: everything ---- const T_ = {}; // id -> [value, label, tooltip] T_.messages = [num(T.messages), 'chat messages', since ? `Before ${since} from Twitch's daily analytics, after that counted by HeyStreamer` : '']; T_.watch = [num(T.watchHours), 'hours watched', T.historyWatchHours ? `${num(T.historyWatchHours)} h from Twitch's daily analytics (before ${since}), the rest counted by HeyStreamer` : T.baselineHours ? `Includes ${num(T.baselineHours)} h counted by StreamElements before HeyStreamer counted itself` : '']; if (r.p !== 'stream') T_.streams = [num(T.streams), T.streams === 1 ? 'stream' : 'streams', since ? `Before ${since} every day with a stream counts as one stream (Twitch's daily analytics)` : '']; T_.streamed = r.p === 'stream' && s.streams[0] ? [hm(s.streams[0].minutes), s.streams[0].live ? 'live for' : 'streamed'] : [num(T.hoursStreamed), 'hours streamed']; T_.emotes = [num(T.emotes), 'emotes used', since ? `Counted by HeyStreamer since ${since}` : '']; if (TW && TW.clips) T_.clips = [num(TW.clips), 'clips created', `${num(TW.clipViews)} clip views (Twitch's daily analytics)`]; T_.redeems = [num(T.redeems), 'rewards redeemed', `${num(T.channelPoints)} channel points spent${since ? `; counted since ${since}` : ''}`]; if (admin) { if (r.p === 'all' && C.followers != null) T_.followers = [num(C.followers), 'followers', 'Following the channel right now (from Twitch)']; if (r.p === 'all' && C.subsNow != null) T_.subsNow = [num(C.subsNow), 'subscribers', 'Subscribed right now (from Twitch, without the streamer)']; T_.chatters = [num(T.chatters), since ? `chatters since ${since}` : 'chatters', 'Different people who chatted. Twitch\'s export has no names, so this only counts from HeyStreamer\'s start']; T_.viewers = [num(T.viewers), since ? `viewers since ${since}` : 'viewers', `Different people in the chatter list while live, lurkers included${T.viewersFromBaseline ? `. Includes ${num(T.viewersFromBaseline)} viewers known only from StreamElements' watch time (from before HeyStreamer counted), which is why All time is higher than the other tabs` : ''}`]; const pk = r.p === 'stream' ? s.streams[0] : C; if (pk && pk.peak != null) T_.peak = [num(pk.peak), 'peak viewers', pk.avg != null ? `About ${num(pk.avg)} on average` : '']; if (TW && TW.uniquePerDay != null) T_.unique = [num(TW.uniquePerDay), 'unique viewers / stream day', 'Twitch\'s count of different people who watched, averaged over the days you streamed']; if (TW && (TW.newEngaged || TW.returningEngaged)) T_.engaged = [num(TW.newEngaged), 'new engaged viewers', `${num(TW.returningEngaged)} returning engaged viewer-days. Engaged = watched 5+ minutes and chatted or followed (Twitch)`]; if (TW && TW.revenue) T_.revenue = [`$${TW.revenue.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`, 'revenue · admin only', TW.revenueByType.map(([k, x]) => `${k} $${x.toFixed(2)}`).join(', ') + ' (Twitch export, before payout fees and taxes)']; if (T.follows && !T_.followers) T_.follows = [num(T.follows), 'new followers']; if (T.intros) T_.intros = [num(T.intros), 'first-time chatters']; if (T.subs || T.giftSubs) T_.subs = [num(T.subs + T.giftSubs), 'subs', `${num(T.subs)} subs and resubs, ${num(T.giftSubs)} gifted`]; if (T.bits) T_.bits = [num(T.bits), 'bits', s.bitsSource === 'twitch' ? 'All time, from Twitch (top 100 cheerers)' : T.powerUps ? `${num(T.powerUps)} Power-ups included` : '']; if (T.raids) T_.raids = [num(T.raids), T.raids === 1 ? 'raid' : 'raids', `${num(T.raidViewers)} viewers brought in`]; if (T.checkins) T_.checkins = [num(T.checkins), 'check-ins', 'First message of a stream']; } const order = admin ? ['followers', 'subsNow', 'messages', 'chatters', 'viewers', 'watch', 'streams', 'streamed', 'peak', 'unique', 'engaged', 'clips', 'revenue', 'follows', 'intros', 'emotes', 'subs', 'bits', 'redeems', 'raids', 'checkins'] : ['messages', 'watch', 'streams', 'streamed', 'emotes', 'clips', 'redeems']; const tiles = order.filter(k => T_[k]).map(k => T_[k]); %> <% if (admin || v === 'streams') { %>
<% for (const [val, k, tip] of tiles) { %>
<%= val %>
<%= k %><%- info(tip) %>
<% } %>
<% } %> <% if (v === 'streams') { %> <%# ===================== STREAMS ===================== %> <% if (r.p === 'stream' && s.streams[0]) { const x = s.streams[0]; %>

<%= day(x.started_at) %>, <%= time(x.started_at) %>–<%= x.ended_at ? time(x.ended_at) : 'now' %><%= x.games.length ? ` · ${x.games.join(', ')}` : '' %> <% if (x.live) { %> · live now<% } %> <% if (x.source === 'vod') { %> · from the VOD (HeyStreamer was not counting)<% } %>

<% // viewers every 5 minutes as an area line; title / category changes as dashed markers const tl = s.timeline; if (tl && (tl.samples.length || tl.changes.length)) { const W = 900, H = 200, L = 34, B = 20, TOP = 10; const t0 = Date.parse(tl.start), t1 = Math.max(Date.parse(tl.end), t0 + 60000); const max = Math.max(1, ...tl.samples.map(p => p.viewers)); const top = (() => { const p = Math.pow(10, Math.floor(Math.log10(max))); return [1, 2, 2.5, 5, 10].map(m => m * p).find(x => x >= max); })(); const X = t => L + (W - L) * (Date.parse(t) - t0) / (t1 - t0), Y = n => TOP + (H - TOP - B) * (1 - n / top); let svg = ``; for (const g of [0, top / 2, top]) svg += `${g}`; for (let h = 0; h <= (t1 - t0) / 36e5; h++) { const tt = new Date(t0 + h * 36e5).toISOString(); svg += `${time(tt)}`; } if (tl.samples.length) { const pts = tl.samples.map(p => `${X(p.at).toFixed(1)},${Y(p.viewers).toFixed(1)}`); svg += ``; svg += ``; for (const p of tl.samples) svg += ``; } for (const c of tl.changes) { if (c.start) continue; const x = X(c.at).toFixed(1); svg += ``; } %>

Viewers

<%- svg %>
<% if (!tl.samples.length) { %>

No viewer counts were recorded for this stream.

<% } %> <% if (tl.changes.length) { %>
    <% for (const c of tl.changes) { %>
  1. <%= c.start ? 'start' : time(c.at) %> <%= c.title || '—' %><% if (c.game) { %> <%= c.game %><% } %>
  2. <% } %>
<% } %> <% } %> <% } %> <% // one activity chart, like Twitch's: pick what it shows const METRICS = [['messages', 'Chat messages', ''], ['watchHours', 'Hours watched', 'h'], ['streamHours', 'Hours streamed', 'h'], ['avgViewers', 'Average viewers', ''], ['follows', 'New followers', '']]; const rows = s.series ? s.series.rows : []; const shown = METRICS.filter(([k]) => rows.some(x => x[k])); %> <% if (rows.length && shown.length) { %>

Activity per <%= s.series.unit %>

<% shown.forEach(([k, label], i) => { %><% }) %>
<% } %> <% // games played: donut of streamed time per category (from HeyStreamer's title / category history) const PAL = ['#b74347', '#5b8fc7', '#d39b3a', '#4f9a68', '#8c6bb1', '#c76f9a', '#3fa3a1', '#9a8a5a']; const games = s.games || []; const totalMin = games.reduce((a, g) => a + g.minutes, 0); const slices = games.length > 8 ? [...games.slice(0, 7), { game: `${games.length - 7} more`, minutes: games.slice(7).reduce((a, g) => a + g.minutes, 0), other: true }] : games; %> <% if (r.p !== 'stream' || slices.length > 1) { %>

Games<%- info('Time per category, from every title / category change HeyStreamer saw. Twitch keeps no history of categories, so streams from before HeyStreamer recorded them have no game.') %>

<% if (!totalMin) { %>

no categories recorded in this period yet

<% } else { %>
<% let acc = 0; slices.forEach((g, i) => { const a0 = acc / totalMin * 2 * Math.PI, a1 = (acc + g.minutes) / totalMin * 2 * Math.PI; acc += g.minutes; const big = a1 - a0 > Math.PI ? 1 : 0, P = a => `${(Math.sin(a) * .95).toFixed(4)} ${(-Math.cos(a) * .95).toFixed(4)}`, Q = a => `${(Math.sin(a) * .55).toFixed(4)} ${(-Math.cos(a) * .55).toFixed(4)}`; const d = slices.length === 1 ? 'M0 -.95 A.95 .95 0 1 1 0 .95 A.95 .95 0 1 1 0 -.95 Z M0 -.55 A.55 .55 0 1 0 0 .55 A.55 .55 0 1 0 0 -.55 Z' : `M${P(a0)} A.95 .95 0 ${big} 1 ${P(a1)} L${Q(a1)} A.55 .55 0 ${big} 0 ${Q(a0)} Z`; %> <%= g.game %> · <%= hm(g.minutes) %> <% }) %>
<% } %> <% } %> <% if (r.p !== 'stream' && s.streams.length) { %>

<%= r.p === 'all' ? 'Recent streams' : 'Streams' %>

<% for (const x of s.streams.slice(0, r.p === 'all' ? 15 : 100)) { %> <% } %>
DateTitle · gamesLengthPeakViewersChattersMessages
<%= day(x.started_at) %><%= x.live ? ' · live' : '' %><% if (x.source === 'vod') { %> VOD<% } %> <%= x.title || '—' %><% if (x.games.length) { %><%= x.games.join(' · ') %><% } %> <%= hm(x.minutes) %><%= x.peak != null ? num(x.peak) : '—' %> <%= num(x.viewers) %><%= num(x.chatters) %><%= num(x.messages) %>
<% } %> <% } else { %> <%# ===================== COMMUNITY ===================== %> <% const cm = s.community || {}; %> <% if ((cm.supporters || []).length) { %>

Thank you for the support

<% for (const x of cm.supporters.slice(0, 40)) { const what = [x.bits ? `${num(x.bits)} bits` : '', x.subs ? (x.subs === 1 ? 'sub' : `${x.subs} subs`) : '', x.gifts ? `${num(x.gifts)} gifted` : '', admin && x.tips ? 'tip' : ''].filter(Boolean).join(' · '); %> <%- person(x) %> <%= what %> <% } %>
<% } %> <% if ((cm.raiders || []).length) { %>

Raids

<% for (const x of cm.raiders) { %><%- person(x) %> <%= x.raids > 1 ? `${x.raids} raids · ` : '' %><%= num(x.viewers) %> viewers<% } %>
<% } %> <% const board = b => { const nw = b.nw ? ` style="--nw:${b.nw}"` : ''; let out = `

${esc(b.title)}

`; if (!b.rows.length) return out + '

nothing yet

'; out += '
    '; b.rows.forEach((x, i) => { out += `
  1. ${i + 1}${person(x)}${b.level ? `${esc(b.level(x))}` : ''}${(b.fmt || num)(x.n)}
  2. `; }); return out + '
'; }; const boards = [ ...(locals.extraBoards || []), { title: 'Most active in chat', rows: s.boards.chat }, { title: 'Watch time', rows: s.boards.watch, fmt: hm, nw: '6.4rem' }, { title: 'Channel points spent', rows: s.boards.channelPoints, optional: true }, ...(admin ? [{ title: 'Bits', rows: s.boards.bits, optional: true }, { title: 'Subs and gifts', rows: s.boards.subs, optional: true }] : []), ].filter(b => !(b.optional && !b.rows.length)); %>

Leaderboards

<% for (const b of boards) { %><%- board(b) %><% } %>
<% if ((locals.ranks || []).length) { const LV = ['Veteran', 'Loyal', 'Regular']; %>

Community ranks<%- info('Levels by total watch time: Regular from 50 h, Loyal from 200 h, Veteran from 1000 h. All time.') %>

<% for (const lv of LV) { const list = ranks.filter(x => x.level === lv); if (!list.length) continue; %>

<%= lv %> <%= list.length %>

<% for (const x of list) { %><%- person(x) %><% } %>
<% } %>
<% } %> <% if (s.channelEmotes && s.channelEmotes.length) { %>

Channel emotes<%- info('The channel\'s own emotes and how often they were used in this chat in this period. Twitch does not tell how often they are used in other channels.') %>

<% for (const e of s.channelEmotes) { %> <% if (e.image) { %><% } %><%= e.name %> <%= num(e.uses) %> <% } %>
<% } %>

Top emotes

<% if (!s.emotes.length) { %>

nothing yet

<% } else { %>
<% for (const e of s.emotes) { const src = emoteSrc(e); %> <% if (src) { %><% } %><%= e.emote %> <%= num(e.uses) %> <% } %>
<% } %> <% if (s.commands.length) { %>

Top commands

<% for (const c of s.commands) { %><%= c.command %> <%= num(c.uses) %><% } %>
<% } %> <% if (s.rewards.length) { %>

Rewards and Power-ups

<% for (const x of s.rewards) { %><%= x.item %> <%= num(x.uses) %><% } %>
<% } %> <% } %>