Skip to content

Conversation

@andrewbrook
Copy link

Discussion

Using the Page Lifecycle API to detect when a page is backgrounded, terminated, etc. and flush the logs. This is intended to capture some portion of queued logs that might otherwise be lost, though it is not 100% reliable.

Testing

Unit tested, manually tested

API Changes

N/A

@changeset-bot
Copy link

changeset-bot bot commented Dec 10, 2025

⚠️ No Changeset found

Latest commit: 55f5af5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @andrewbrook, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the telemetry system by ensuring that logs are flushed proactively when a web page is backgrounded or hidden. By integrating with the Page Lifecycle API, the system can now detect when a page's visibility state changes, preventing potential loss of queued telemetry data that might otherwise occur if the page were to be terminated without a final submission. This change aims to improve the completeness and reliability of collected telemetry information.

Highlights

  • Immediate Log Flushing: Implemented a mechanism to immediately flush queued telemetry logs when a web page enters a 'hidden' state, such as when the user switches tabs or minimizes the browser.
  • Page Lifecycle API Integration: Utilized the browser's Page Lifecycle API, specifically the visibilitychange event, to detect when the page's visibility state changes to 'hidden' and trigger the log flush.
  • Code Refactoring: The flush function, responsible for forcing telemetry data to be sent, has been moved from api.ts to helpers.ts for better organization.
  • New Event Listener Registration: A new registerListeners function was introduced in helpers.ts and integrated into the telemetry service initialization to set up the visibilitychange event handler.
  • Dependency Update and Testing: Added @firebase/util as a dependency to leverage utility functions like isNode and included comprehensive unit tests for the new registerListeners functionality, ensuring it behaves correctly in both browser and Node.js environments.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a useful feature to flush telemetry logs when a page's visibility state changes to 'hidden', helping to prevent data loss. The implementation is clean, with the logic correctly placed and new unit tests to verify the behavior. I have one suggestion to further improve the reliability by also listening to the pagehide event, which is a stronger signal for page termination.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Dec 10, 2025

Size Report 1

Affected Products

  • @firebase/telemetry

    TypeBase (d5eaa45)Merge (ea1b878)Diff
    browser9.29 kB9.62 kB+331 B (+3.6%)
    module9.29 kB9.62 kB+331 B (+3.6%)
  • firebase

    TypeBase (d5eaa45)Merge (ea1b878)Diff
    firebase-telemetry.js88.5 kB88.8 kB+267 B (+0.3%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/QnyuAfs69N.html

@andrewbrook andrewbrook marked this pull request as ready for review December 10, 2025 15:53
@andrewbrook andrewbrook requested review from a team as code owners December 10, 2025 15:53
@google-oss-bot
Copy link
Contributor

google-oss-bot commented Dec 10, 2025

Size Analysis Report 1

Affected Products

  • @firebase/telemetry

    • captureError

      Size

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      size5.55 kB5.93 kB+381 B (+6.9%)
      size-with-ext-deps64.9 kB65.3 kB+386 B (+0.6%)

      Dependency

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      functions

      captureError
      createLoggerProvider
      getAppVersion
      getSessionId
      isExportRetryable
      parseRetryAfterToMills
      registerTelemetry
      startNewSession

      captureError
      createLoggerProvider
      flush
      getAppVersion
      getSessionId
      isExportRetryable
      parseRetryAfterToMills
      registerListeners
      registerTelemetry
      startNewSession

      + flush
      + registerListeners

    • flush

      Size

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      size4.95 kB5.20 kB+252 B (+5.1%)
      size-with-ext-deps64.3 kB64.6 kB+256 B (+0.4%)

      Dependency

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      functions

      createLoggerProvider
      flush
      getAppVersion
      getSessionId
      isExportRetryable
      parseRetryAfterToMills
      registerTelemetry
      startNewSession

      createLoggerProvider
      flush
      getAppVersion
      getSessionId
      isExportRetryable
      parseRetryAfterToMills
      registerListeners
      registerTelemetry
      startNewSession

      + registerListeners

    • getTelemetry

      Size

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      size4.99 kB5.37 kB+381 B (+7.6%)
      size-with-ext-deps71.3 kB71.7 kB+386 B (+0.5%)

      Dependency

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      functions

      createLoggerProvider
      getAppVersion
      getSessionId
      getTelemetry
      isExportRetryable
      parseRetryAfterToMills
      registerTelemetry
      startNewSession

      createLoggerProvider
      flush
      getAppVersion
      getSessionId
      getTelemetry
      isExportRetryable
      parseRetryAfterToMills
      registerListeners
      registerTelemetry
      startNewSession

      + flush
      + registerListeners

    • nextOnRequestError

      Size

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      size5.92 kB6.31 kB+381 B (+6.4%)
      size-with-ext-deps72.3 kB72.6 kB+386 B (+0.5%)

      Dependency

      TypeBase (d5eaa45)Merge (ea1b878)Diff
      functions

      captureError
      createLoggerProvider
      getAppVersion
      getSessionId
      getTelemetry
      isExportRetryable
      nextOnRequestError
      parseRetryAfterToMills
      registerTelemetry
      startNewSession

      12 dependencies

      captureError
      createLoggerProvider
      flush
      getAppVersion
      getSessionId
      getTelemetry
      isExportRetryable
      nextOnRequestError
      parseRetryAfterToMills
      registerListeners
      registerTelemetry
      startNewSession

      + flush
      + registerListeners

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/ZfWDdDMQVm.html

Copy link

@tonybaroneee tonybaroneee left a comment

Choose a reason for hiding this comment

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

Nice!

@andrewbrook andrewbrook merged commit 07916b3 into telemetry Dec 10, 2025
45 checks passed
@andrewbrook andrewbrook deleted the telemetry4 branch December 10, 2025 16:28
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