Skip to main content

Overview

The trend endpoint returns minute-by-minute visitor counts for the last 30 minutes. This is perfect for displaying realtime traffic trends in a chart.

Endpoint

GET https://api.tinybird.co/v0/pipes/trend.json

Parameters

tenant_id
string
Filter by tenant ID
domain
string
Filter by domain

SQL Query

The endpoint uses a multi-node pipeline:

Timeseries Generation

with (now() - interval 30 minute) as start
select addMinutes(toStartOfMinute(start), number) as t
from (select arrayJoin(range(1, 31)) as number)

Visits by Minute

select toStartOfMinute(timestamp) as t, uniq(session_id) as visits
from analytics_hits
where timestamp >= (now() - interval 30 minute)
    {% if defined(tenant_id) %}
    AND tenant_id = {{ String(tenant_id, description="Filter by tenant ID") }}
    {% end %}
    {% if defined(domain) %}
    AND domain = {{ String(domain, description="Filter by domain") }}
    {% end %}
group by toStartOfMinute(timestamp)
order by toStartOfMinute(timestamp)

Join Timeseries

select a.t, b.visits from timeseries a left join hits b on a.t = b.t order by a.t

Response

t
datetime
required
The minute timestamp for this data point
visits
uint64
Number of unique visits in this minute (null if no visits)

TypeScript Usage

import { createAnalyticsClient } from '@tinybirdco/analytics-client';

const tinybird = createAnalyticsClient();

// Get trend for all domains
const result = await tinybird.query.trend();
result.data.forEach(point => {
  console.log(`${point.t}: ${point.visits ?? 0} visits`);
});

// Get trend for a specific domain
const domainResult = await tinybird.query.trend({
  domain: 'example.com'
});

// Use with a chart library
const chartData = domainResult.data.map(point => ({
  time: new Date(point.t),
  visits: point.visits ?? 0
}));

// Get trend for a specific tenant
const tenantResult = await tinybird.query.trend({
  tenant_id: 'tenant-123',
  domain: 'example.com'
});

Example Response

{
  "data": [
    {
      "t": "2024-01-15 14:00:00",
      "visits": 12
    },
    {
      "t": "2024-01-15 14:01:00",
      "visits": 15
    },
    {
      "t": "2024-01-15 14:02:00",
      "visits": null
    },
    {
      "t": "2024-01-15 14:03:00",
      "visits": 8
    }
  ]
}

Notes

  • The endpoint always returns exactly 30 data points (one per minute)
  • Minutes with no visits will have visits: null in the response
  • Results are ordered chronologically from oldest to newest
  • The trend updates in realtime as new data arrives

Build docs developers (and LLMs) love