Skip to content

feat: add organization ID to MAU stats in Cloudflare Analytics Engine#1223

Merged
riderx merged 4 commits into
mainfrom
include_org_id_in_cf_mau_usage_table
Oct 18, 2025
Merged

feat: add organization ID to MAU stats in Cloudflare Analytics Engine#1223
riderx merged 4 commits into
mainfrom
include_org_id_in_cf_mau_usage_table

Conversation

@WcaleNieWolny
Copy link
Copy Markdown
Contributor

@WcaleNieWolny WcaleNieWolny commented Oct 14, 2025

  • Add org_id parameter to trackDeviceUsageCF function
  • Include org_id in Cloudflare Analytics indexes alongside app_id
  • Update createStatsMau to accept and pass org_id
  • Modify update.ts to pass appOwner.owner_org to MAU tracking
  • Update DeviceUsageCF interfaces to include org_id field
  • Modify readDeviceUsageCF query to retrieve org_id from index2
  • Enable organization-level MAU analytics and activity detection

This change allows for organization-level queries and filtering of MAU statistics in Cloudflare Analytics Engine, enabling future features like activity detection for organizations with recent MAU stats.

Summary by CodeRabbit

  • New Features
    • Analytics now attribute device usage and monthly active users to organizations when available, enabling org-level insights and reporting.
    • Aggregations now include both app and organization dimensions for more accurate breakdowns.
    • Backward compatible: usage without an associated organization continues to be tracked as before.

- Add org_id parameter to trackDeviceUsageCF function
- Include org_id in Cloudflare Analytics indexes alongside app_id
- Update createStatsMau to accept and pass org_id
- Modify update.ts to pass appOwner.owner_org to MAU tracking
- Update DeviceUsageCF interfaces to include org_id field
- Modify readDeviceUsageCF query to retrieve org_id from index2
- Enable organization-level MAU analytics and activity detection

This change allows for organization-level queries and filtering of MAU
statistics in Cloudflare Analytics Engine, enabling future features
like activity detection for organizations with recent MAU stats.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 14, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Extended device-usage tracking to include org_id across Cloudflare analytics. Updated function signatures, interfaces, and MAU aggregation to carry org identifiers. Propagated org_id from update flow into stats collection. Adjusted analytics indexes and read path to handle org_id.

Changes

Cohort / File(s) Summary
Cloudflare analytics tracking
supabase/functions/_backend/utils/cloudflare.ts
Added optional org_id to trackDeviceUsageCF; updated DeviceUsageCF (optional org_id) and DeviceUsageAllCF (required org_id); wrote indexes to include app_id and org_id; read path selects org_id; MAU reduce now groups and assigns org_id.
Stats API propagation
supabase/functions/_backend/utils/stats.ts
createStatsMau now accepts optional org_id and forwards it to trackDeviceUsageCF when DEVICE_USAGE is enabled; SB path unchanged.
Update flow caller change
supabase/functions/_backend/utils/update.ts
updateWithPG calls createStatsMau with appOwner.owner_org, aligning with the new signature.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant Update as updateWithPG
  participant Stats as createStatsMau
  participant CF as trackDeviceUsageCF (Cloudflare)
  participant Analytics as CF Analytics Store

  Client->>Update: Request triggering update
  Update->>Stats: createStatsMau(c, device_id, app_id, org_id)
  alt DEVICE_USAGE enabled
    Stats->>CF: trackDeviceUsageCF(c, device_id, app_id, org_id)
    CF->>Analytics: Write usage {device_id, app_id, org_id, ...}
    CF-->>Stats: ack
  else DEVICE_USAGE disabled
    Stats-->>Update: no-op for CF path
  end
  Update-->>Client: Response
  note over Analytics: Reads/grouping now include org_id in MAU aggregation
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

A hop, a bop, an org_id hop—
I stamp my paw where metrics stop.
Devices counted, apps in line,
Clouds now note the org’s design.
With carrots tall and logs so neat,
Our MAU garden’s quite complete. 🥕✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description does not follow the repository’s required template and is missing the “## Summary,” “## Test plan,” “## Screenshots,” and “## Checklist” sections, so it lacks structured information about testing steps and conformance to style guidelines. Please update the description to match the repository template by adding the “## Summary,” “## Test plan,” “## Screenshots” (if applicable), and the checklist section with the required checkboxes and details on how to test your changes.
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly summarizes the primary change by indicating the addition of an organization ID to MAU stats within the Cloudflare Analytics Engine and uses concise phrasing that aligns with the feature being implemented.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@WcaleNieWolny
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 15, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
supabase/functions/_backend/utils/cloudflare.ts (1)

45-47: Confirm Analytics Engine dataset supports the extra index

writeDataPoint now emits two indexes. Please double-check the device_usage dataset was recreated or pre-provisioned with a second index slot—otherwise Cloudflare rejects the write with a schema mismatch.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 778817d and d669a68.

📒 Files selected for processing (3)
  • supabase/functions/_backend/utils/cloudflare.ts (4 hunks)
  • supabase/functions/_backend/utils/stats.ts (1 hunks)
  • supabase/functions/_backend/utils/update.ts (1 hunks)
🔇 Additional comments (2)
supabase/functions/_backend/utils/update.ts (1)

145-145: Org context propagation looks good

Passing appOwner.owner_org here keeps the MAU write path aligned with the new Cloudflare schema while still tolerating missing orgs downstream.

supabase/functions/_backend/utils/stats.ts (1)

10-15: Signature update stays backward-compatible

Optional org_id parameter keeps existing call sites compiling while letting the Cloudflare path enrich its payload—nice tidy extension.

c.env.DEVICE_USAGE.writeDataPoint({
blobs: [device_id],
indexes: [app_id],
indexes: [app_id, org_id || ''],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ca ne crée pas de problèmes de créer un indexe sur une potentielle chaîne vide ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thing so

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riderx @Dalanir I have fixed this in 04444f6

@Dalanir Dalanir assigned Dalanir and WcaleNieWolny and unassigned Dalanir Oct 16, 2025
@riderx
Copy link
Copy Markdown
Member

riderx commented Oct 16, 2025

@WcaleNieWolny this change make Cloudflare change but forget Supabase both systems are iso with your change only in CF they unsync

@WcaleNieWolny
Copy link
Copy Markdown
Contributor Author

@riderx You have previously agreed that the system will be Cloudflare only. I have been working under this assumption

@sonarqubecloud
Copy link
Copy Markdown

@riderx
Copy link
Copy Markdown
Member

riderx commented Oct 18, 2025

All what we do is use in prod in cloudflare but we never let the code desync

Base automatically changed from split_cron_job_org_into_two to main October 18, 2025 01:02
@riderx riderx merged commit 393185b into main Oct 18, 2025
7 checks passed
@riderx riderx deleted the include_org_id_in_cf_mau_usage_table branch October 18, 2025 01:14
@riderx
Copy link
Copy Markdown
Member

riderx commented Oct 18, 2025

We have test, we have many thing who rely on this code ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants