analytics icon indicating copy to clipboard operation
analytics copied to clipboard

anonymousId coming back as undefined on iOS

Open javidjamae opened this issue 5 years ago • 1 comments

On certain iOS devices I'm noticing that when we call:

const { anonymousId, userId } = analytics.user()

both are coming back as undefined. We don't have this problem on any other device (Android, desktop, etc).

I'm guessing it has to do with not being able to access localStorage for some reason?

Curious if somehow the hasLocalStorage definition could return true even though iOS is still somehow restricting access to write or read from localStorage.

Does the analytics library depend on localStorage being available? Based on this doc, I assumed that it would fall back to cookies and then global.

Any clues as to why this might happen or how to work around it?

(Side note: I noticed this issue that I thought might be related, but it seems to be related to the GTM plugin, which we're not using.)

javidjamae avatar Dec 21 '20 17:12 javidjamae

Thanks for the report! This is odd indeed.

Do you know which versions / browsers?

Followup Q: when you say " iOS devices" you know mean react native apps do you?

Does the analytics library depend on localStorage being available? Based on this doc, I assumed that it would fall back to cookies and then global.

Yeah, the fallback order is here.


There is an undocumented feature for providing a custom storage mechanism.

It would be possible to pass a custom one like so:

import Analytics from 'analytics'
import googleAnalytics from '@analytics/google-analytics'

const analytics = Analytics({
  app: 'app-name',
  storage: {
    getItem: (key) => { /* custom thing */ },
    setItem: (key, value) => { /* custom thing */ },
    removeItem: (key) => { /* custom thing */ }
  },
  plugins: [
    googleAnalytics({
      trackingId: 'UA-121991123',
    }),
  ]
})

Note: these custom functions must be synchronous (right now)

DavidWells avatar Dec 22 '20 21:12 DavidWells