Skip to content

fix: preserve analytics evaluations tracked during in-flight or failed flushes - #329

Open
shafiktanbir wants to merge 2 commits into
Flagsmith:mainfrom
shafiktanbir:fix/analytics-flush-preserve
Open

shafiktanbir wants to merge 2 commits into
Flagsmith:mainfrom
shafiktanbir:fix/analytics-flush-preserve

Conversation

@shafiktanbir

@shafiktanbir shafiktanbir commented Nov 23, 2026 •

Copy link
Copy Markdown

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

AnalyticsProcessor.flush() cleared this.analyticsData = {} only after await this.currentFlush, so any trackFeature() calls made while the HTTP POST was in flight were discarded once the request resolved. Also, fetch only rejects on network failures, so non-2xx API responses did not raise an error and dropped the flushed counts.

This copies and clears this.analyticsData before starting the request, drains evaluations queued during an in-flight flush once a flush succeeds, checks response.ok when a Response object is returned, and restores any unsent batch counts back into this.analyticsData on error.

How did you test this code?

Added unit tests in tests/sdk/analytics.test.ts covering evaluations tracked while a flush is in flight and non-ok HTTP responses, and ran npx prettier --check sdk/analytics.ts tests/sdk/analytics.test.ts, npm test, and npm run test:esm-build locally.

@shafiktanbir
shafiktanbir requested a review from a team as a code owner November 23, 2026 10:24
@shafiktanbir
shafiktanbir requested review from emyller and removed request for a team November 23, 2026 10:24

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread sdk/analytics.ts Resolved
Comment thread tests/sdk/analytics.test.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 2 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval skipped because this PR is from an external contributor.

Re-trigger cubic

@emyller

emyller commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Hi @shafiktanbir, thanks for the contribution! Is this work related to any open issue? Please help us understand where you come from, and how this impacts your Flagsmith installation.

@shafiktanbir

Copy link
Copy Markdown
Author

Hi @emyller, thanks for taking a look! This isn't tied to an open issue (I was reading through #304 and how the Node SDK handles background flushing alongside EventProcessor in #325 while evaluating feature-flag SDKs for Node/TypeScript services, so we don't have a live Flagsmith deployment impacted by this).

While reading sdk/analytics.ts, I noticed its docstring says data stays in memory until successfully posted, but flush() had two gaps under concurrent async calls:

  1. Resetting this.analyticsData = {} after await this.currentFlush wiped any trackFeature() counts recorded while the POST was in flight.
  2. fetch doesn't reject on HTTP 4xx/5xx responses, so non-2xx responses dropped the batch instead of retaining counts for the next flush.

Happy to open an issue first if you prefer tracking it there, or close this PR if AnalyticsProcessor is intentionally best-effort.

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.

2 participants