Skip to content

Analytics

Internals of the Analytics domain. For the public API, see Analytics API.

The domain is part of the ticketing lane (root paths, { respCode, respDesc, result } bodies). Checked against CoreAPI staging on 2026-11-16.

How it fits together

graph LR
    Admin["Admin frontend"] -->|/api/analytics/*, /api/report*| H["Analytics handlers"]
    Web["Storefront"] -->|POST /api/analytics/track| H
    H --> S["Services"]
    S -->|orders, customers, ticket groups| DB[("johorzoo schema")]
    S -->|visitor traces| T["Tempo (query)"]
    S --> CH["ClickHouse service"]
    H -->|InsertEvent: OTLP/HTTP log record| O["OTel collector"]
    SCH["Report schedulers"] --> S
    S --> F["Report files on disk (REPORT_STORAGE_PATH)"]
    SCH -->|ticket group email profile / SMTP| M["Email"]
  • Sales and onsite figures are computed from the commerce tables through OrderTicketGroupService and CustomerService (the repositories are passed in from setupTicketingService). Only transaction_status = 'success' orders count.
  • Member figures come from the customer table.
  • Online-visitor figures are built from traces queried from Tempo (queryWithTraceQL), cached per request in AnalyticsCache; ClickHouseService is also constructed for them.
  • Event tracking: PostAnalyticsHandler.TrackEvent validates the body and sends an OTLP/HTTP log record (scope playtelly.analytics, body = event name, attributes user_id, session_id, device_type, properties as JSON) to the OTel collector. The collector is what makes it visible to the visitor queries.
  • Auth: viewer routes use RequireWorkspacePermissionFixed(coreDB, SPATIO_TICKETING_WORKSPACE_ID, workspace:analytics:view); report routes use workspace:reports:manage the same way. Reports were ADMIN/SYSADMIN only before; MEMBER is excluded. In routes.go, specific report routes are registered before /reports/:id/....
  • Report generation (report_service.go, large) builds Excel (and charts), CSV and PDF. PDF conversion uses LibreOffice (LIBREOFFICE_PATH). Files are written under REPORT_STORAGE_PATH; the report_attachment row keeps path, size and content type.

Background jobs

Run by runScheduler in schedulers.go with one Postgres advisory lock per job.

Job Interval What it does Disable with
Report processing 30 min For each non-deleted report, if now is its scheduled day and scheduleTime window (Malaysia time) and it wasn't sent for the current period, generate and email it. Period checks: same day, ISO week, month, quarter or year. DISABLE_REPORT_SCHEDULER=true
Report email retry 10 min Re-send generated files whose email failed (is_email_sent = false). DISABLE_REPORT_EMAIL_RETRY_SCHEDULER=true

Quarterly reports fire only on the 1st of January, April, July and October; annual ones on scheduleMonth/scheduleDay. The 30-minute tick plus the schedule window means scheduleTime is honoured to within one tick.

Data model

erDiagram
    Report {
        uint report_id
        string title
        string type
        string data_options
        string frequency
        string email_to
        string format
        string attraction_ids
        string product_ids
        string reporting_period_type
        string schedule_time
        int schedule_day_of_week
        string schedule_day_of_month
        int schedule_month
        int schedule_day
        bool is_deleted
        string last_date_sent
    }
    ReportAttachment {
        uint report_attachment_id
        uint report_id
        string attachment_name
        string attachment_path
        int64 attachment_size
        string content_type
        bool is_email_sent
        string generated_date
    }
    Report ||--o{ ReportAttachment : generates

Tables johorzoo.report and johorzoo.report_attachment. data_options, format, attraction_ids and product_ids are stored semicolon-separated and split in the response.

Environment variables

Variable Used for
TICKETING_OTEL_COLLECTOR_URL Where track events are sent.
TEMPO_BASE_URL, TEMPO_USERNAME, TEMPO_PASSWORD Tempo query for online-visitor figures (credentials live in the deployment's secret store).
REPORT_STORAGE_PATH Directory for generated report files.
LIBREOFFICE_PATH LibreOffice binary for PDF conversion.
SPATIO_TICKETING_WORKSPACE_ID Workspace used for the permission checks.
DISABLE_REPORT_SCHEDULER, DISABLE_REPORT_EMAIL_RETRY_SCHEDULER Turn off a job.

ClickHouse URL and credentials are also in pkg/config (cfg.ClickHouse).

Known issues

  • The old doc listed a one_time frequency; code rejects it (oneof=daily weekly monthly quarterly annual).
  • GET /api/report reads ?reportId=, not ?id= as the old doc said; delete takes the ID in the body, not the query.
  • POST /api/report is registered as Post("report", …) without a leading slash; Fiber normalises it, but fix it.
  • POST /api/report/generate accepts mode: "local_download" (testing only); not documented for partners.
  • The track endpoint is public and unauthenticated by design; it has no rate limit or origin check.
  • Several handlers return { respCode } on success but { error, message } on failure; both shapes are documented for partners.
  • Report files are served by path from local disk, so a multi-replica deployment needs shared storage for REPORT_STORAGE_PATH.

Change history

  • 2026-08-19: scope-options, report scope and schedule fields.
  • 2026-08-12: ticketing analytics moved into CoreAPI.
  • 2026-06-23: first analytics documentation.