The Code Tab and SmashGL Queries

~7 min read

The Code Tab and SmashGL Queries

The Code tab on any card editor shows — and lets you edit — the underlying SmashGL query that powers the card. The Simple tab writes SmashGL for you; the Code tab gives you direct access when you need capabilities the Simple tab doesn't cover.

Advanced: This section assumes you're comfortable with the basics of adding and configuring a card. If you haven't configured a card using the Simple tab yet, start there first.

When to Use the Code Tab

Use the Code tab when you need:

Using the Code Tab as a Data Explorer

Before committing to a visualization type, it helps to see what your data actually looks like. The Code tab lets you write a minimal query, preview the raw result, and use that to decide how to visualize it.

  1. Add a card to your dashboard (or open an existing card's editor).
  2. Switch to the Code tab.
  3. Write the simplest possible SmashGL query for your data:
GOAL      hit revenue to >= $50,000
WINDOW    over last 30 days
SOURCE    shopify.sales.net_revenue AS revenue
SMASH metric TITLED "Test"
  1. Save and view the card to see live data.
  2. Switch back to the Code tab to edit and re-test.

Understanding the Data Shape

The shape of your query's output determines which visualization fits.

Single aggregate value → Metric or Gauge

A query without GROUP BY day returns one row — the total for the period:

GOAL      hit revenue to >= $50,000
WINDOW    over last 30 days
SOURCE    shopify.sales.net_revenue AS revenue
SMASH metric TITLED "Revenue"

→ Returns one row: net_revenue: 12,450

Use Metric (to show goal progress and vs-prior comparison) or Gauge (to show arc progress toward goal only).

Daily rows → Sparkline or Chart

Add GROUP BY day ORDER BY day ASC to get one row per day:

WINDOW    over last 30 days
SOURCE    shopify.sales.net_revenue AS revenue
          GROUP BY day
          ORDER BY day ASC
SMASH sparkline WITH trend TITLED "Revenue Trend"

→ Returns 30 rows, one per day.

Look at the shape: is it informative (rising, falling, weekly cycles) or flat/erratic? If the line tells a story, Sparkline is the right choice.

Rows by product → Table

Group by a dimension to get a ranked list:

WINDOW    over last 30 days
SOURCE    shopify.sales.gross_revenue AS products
          GROUP BY product_name
          ORDER BY gross_revenue DESC
          LIMIT 10
SMASH table TITLED "Top Products"

→ Returns up to 10 rows, one per product, ranked by revenue. Use Table.

Exploration Workflow: An Example

Say you want to track gross profit for the first time.

Step 1 — Does the field exist?

GOAL      hit gp to >= 10000
WINDOW    this month
SOURCE    shopify.sales.gross_profit AS gp
SMASH metric TITLED "Gross Profit (Test)"

If the card shows a number, the field is populated. If it shows zero, check that cost data is entered for your products.

Step 2 — Is the trend informative?

WINDOW    this month
SOURCE    shopify.sales.gross_profit AS gp
          GROUP BY day
          ORDER BY day ASC
SMASH sparkline WITH trend TITLED "Gross Profit Trend (Test)"

Look at the trend line. Mostly flat with a few spikes? Use Metric instead. Clear weekly pattern or a steady rise? The sparkline is worth keeping.

Step 3 — Is a goal the right frame? Ask: do you have a specific profit target for the month? If yes → Metric with an absolute GOAL and the OKR progress bar. If the goal is "grow vs last month" → use a relative +N% goal. If the goal is a long-term annual target → use Gauge.

Step 4 — Commit:

GOAL      hit gp to >= 20,000 this month
SOURCE    shopify.sales.gross_profit AS gp
SMASH metric WITH alert TITLED "Gross Profit"

What Good Data Looks Like

Before setting a goal, check these things in the preview:

Check What to look for Action if missing
Does the field exist? Non-zero value in the card Verify your store has the underlying data (cost, orders, sessions, etc.)
Is the value plausible? Order of magnitude makes sense Check WINDOW — is it too short or too long?
Is GROUP BY day returning rows? 7–90 rows of daily data Add ORDER BY day ASC if rows are out of order
Does the trend tell a story? Rising, falling, or cyclical shape If flat, skip Sparkline and use Metric

For the full SmashGL language specification, see the SmashGL Reference.


Compound Goals

A compound goal lets you set two or more conditions on a single card, joined by AND (all required) or OR (any one sufficient).

AND — All Conditions Must Be Met

GOAL revenue >= 15000 AND orders >= 300

Use AND when all conditions are equally important — you need both the revenue number AND the order volume to consider the period a success.

OR — Any One Condition Is Sufficient

GOAL revenue >= 50000 OR gross_profit >= 20000

Use OR when you have multiple paths to success — the period is a win if either revenue or gross profit clears its target.

Using Compound Goals

Compound goals require the Code tab. Write the full compound GOAL clause in SmashGL. See the SmashGL Reference for syntax. The Simple tab supports a single goal condition only.

You can add SOFT or HARD at the end of a compound goal to apply to all sub-goals at once:

GOAL sessions >= 10000 AND orders >= 500 SOFT

RECONCILIATION NOTES (not part of the published chapter):

Still need help?