Skip to content

fix page_leave event - #21

Merged
Ret2Hell merged 2 commits into
mainfrom
fix/page-leave
May 4, 2026
Merged

Ret2Hell merged 2 commits into
mainfrom
fix/page-leave

Conversation

@Ret2Hell

@Ret2Hell Ret2Hell commented May 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Enhanced time-on-page tracking with improved event emissions during page navigation.
    • Added ability to retrieve accumulated time-on-page duration.
  • Tests

    • Extended test coverage for time-on-page and page-view tracker functionality with parameterized configurations.

@coderabbitai

coderabbitai Bot commented May 4, 2026 •

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Ret2Hell has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 45 minutes and 29 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5a286bb0-c86d-472c-a889-3b3c2cc06b61

📥 Commits

Reviewing files that changed from the base of the PR and between 87cd61d and f82c2f1.

📒 Files selected for processing (2)
  • packages/core/src/autocapture/page-view.test.ts
  • packages/core/src/tracker.ts
📝 Walkthrough

Walkthrough

The PR introduces a flushTimeOnPage() function to capture accumulated page visibility duration without emitting events. Page-view tracker now conditionally emits PAGE_LEAVE events when timeOnPage is enabled, and tracker enriches them with duration from flushing. resetTimeOnPage() behavior changes to no longer emit during SPA navigation.

Changes

Time-on-Page Duration Flushing & Page-Leave Integration

Layer / File(s) Summary
Exports
packages/core/src/autocapture/index.ts, packages/core/src/autocapture/index.test.ts
flushTimeOnPage added to public autocapture exports and validated in export tests.
Time-on-Page Core
packages/core/src/autocapture/time-on-page.ts
New flushTimeOnPage() accumulates active time, resets internal state, and returns duration (≥1s) or null. resetTimeOnPage() now only resets state without emitting.
Page-View Control Flow
packages/core/src/autocapture/page-view.ts
attachPageViewTracker reads timeOnPage config flag. fireIfUrlChanged conditionally emits PAGE_LEAVE event with navigationType before PAGE_VIEW when flag is enabled.
Tracker Integration
packages/core/src/tracker.ts
SPA page-view handler enriches PAGE_LEAVE event properties by calling flushTimeOnPage() and merging returned duration into properties.
Tests
packages/core/src/autocapture/page-view.test.ts, packages/core/src/autocapture/time-on-page.test.ts
Parameterized tests with timeOnPage enabled/disabled configs. Verified PAGE_LEAVE + PAGE_VIEW ordering on navigation, flushTimeOnPage() return values, and duration accumulation behavior.

Sequence Diagram

sequenceDiagram
    participant User as User/<br/>Navigation
    participant PageView as Page-View<br/>Tracker
    participant TimeOnPage as Time-on-Page<br/>Module
    participant Tracker as Tracker/<br/>Event Emit

    rect rgba(100, 150, 200, 0.5)
    note over User,Tracker: When timeOnPage enabled: URL change
    User->>PageView: URL changes (pushState)
    PageView->>PageView: _timeOnPage = true
    PageView->>TimeOnPage: flushTimeOnPage()
    TimeOnPage-->>PageView: duration (or null)
    PageView->>Tracker: emit PAGE_LEAVE<br/>{navigationType, duration?}
    Tracker-->>Tracker: Track PAGE_LEAVE
    PageView->>Tracker: emit PAGE_VIEW<br/>{navigationType, title}
    Tracker-->>Tracker: Track PAGE_VIEW
    end

    rect rgba(150, 100, 200, 0.5)
    note over User,Tracker: When timeOnPage disabled: URL change
    User->>PageView: URL changes (pushState)
    PageView->>PageView: _timeOnPage = false
    PageView->>Tracker: emit PAGE_VIEW<br/>{navigationType, title}
    Tracker-->>Tracker: Track PAGE_VIEW
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A flush of time now flows so free,
PAGE_LEAVE and VIEW, in harmony!
Duration captured, cleanly sent,
SPA navigation—time well spent! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix page_leave event' is partially related to the changeset, referring to a real aspect of the changes (PAGE_LEAVE event handling), but lacks specificity about the main structural change: refactoring PAGE_LEAVE emission from time-on-page to page-view tracker with a new flushTimeOnPage() function.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/page-leave

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
Review rate limit: 0/1 reviews remaining, refill in 45 minutes and 29 seconds.

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

@sonarqubecloud

sonarqubecloud Bot commented May 4, 2026

Copy link
Copy Markdown

@Ret2Hell
Ret2Hell merged commit 452d3d8 into main May 4, 2026
2 checks passed
@Ret2Hell
Ret2Hell deleted the fix/page-leave branch May 4, 2026 11:36
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.

1 participant