Solved

Unreliable behaviour of browser sdk init(api_key, user_id) method.

  • 23 March 2023
  • 5 replies
  • 320 views

Badge +1

When I am sending user id in init method, it sets the amplitude user id which is working fine, but when I call the init method without user id ( null ), it still uses the old user id. It should set the user id as null.
Is this an expected behaviour or it is a bug?
Do I need to use the setUserId method separately ? ( this one I don’t prefer as it is not a promise )

icon

Best answer by Vishal 23 March 2023, 23:13

View original

5 replies

Userlevel 7
Badge +10

Hi @Vishal 

I’m not quite sure here, but you can try using this to set user_id as null instead of init method

amplitude.getInstance().setUserId(null);

https://www.docs.developers.amplitude.com/data/sdks/javascript/#logged-out-and-anonymous-users

 

Badge +1

Hi @Saish Redkar thanks for your reply, I really appreciate.
But I don’t want to use setUserId(null) method as this is not a promise and I have like chain of amplitude api calls, which should be executed in the way it is written.
So using setUserId(null) ( not a promise ) will create inconsistency. 
Thanks again 😊

Badge +1

By the way @Saish Redkar does this happen on your end as well ?
I am passing null to setUserId and it causes this error can not read length of null ?
I mean this is really weird.

Badge +1

Alright I found out that if we want to reset the user id during initialisation we have to pass empty string in init(api_key, user_id || “”) method.

Userlevel 1
Badge +1

We are using Google Tag Manager.

We’re having an issue where we do not see setUserId in our Init tag anywhere, but it is coming through in our Custom event data. We do not currently have custom UserId to use and do not want to set it.

The only different item is that we do have tag sequencing on to make sure our init tag fires before the event (we’re just testing things out). Where else could this setUserId be getting set? Could it be in the GTM template somehow?

I’ve published our GTM container to make sure all changes are live. We’re using the Amplitude Browser SDK tag in GTM. My project key matches the API key I put into the tag.

Thank you

This is a custom tag we’re testing to send viewed page data as a Custom Event.

 

Reply