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())
})
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 :)