> ## Documentation Index
> Fetch the complete documentation index at: https://www.commercengine.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Dynamic query

> Retrive analytics data as per dynamic query passed.



## OpenAPI

````yaml https://openapi.commercengine.io/ce-admin.json post /analytics/dynamic-query
openapi: 3.1.0
info:
  title: CE Admin APIs
  version: '1.0'
  summary: Comprehensive admin interface for managing your e-commerce platform
  description: >-
    Complete administrative API suite for Commerce Engine, providing powerful
    tools to manage products, orders, customers, inventory, analytics, and
    system configuration. Designed for administrators and backend integrations
    requiring full platform control.
  license:
    name: All Rights Reserved
    url: https://www.commercengine.io/contact-us
  contact:
    email: support@commercengine.io
    url: https://www.commercengine.io/contact-us
    name: Support
servers:
  - url: https://staging.api.commercengine.io/api/v1/{store_id}/admin
    description: Staging Server
    variables:
      store_id:
        default: store_id
        description: Store ID
  - url: https://prod.api.commercengine.io/api/v1/{store_id}/admin
    description: Prod Server
    variables:
      store_id:
        default: store_id
        description: Store ID
security:
  - Authorization: []
tags:
  - name: Analytics
    description: Analytics
  - name: Catalog
    description: Catalog
  - name: Coupons & promotions
    description: Coupons & promotions
  - name: Customers
    description: Customers
  - name: Filter Options
    description: Filter Options
  - name: Inventories
    description: Inventories
  - name: Marketplace
    description: Marketplace
  - name: Marketplace Catalog
    description: Marketplace Catalog
  - name: Media Gallery
    description: Media Gallery
  - name: Metrics
    description: Metrics
  - name: Orders
    description: Orders
  - name: Payments
    description: Payments
  - name: Payouts
    description: Payouts
  - name: POS
    description: POS
  - name: POSAdmin
    description: Admin endpoints to be used as proxy in pos
  - name: Segments
    description: Segments
  - name: Shipping
    description: Shipping
  - name: SSE
    description: SSE
  - name: Store
    description: Store
  - name: Webhooks
    description: Webhooks
paths:
  /analytics/dynamic-query:
    post:
      tags:
        - Analytics
      summary: Dynamic query
      description: Retrive analytics data as per dynamic query passed.
      operationId: execute-dynamic-query
      requestBody:
        content:
          application/json:
            schema:
              type: object
              oneOf:
                - $ref: '#/components/schemas/AnalyticsStats'
                - $ref: '#/components/schemas/AnalyticsRecords'
            examples:
              Example 1:
                value:
                  schema_name: analytics_log
                  query_type: stats
                  query:
                    aggregate:
                      - type: COUNT
                        field_name: id
                        alias: events_count
                    group_by:
                      fields: origin_source
                      having_condition:
                        - field_name: events_count
                          operator: greater
                          value: 2000
                    filters:
                      - field_name: origin_source
                        operator: is-not-empty
                        value: null
                    sort_by:
                      origin_source: asc
                    limit: 25
                    offset: 0
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                  message:
                    type: string
                  content:
                    oneOf:
                      - $ref: '#/components/schemas/DynamicQueryRecords'
                      - $ref: '#/components/schemas/DynamicQueryStatsData'
                      - $ref: '#/components/schemas/DynamicQueryMetricsData'
components:
  schemas:
    AnalyticsStats:
      title: AnalyticsStats
      type: object
      required:
        - schema_name
        - query_type
        - query
      properties:
        schema_name:
          description: 'Different schema name will be as per the types of data required. '
          type: string
        query_type:
          enum:
            - stats
        query:
          type: object
          properties:
            aggregate:
              type: array
              items:
                type: object
                required:
                  - type
                  - field_name
                  - alias
                properties:
                  type:
                    enum:
                      - COUNT
                      - SUM
                      - MIN
                      - MAX
                      - AVG
                  field_name:
                    type: string
                  alias:
                    type: string
            group_by:
              description: multiple fields separated by comma.
              type: object
              properties:
                fields:
                  type: string
                having_condition:
                  description: >-
                    additional conditions on filters. only aggregate columns can
                    be passed in having condition.
                  type: array
                  items:
                    type: object
            filters:
              type: array
              items:
                $ref: '#/components/schemas/DynamicQueryFilter'
            sort_by:
              description: >-
                json object with key-value. example:
                {"field1":"asc","field2":"desc"}
              type: object
            limit:
              type: integer
              default: 25
            offset:
              type: integer
              default: 0
          required:
            - aggregate
      x-tags:
        - Analytics
    AnalyticsRecords:
      title: AnalyticsRecords
      type: object
      required:
        - schema_name
        - query_type
        - query
      properties:
        schema_name:
          type: string
        query_type:
          enum:
            - records
        query:
          type: object
          properties:
            fields:
              description: multiple fields separated by comma.
              type: string
            filters:
              type: array
              items:
                $ref: '#/components/schemas/DynamicQueryFilter'
            sort_by:
              description: >-
                json object with key-value. example:
                {"field1":"asc","field2":"desc"}
              type: object
            limit:
              type: integer
              default: 25
            offset:
              type: integer
              default: 0
          required:
            - fields
      x-tags:
        - Analytics
    DynamicQueryRecords:
      title: DynamicQueryRecords
      type: object
      properties:
        data:
          description: >-
            array of object containing key-value data as per fields passed in
            query payload.
          type: array
          items:
            type: object
        pagination:
          $ref: '#/components/schemas/Pagination'
      nullable: true
      x-tags:
        - Analytics
    DynamicQueryStatsData:
      title: DynamicQueryStatsData
      type: object
      properties:
        data:
          description: >-
            array of object containing key-value data as per fields, aggregate
            fields or group by fields passed in query payload.
          type: array
          items:
            type: object
        pagination:
          $ref: '#/components/schemas/Pagination'
      nullable: true
      x-tags:
        - Analytics
    DynamicQueryMetricsData:
      title: DynamicQueryMetricsData
      type: object
      properties:
        data:
          description: >-
            array of object. similar structure of metrics api, but actual keys
            will be decided by request payload.
          type: array
          items:
            type: object
        pagination:
          $ref: '#/components/schemas/Pagination'
      nullable: true
      x-tags:
        - Analytics
    DynamicQueryFilter:
      title: DynamicQueryFilter
      type: object
      properties:
        field_name:
          type: string
        operator:
          enum:
            - equal
            - not-equal
            - exact
            - contains
            - does-not-contain
            - is-empty
            - is-not-empty
            - starts-with
            - ends-with
            - less
            - less-or-equal
            - greater
            - greater-or-equal
            - between
        value:
          oneOf:
            - type:
                - string
                - 'null'
            - type: integer
            - type: number
            - type: boolean
            - type: array
              items:
                type: string
      x-tags:
        - Analytics
    Pagination:
      title: Pagination
      description: pagination metadata structure
      type: object
      properties:
        total_records:
          type: integer
        total_pages:
          type: integer
        previous_page:
          description: Previous page number
          type:
            - integer
            - 'null'
        limit:
          type: integer
          default: 25
        next_page:
          description: Next page number
          type:
            - integer
            - 'null'
  securitySchemes:
    Authorization:
      type: http
      scheme: bearer

````