Skip to content

Conversation

@GochoMugo
Copy link

In the case where the website is denied access to localStorage, the tracker would fail as a DOMException is thrown when accessing window.localStorage. This PR adds a simple fix of ignoring the error and moving on. This allows us to keep tracking.

@vercel
Copy link

vercel bot commented Dec 11, 2025

@GochoMugo is attempting to deploy a commit to the umami-software Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 11, 2025

Greptile Overview

Greptile Summary

Wrapped window.localStorage access in a try-catch block to handle DOMException when storage access is denied (e.g., in sandboxed iframes or restrictive browser settings).

Key changes:

  • Changed localStorage from a const to a let variable initialized in a try-catch block
  • When storage access is denied, localStorage remains undefined instead of throwing an uncaught exception
  • The existing optional chaining (localStorage?.getItem()) safely handles the undefined case
  • Tracker continues functioning and sending analytics even when storage is unavailable

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The change is a focused defensive fix that prevents tracker failure in restricted environments. It uses proper error handling, maintains backward compatibility, and leverages existing optional chaining to safely handle undefined values. No breaking changes or side effects.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
src/tracker/index.js 5/5 Wrapped localStorage access in try-catch to handle DOMException when storage is denied, allowing tracker to continue functioning

Sequence Diagram

sequenceDiagram
    participant Browser
    participant Tracker
    participant LocalStorage
    participant TrackingAPI

    Browser->>Tracker: Load tracker script
    Tracker->>Tracker: Initialize variables
    
    alt Storage access allowed
        Tracker->>LocalStorage: Access window.localStorage
        LocalStorage-->>Tracker: Return localStorage object
        Tracker->>Tracker: Set localStorage variable
    else Storage access denied
        Tracker->>LocalStorage: Access window.localStorage
        LocalStorage-->>Tracker: Throw DOMException (SecurityError)
        Tracker->>Tracker: Catch exception, localStorage remains undefined
    end
    
    Tracker->>Tracker: Check trackingDisabled()
    Tracker->>LocalStorage: localStorage?.getItem('umami.disabled')
    
    alt localStorage is defined
        LocalStorage-->>Tracker: Return value or null
    else localStorage is undefined
        Tracker->>Tracker: Optional chaining returns undefined (safe)
    end
    
    alt Tracking not disabled
        Tracker->>TrackingAPI: Send tracking data
        TrackingAPI-->>Tracker: Response
    else Tracking disabled
        Tracker->>Tracker: Skip tracking
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

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