Documentation menuAPI: Custom SQL

API

Custom SQL endpoint

This endpoint runs your own SQL against the events of the sites you can open. Scope: sql:read. It needs a member or a key.

Last updated . This guide belongs to API access and data export.

POST /organizations/:organizationId/analytics/query

FieldRequiredContent
queryYes1 to 20,000 characters
siteIdNoLimits the query to one site. Without it, all your sites are included
curl -X POST "https://app.loogika.com/api/organizations/ORG_ID/analytics/query" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "siteId": 12, "query": "SELECT pathname, count() AS pageviews FROM scoped_events WHERE type = '\''pageview'\'' AND timestamp >= now() - INTERVAL 7 DAY GROUP BY pathname ORDER BY pageviews DESC LIMIT 10" }'
{ "data": [ { "pathname": "/", "pageviews": "9120" } ], "meta": { "queryId": "b7e0d4c2", "rowCount": 1, "maxExecutionTimeSeconds": 10, "maxRows": 1000 } }

Large counts can come back as text, as in the example.

What are the rules for a query?

  • It must start with SELECT or WITH, and it must be one statement.
  • It can read from scoped_events only. You can build your own CTEs on top of it.
  • The time parameters do not apply. Put the period in the query with a condition on timestamp.
  • Double quotes, backticks and comments are not allowed outside text values.

Custom dashboards and SQL lists the columns.

What are the limits?

LimitValue
Rows1,000. A longer result is cut without a warning
Run time10 seconds
Requests60 per minute

A query that breaks a rule, or that runs too long, returns status 400 with the reason.