目录

MoonCohortKit

MoonCohortKit is a MoonBit foundation library for cohort, retention, funnel, and segment analysis over product events.

It is designed for MoonBit applications that need reproducible growth analytics without binding the core logic to a database, charting system, browser API, or server framework.

What It Solves

Product and data tools often need to answer questions such as:

  • Which day did each user enter a cohort?
  • How many users returned after 1, 2, or 7 days?
  • Where does a signup-to-purchase funnel lose users?
  • Which segment keeps stronger retention?
  • Can the analysis result be exported for a CLI, web UI, report, or test fixture?

MoonCohortKit provides those primitives as reusable MoonBit code.

Why It Is Not A Duplicate

MoonCohortKit is not a chart library, not a database engine, not a web analytics SDK, and not an experiment platform. It focuses on the small but reusable layer between raw events and downstream rendering:

  • event normalization
  • cohort construction
  • D+N retention matrix calculation
  • funnel conversion and drop-off calculation
  • segment aggregation
  • stable JSON export

That makes it useful for dashboards, educational tools, offline reports, low-code analytics, and later MoonBit data applications.

Core API

  • Event::new(user, day, name, segment)
  • build_cohort(events, entry_event)
  • cohort_buckets(members)
  • retention_matrix(events, members, active_event, max_age)
  • funnel(events, steps)
  • conversion_bp(steps)
  • step_dropoff_bp(steps, index)
  • segment_members(members, segment)
  • segment_size(members, segment)
  • retention_summary(cells, max_age)
  • segment_retention_matrix(events, members, segment, active_event, max_age)
  • active_users_by_day(events, active_event, max_day)
  • retention_to_json(cells)
  • funnel_to_json(steps)

Rates are represented in basis points, so 4000 means 40.00%. This avoids platform-specific floating-point formatting in the core API.

Quick Start

let events = demo_events()
let members = build_cohort(events, "signup")
let matrix = retention_matrix(events, members, "active", 3)
let steps = funnel(events, ["signup", "active", "purchase"])

println("users: \{members.length()}")
println("conversion_bp: \{conversion_bp(steps)}")
println(retention_to_json(matrix))

Run the built-in demo:

moon run cmd/main

Run checks and tests:

moon check --target all
moon test --target wasm
moon test --target wasm-gc

Repository Layout

  • types.mbt: event, cohort, retention, and funnel data structures
  • cohort.mbt: cohort construction and cohort-day summaries
  • retention.mbt: D+N retention matrix calculation
  • funnel.mbt: funnel conversion and drop-off analysis
  • segment.mbt: segment filtering and statistics
  • summary.mbt: cross-cohort retention summary and active-user buckets
  • export.mbt: stable JSON output helpers
  • templates.mbt: demo event set and example step definitions
  • cmd/main: runnable CLI demonstration
  • docs: architecture, related work, and acceptance notes

Project Boundary

MoonCohortKit deliberately does not implement event collection, persistent storage, user identity resolution, browser tracking, chart rendering, or SQL query execution. Those systems can call this library after they have loaded events into MoonBit values.

License

Apache-2.0.

关于
51.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号