A React SDK for displaying insights with interactive charts and data visualizations
npm install insight-sdkbash
npm install insight-sdk
npm install react react-dom # Peer deps
`
Quick Start
`tsx
import { Insight } from "insight-sdk";
const dataResolver = async (metric, grain, fromTime, toTime) => {
// Fetch your data
return [{ date: "2024-01-01", value: 1200 }];
};
type="trend"
metric="page_views"
timeGrain="daily"
timeRange={30}
dataResolver={dataResolver}
/>;
`
API Reference
$3
The main component for rendering insights.
#### Props
| Prop | Type | Required | Description |
| ------------------------- | ---------------------------------- | -------- | ---------------------------------------------------------------------------- |
| type | "trend" \| "contributor" | โ
| Type of insight to display |
| metric | string | โ
| The metric to analyze (e.g., "page_views", "revenue") |
| dimension | string | โ | Dimension for contributor insights (e.g., "country", "device") |
| timeGrain | "daily" \| "weekly" \| "monthly" | โ
| Time granularity for data aggregation |
| timeRange | number | โ
| Number of days to look back from today |
| dataResolver | function | โ
| Async function to fetch metric data |
| dimensionValuesResolver | function | โ | Async function to fetch dimension values (required for contributor insights) |
| width | string \| number | โ | Width of the chart (default: "100%") |
| height | string \| number | โ | Height of the chart (default: "400px") |
| refreshInterval | number | โ | Polling interval in ms (default: 0, no polling) |
Usage Examples
$3
Displays a line chart showing how a metric changes over time.
`tsx
import { Insight } from "insight-sdk";
const dataResolver = async (metric, grain, fromTime, toTime) => {
// Example: Fetch daily page views for the last 30 days
const data = [
{ date: "2024-01-01", value: 1200 },
{ date: "2024-01-02", value: 1350 },
{ date: "2024-01-03", value: 1180 },
];
return data;
};
type="trend"
metric="page_views"
timeGrain="daily"
timeRange={30}
dataResolver={dataResolver}
/>;
`
$3
Displays a stacked bar chart showing how different dimension values contribute to the metric.
`tsx
import { Insight } from "insight-sdk";
const dataResolver = async (metric, grain, fromTime, toTime) => {
// Example: Fetch contributor data
const data = [
{ dimension: "desktop", value: 800 },
{ dimension: "mobile", value: 600 },
{ dimension: "tablet", value: 200 },
];
return data;
};
const dimensionValuesResolver = async (metric, dimension) => {
// Return available dimension values
return ["desktop", "mobile", "tablet"];
};
type="contributor"
metric="page_views"
dimension="device"
timeGrain="daily"
timeRange={30}
dataResolver={dataResolver}
dimensionValuesResolver={dimensionValuesResolver}
/>;
``