Skip to main content

Execute formula

Enterprise

This endpoint is part of Countly Enterprise. To get access, contact sales or compare versions. Existing customers can reach the support portal with questions.

Endpoint

/o?method=calculated_metrics

Overview

Executes a formula and returns calculated values by requested buckets. Supports saved formulas and ad-hoc formulas.

Authentication

Pass api_key or auth_token as a query parameter, or send countly-token as a header. See Authentication.

Permissions

Requires formulas Read permission.

Request Parameters

ParameterTypeRequiredDescription
methodStringYesMust be calculated_metrics.
app_idStringYesTarget app ID.
modeStringYessaved, unsaved, or snapshot.
metric_idStringConditionalRequired when mode=saved.
formulaJSON String (Array)ConditionalRequired when mode=unsaved or mode=snapshot.
bucketString or JSON String (Array)YesBucket or bucket list (for example daily or ["daily","weekly","single"]).
periodStringYesCountly period format (for example 7days, 30days, month).
previousBoolean/StringNoInclude previous-period calculation when true.
metric_detailsBoolean/StringNoInclude formula metadata (title, description, unit, format, dplaces).
allow_longtaskBoolean/StringNoEnables long-task/report execution flow.
db_overrideStringNoData adapter override (compare and config are ignored for adapter selection).
comparisonBoolean/StringNoEnables comparison mode in query parameters.
api_keyStringConditionalRequired if auth_token is not provided.
auth_tokenStringConditionalRequired if api_key is not provided.

Examples

/o?
method=calculated_metrics&
app_id=64f5c0d8f4f7ac0012ab3456&
mode=saved&
metric_id=67bd31c92e7f0b0012ab4567&
bucket=["daily","weekly","single"]&
period=30days&
previous=true&
metric_details=true
/o?
method=calculated_metrics&
app_id=64f5c0d8f4f7ac0012ab3456&
mode=unsaved&
formula=[{"variables":[{"ex":{"type":"source","name":"e","params":{"event":"purchase"}}}]}]&
bucket=["daily"]&
period=7days

Response

Success Response

{
"app_id": "64f5c0d8f4f7ac0012ab3456",
"buckets": ["daily", "weekly", "single"],
"lu": "2026-02-15T11:20:48.173Z",
"title": "Revenue per Session",
"description": "Revenue divided by sessions",
"unit": "USD",
"format": "float",
"dplaces": 2,
"data": {
"daily": {
"current": {
"value": [21.11, 20.44, 22.03],
"buckets": ["2026:2:10", "2026:2:11", "2026:2:12"]
},
"previous": {
"value": [19.88, 20.01, 20.93],
"buckets": ["2026:2:7", "2026:2:8", "2026:2:9"]
}
}
}
}

Response Fields

FieldTypeDescription
app_idStringApp ID used for calculation.
bucketsArrayRequested bucket list.
luDate StringResponse timestamp.
titleStringFormula title (present when metric_details is enabled or long-task metadata is used).
descriptionStringFormula description (when available).
unitStringUnit metadata (when available).
formatStringOutput format metadata (when available).
dplacesNumberDecimal place metadata (when available).
dataObjectBucket-keyed calculation results.

Error Responses

  • 400
{
"result": "Incorrect formula"
}
  • 404
{
"result": "Formula not found."
}
  • 500
{
"result": "Formula fetch error"
}
  • 500
{
"result": "Formula range evaluation error"
}

Behavior

  • mode=unsaved and mode=snapshot parse formula content directly from formula.
  • mode=saved loads formula by metric_id and enforces visibility (global, owner, or shared email).
  • previous=true adds a previous-period result for each bucket.
  • bucket can be a single value or JSON array; response data is keyed by each bucket value.
  • If mode is missing/unsupported, the endpoint returns an empty object payload.
  • When allow_longtask is enabled, processing can be dispatched through long-task/report flow.
Implementation details

Configuration Impact

SettingDefaultAffectsUser-visible impact
api.*Server API defaultsShared API execution controls (for example processing thresholds/limits).Changes to API-level controls can affect runtime behavior, limits, or response timing for this endpoint.

Database Collections

CollectionUsed forData touched by this endpoint
countly.calculated_metricsEndpoint data sourceStores endpoint-related records this endpoint reads or modifies.
countly.long_tasksBackground task trackingStores long-task lifecycle records for asynchronous endpoint processing.