Skip to main content
Solved

getDeviceId() returns undefined

  • 20 May 2024
  • 4 replies
  • 208 views

Hello there!

I use last Amplitude SDK for Web @amplitude/analytics-browser@2.7.4. After initialization I want to use amplitude.getDeviceId() but it always returns undefined (getUserId(), getSessionId also return undefined).

In amplitude personal area I see that device id is set. I am also able to set it manually on initialization setting deviceId parameter but getDeviceId() returns undefined in any case. I cannot understand what I did wrong. Could you help please?

4 replies

I understood how to get deviceId in a proper way. It was not clear from documentation. It can be returned in this way:

amplitude.init(API_KEY).promise.then(function() {
console.log(amplitude.getDeviceId())
})

 

Userlevel 4
Hi Artem,

Thanks for reaching out and glad you were able to identify this on your end! For the Browser SDK you can reference the documentation here which also provides an example of storing it as a variable: https://www.docs.developers.amplitude.com/data/sdks/typescript-browser/#device-id-lifecycle

Best,
Jarren


P.S. Checkout upcoming events and user meetups on our events page.

Hi @jarren.patao .

Thank you for the answer. Just want to add my another mistake I did. I have two modules:

 

module A:

import * as amplitude from '@amplitude/analytics-browser'

...

amplitude.init(....)


module B:

import * as amplitude from '@amplitude/analytics-browser'

export const someHandler = () => {
console.log(amplitude.getDeviceId()) // returns undefined
}

 

The mistake is importing amplitude twice as it creates second instance of amplitude. Instead of this I just export created instance from module A and use it in module B now:

 

 module A:

import * as amplitude from '@amplitude/analytics-browser'

export const amplitudeInstance = amplitude

...

amplitude.init(....)


module B:

import { amplitudeInstance } from 'moduleA'

export const someHandler = () => {
console.log(amplitudeInstance.getDeviceId()) // it works ok
}

 

Solved :)

Userlevel 4
Hi Artem,

Glad to have been able to help and thank you for providing the additional information around the other instrumentation issue. We'll note this on our end for future troubleshooting as well! Please let us know if you ever have any questions or concerns. Have a great day!

Best,
Jarren


P.S. Checkout upcoming events and user meetups on our events page.

Reply