# Truy xuất dữ liệu phân tích

> Truy xuất dữ liệu phân tích để có thông tin chi tiết về hành vi người dùng và các chỉ số hiệu suất nhằm sử dụng thêm trong các biểu đồ.
>
> Doanh thu từ endpoint này dựa trên **sự kiện**: mỗi giá trị được quy cho **ngày giao dịch** — ngày diễn ra giao dịch mua, gia hạn hoặc hoàn tiền. Để quy doanh thu theo ngày cài đặt của từng người dùng (ví dụ: để đối chiếu với Ads Manager), hãy sử dụng endpoint cohort (`POST /api/v1/client-api/metrics/cohort/`).
>
> Giới hạn tốc độ: 2 yêu cầu mỗi giây.

## OpenAPI

```yaml
/api-specs/export-analytics-api.yaml post /api/v1/client-api/metrics/analytics/
openapi: 3.1.0
info:
  title: Adapty Export Analytics API
  version: 1.0.0
  description: |
    Adapty Export Analytics API cho phép bạn xuất dữ liệu phân tích sang định dạng CSV hoặc JSON,
    giúp bạn linh hoạt khám phá sâu hơn các chỉ số hiệu suất của ứng dụng, tùy chỉnh báo cáo
    và phân tích xu hướng theo thời gian. Với API này, bạn có thể dễ dàng truy xuất dữ liệu phân tích
    chi tiết, thuận tiện cho việc theo dõi, chia sẻ và tinh chỉnh thông tin dữ liệu khi cần.
servers:
  - url: https://api-admin.adapty.io
    description: Production server
security:
  - apikeyAuth: []
paths:
  /api/v1/client-api/metrics/analytics/:
    post:
      summary: Truy xuất dữ liệu phân tích
      description: |-
        Truy xuất dữ liệu phân tích để có thông tin chi tiết về hành vi người dùng và các chỉ số hiệu suất nhằm sử dụng thêm trong các biểu đồ.

        Doanh thu từ endpoint này dựa trên **sự kiện**: mỗi giá trị được quy cho **ngày giao dịch** — ngày diễn ra giao dịch mua, gia hạn hoặc hoàn tiền. Để quy doanh thu theo ngày cài đặt của từng người dùng (ví dụ: để đối chiếu với Ads Manager), hãy sử dụng endpoint cohort (`POST /api/v1/client-api/metrics/cohort/`).

        Giới hạn tốc độ: 2 yêu cầu mỗi giây.
      operationId: retrieveAnalyticsData
      security:
        - apikeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AnalyticsDataRequest"
            examples:
              basic:
                summary: Yêu cầu dữ liệu phân tích cơ bản
                value:
                  chart_id: revenue
                  filters:
                    date:
                      - "2024-01-01"
                      - "2024-12-31"
                    country:
                      - us
                    attribution_channel:
                      - social_media_influencers
                  period_unit: week
                  segmentation: attribution_campaign
      responses:
        "200":
          description: Đã truy xuất dữ liệu phân tích thành công
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AnalyticsDataResponse"
              example:
                data:
                  revenue:
                    data: []
                    value: 0
                    value_from: null
                    value_to: null
                    compare_value_from: null
                    compare_value_to: null
                    title: revenue
                    type: multi
                    default_aggregation: sum
                    description: |-
                      <div>Total money received from both subscriptions and one-time purchases. Does not include
                      revenue from subscriptions and purchases that were refunded afterward. Calculated before the store's fee.<br />
                      For example, there were 5 monthly $10 subs, 1 yearly $100 sub and 10 one-time $50 purchases today,<br />
                      revenue = 5*$10 + 1*$100 + 10*$50 = $650</div>
                    metric_name: Revenue
                    is_json: true
                    unit: USD
                    recommended_period: null
                  proceeds:
                    data: []
                    value: 0
                    value_from: null
                    value_to: null
                    compare_value_from: null
                    compare_value_to: null
                    title: revenue
                    type: multi
                    default_aggregation: sum
                    description: |-
                      <div>Total money received from both subscriptions and one-time purchases. Does not include
                      revenue from subscriptions and purchases that were refunded afterward. Calculated before the store's fee.<br />
                      For example, there were 5 monthly $10 subs, 1 yearly $100 sub and 10 one-time $50 purchases today,<br />
                      revenue = 5*$10 + 1*$100 + 10*$50 = $650</div>
                    metric_name: Revenue
                    is_json: true
                    unit: USD
                    recommended_period: null
                  net_revenue:
                    data: []
                    value: 0
                    value_from: null
                    value_to: null
                    compare_value_from: null
                    compare_value_to: null
                    title: revenue
                    type: multi
                    default_aggregation: sum
                    description: |-
                      <div>Total money received from both subscriptions and one-time purchases. Does not include
                      revenue from subscriptions and purchases that were refunded afterward. Calculated before the store's fee.<br />
                      For example, there were 5 monthly $10 subs, 1 yearly $100 sub and 10 one-time $50 purchases today,<br />
                      revenue = 5*$10 + 1*$100 + 10*$50 = $650</div>
                    metric_name: Revenue
                    is_json: true
                    unit: USD
                    recommended_period: null
            text/csv:
              schema:
                type: string
                description: Dữ liệu phân tích ở định dạng CSV
        "400":
          description: Yêu cầu không hợp lệ
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ErrorResponse"
        "401":
          description: Không được phép
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/UnauthorizedError"
        "429":
          description: Đã vượt quá giới hạn tốc độ. Tối đa 2 yêu cầu mỗi giây cho mỗi API key.
components:
  schemas:
    AnalyticsDataRequest:
      type: object
      required:
        - chart_id
        - filters
      properties:
        chart_id:
          type: string
          enum:
            - revenue
            - mrr
            - arr
            - arppu
            - subscriptions_active
            - subscriptions_new
            - subscriptions_renewal_cancelled
            - subscriptions_expired
            - trials_active
            - trials_new
            - trials_renewal_cancelled
            - trials_expired
            - grace_period
            - billing_issue
            - refund_events
            - refund_money
            - non_subscriptions
            - arpu
            - installs
          description: Chỉ định biểu đồ bạn cần. Bạn chỉ có thể chỉ định một loại biểu đồ trong mỗi yêu cầu.
          example: revenue
        filters:
          $ref: "#/components/schemas/MetricsFilters"
        period_unit:
          type: string
          enum:
            - day
            - week
            - month
            - quarter
            - year
          description: Chỉ định khoảng thời gian để tổng hợp dữ liệu phân tích
          default: month
        date_type:
          type: string
          enum:
            - purchase_date
            - profile_install_date
          description: Chỉ định ngày nào sẽ được coi là ngày người dùng tham gia
          default: purchase_date
        segmentation:
          type: string
          description: Đặt cơ sở để phân khúc
        format:
          type: string
          enum:
            - json
            - csv
          description: Chỉ định định dạng tệp xuất
          default: json
    AnalyticsDataResponse:
      type: object
      description: Phản hồi chứa dữ liệu phân tích cho loại biểu đồ được yêu cầu
      properties:
        data:
          type: object
          description: Đối tượng chứa các loại chỉ số khác nhau (revenue, proceeds, net_revenue, v.v.)
          additionalProperties:
            $ref: "#/components/schemas/MetricData"
    ErrorResponse:
      type: object
      properties:
        errors:
          type: array
          items:
            type: object
            properties:
              source:
                type: string
              errors:
                type: array
                items:
                  type: string
        error_code:
          type: string
        status_code:
          type: integer
    UnauthorizedError:
      type: object
      properties:
        errors:
          type: array
          items:
            type: object
            properties:
              source:
                type: string
              errors:
                type: array
                items:
                  type: string
        error_code:
          type: string
        status_code:
          type: integer
    MetricsFilters:
      type: object
      required:
        - date
      properties:
        date:
          type: array
          items:
            type: string
          description: Nhập ngày hoặc khoảng thời gian bạn muốn truy xuất dữ liệu biểu đồ
        compare_date:
          type: array
          items:
            type: string
          description: Nhập ngày hoặc khoảng thời gian để so sánh
        store:
          type: array
          items:
            type: string
          description: Lọc theo cửa hàng ứng dụng nơi thực hiện giao dịch mua
        country:
          type: array
          items:
            type: string
          description: Lọc theo mã quốc gia 2 chữ cái nơi thực hiện giao dịch mua
        store_product_id:
          type: array
          items:
            type: string
          description: Mã định danh duy nhất của sản phẩm từ cửa hàng ứng dụng
        duration:
          type: array
          items:
            type: string
          description: Chỉ định thời hạn gói đăng ký
        attribution_source:
          type: array
          items:
            type: string
          description: Nguồn tích hợp cho attribution
        attribution_status:
          type: array
          items:
            type: string
          description: Cho biết attribution là organic hay non-organic
        attribution_channel:
          type: array
          items:
            type: string
          description: Kênh marketing dẫn đến giao dịch
        attribution_campaign:
          type: array
          items:
            type: string
          description: Chiến dịch marketing mang lại giao dịch
        attribution_adgroup:
          type: array
          items:
            type: string
          description: Nhóm quảng cáo attribution mang lại giao dịch
        attribution_adset:
          type: array
          items:
            type: string
          description: Bộ quảng cáo attribution dẫn đến giao dịch
        attribution_creative:
          type: array
          items:
            type: string
          description: Các yếu tố hình ảnh hoặc văn bản cụ thể trong quảng cáo hoặc chiến dịch được theo dõi để đo lường hiệu quả
        offer_category:
          type: array
          items:
            type: string
          description: Chỉ định các danh mục ưu đãi bạn muốn truy xuất dữ liệu
        offer_type:
          type: array
          items:
            type: string
          description: Chỉ định các loại ưu đãi bạn muốn truy xuất dữ liệu
        offer_id:
          type: array
          items:
            type: string
          description: Chỉ định các ưu đãi cụ thể bạn muốn truy xuất dữ liệu
    MetricData:
      type: object
      description: Dữ liệu chỉ số riêng lẻ chứa các giá trị, mô tả và siêu dữ liệu
      properties:
        data:
          type: array
          description: Mảng các điểm dữ liệu cho chỉ số (có thể rỗng đối với các chỉ số tổng hợp)
          items:
            type: object
        value:
          type: number
          description: Giá trị chính của chỉ số này
        value_from:
          type: number
          nullable: true
          description: Giá trị bắt đầu của kỳ (nếu áp dụng)
        value_to:
          type: number
          nullable: true
          description: Giá trị kết thúc của kỳ (nếu áp dụng)
        compare_value_from:
          type: number
          nullable: true
          description: Giá trị bắt đầu cho kỳ so sánh (nếu áp dụng)
        compare_value_to:
          type: number
          nullable: true
          description: Giá trị kết thúc cho kỳ so sánh (nếu áp dụng)
        title:
          type: string
          description: Tiêu đề hiển thị của chỉ số
        type:
          type: string
          description: "Loại chỉ số (ví dụ: 'multi', 'single')"
        default_aggregation:
          type: string
          description: "Phương pháp tổng hợp mặc định (ví dụ: 'sum', 'average')"
        description:
          type: string
          description: Mô tả HTML giải thích chỉ số này đại diện cho điều gì và cách tính toán
        metric_name:
          type: string
          description: Tên nội bộ của chỉ số
        is_json:
          type: boolean
          description: Dữ liệu có ở định dạng JSON hay không
        unit:
          type: string
          description: "Đơn vị đo lường (ví dụ: 'USD', 'percent')"
        recommended_period:
          type: object
          nullable: true
          description: Khoảng ngày được đề xuất cho chỉ số này
          properties:
            date_from:
              type: string
              description: Ngày bắt đầu của kỳ được đề xuất
            date_to:
              type: string
              description: Ngày kết thúc của kỳ được đề xuất
            period_unit:
              type: string
              description: Đơn vị kỳ được đề xuất (day, week, month, v.v.)
  securitySchemes:
    apikeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: |
        Bạn cần xác thực các yêu cầu API bằng secret API key của mình trong header Authorization.
        Bạn có thể tìm thấy key này trong App Settings. Định dạng là `Api-Key {YOUR_SECRET_API_KEY}`,
        ví dụ: `Api-Key secret_live_...`.
```
