encountering a weird issue with the Javascript SDK...
# support
c
encountering a weird issue with the Javascript SDK. We call the identify method using the sdk and it works as expected however we also need the anonymousId that is created in that call. So we use the callback function and then call
rudderanalytics.getAnonymousId()
as shown below
Copy code
rudderanalytics.identify({ action: "impression"}, traits, function(){
  console.log(rudderanalytics.getAnonymousId())
})
The issue we have is the
getAnonymousId
call just returns undefined but according to the SDK documentation it should give the actual Id. https://rudderlabs.gitbook.io/rudderlabs-1/docs/stream-sources/rudderstack-sdk-integration-guides/rudderstack-javascript-sdk/js-sdk-faqs. So why is this happening and why can't we retrieve the anonymousId after we call identify?
h
Hi @clever-army-23352, 1. Generation of anonymousId is not dependent on any event. Once the SDK loaded you can call
rudderanalytics.getAnonymousId()
2. This is not the right way to call identify
Copy code
rudderanalytics.identify({ action: "impression"}, traits, function(){
  console.log(rudderanalytics.getAnonymousId())
})
The first parameter should be the userId not some object. Ref: https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/supported-api/#identify You can make use of this onLoaded load option to get anonymousId. Ref: https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/load-js-sdk/#onloaded
c
In the case of the first item being the userId, that is only in the case when there is one to be added, but if there is not a userID calling identify will then generate an anonymousId doesn’t it?
h
In the case of the first item being the userId, that is only in the case when there is one to be added, but if there is not a userID calling identify will then generate an anonymousId doesn’t it?
No As I already mentioned anonymousId is not dependent on identify call. Once you call
rudderanalytics.load()
and the SDK is loaded it is being created.
c
Okay and if we don’t have a user I’d we just don’t pass that to identify. I will test the script again see if this solves it