Skip to content

[Web] getExistingAnonymousId does not decode JSON-stringified ajs_anonymous_id from localStorage #196

@Jharrison23

Description

@Jharrison23

Bug

When analytics.js (the Segment JavaScript SDK) writes ajs_anonymous_id to localStorage, it uses JSON.stringify(), which wraps the UUID in surrounding double-quote characters. The Flutter SDK's getExistingAnonymousId() in packages/core/lib/utils/store/web.dart reads this value via localStorage.getItem() but does not decode it, so the quotes become part of the anonymous ID string.

This means any Flutter web app running on a domain where analytics.js has previously set ajs_anonymous_id will pick up a corrupted anonymous ID (e.g. "d039d945-..." instead of d039d945-...), causing identity mismatches between the JS and Flutter SDKs.

Expected behavior

anonymousId should be a clean UUID:

d039d945-572c-4148-b072-2b54ef52102a

Actual behavior

anonymousId includes surrounding quotes, which get double-escaped when serialized to analytics-flutter-userInfo.json:

{"anonymousId":"\"d039d945-572c-4148-b072-2b54ef52102a\""}

Proposed fix

json.decode the raw localStorage value in getExistingAnonymousId() before returning it, with a try/catch fallback for values that are already plain strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions