Embedding Setup
Embed one metric without a dashboard
Show one saved chart in your app with dbn-metric: no dashboard around it, your theme, your height.
Do this with your agent
embed-dashboardHelp me with this Databrain task: Embed one metric without a dashboard. Check the current dashboard, host app and guest token scope where relevant. Show the planned changes and how to verify them. Ask before applying changes.
Use <dbn-metric> to put one saved chart on a page of your app, with no dashboard around it. It takes the same guest token, theme, parts and sizing as <dbn-dashboard>.
Before you start
- The metric is an embed of your data app. Find a metric ID shows how to add it and copy its ID.
- Your server makes guest tokens with an API key that has Access Metrics. See Create a guest token.
- Install
@databrainhq/plugin. The chart design, sizing and part examples below require{{PLUGIN_VERSION}}or later. - Use a bundler or a browser import map for the package import below. For direct HTML use the CDN setup.
Steps
-
Load the web component module and put
<dbn-metric>where the chart goes. Setmetric-idto the metric ID:<dbn-metric metric-id="YOUR_METRIC_ID" ui-version="v2" color-scheme="dark" sizing-mode="auto" ></dbn-metric> -
Get a guest token from your server and set it on the element. Keep the API key on the server:
<script type="module"> import '@databrainhq/plugin/web'; const metric = document.querySelector('dbn-metric'); const response = await fetch('/analytics/guest-token'); if (!response.ok) throw new Error('Could not load analytics'); metric.token = (await response.json()).token; </script>
The chart loads with the rows the guest token allows.
Options
| Attribute | What it does |
|---|---|
metric-id | The metric to show. The old name metricid still works. |
token | The guest token. It sets the data app and the rows the viewer can see. |
ui-version | v2 uses the v2 chart design. The default is legacy. Older embeds can use data-dbn-ui="v2" if their plugin supports it. When both are set, data-dbn-ui wins. |
sizing-mode | fixed (the default) fills the box you give it, so give the element a height. auto grows with the chart and sends dbn-height-change. See Sizing. |
theme-name, color-scheme | A named theme, and its light or dark look. See Theme fields. |
metric-filter-options | Values and defaults for the metric’s own saved filters. |
global-filter-options | Defaults for dashboard filters. See Filter state. |
Every attribute has the full list.
Follow the height
In auto mode the element sends dbn-height-change each time its height changes:
document.querySelector('dbn-metric').addEventListener('dbn-height-change', (event) => {
console.log('metric height', event.detail.height);
});Style the card
The card has the same parts as a card on a dashboard:
dbn-metric::part(card-title) { font-weight: 600; }
dbn-metric::part(card-header) { padding-bottom: 4px; }
dbn-metric::part(chart) { padding: 8px; }When the chart has no data or cannot load, the card shows empty-state or error-state. Parts and states lists every part.
Filters
The metric shows its own saved filters, where you set them up in the builder. Set their values with metric-filter-options. A dashboard’s filters are not on a single metric: they belong to <dbn-dashboard>.
If something goes wrong
- The console says
dbn-metric: no metric-id was set. Setmetric-id, in kebab case. - The card does not load. The metric must be an embed of the data app the guest token was made for.
- The card is 0px tall. In
fixedmode, give the element or its container a height, or setsizing-mode="auto".