Solved

Clarification over setOnce functionality


Badge +1

Hello guys.

I am trying to write a Can please anyone clarify how exactly setOnce works? Documentation says

Sets a user property value only once. Subsequent setOnce operations on that user property will be ignored.

But it looks like that is applied to session only, i.e. if I am calling setOnce for the user property on same session, subsequent calls during this session will be ignored (which is expected). But if I am calling setOnce again for the same user on the next session, user property will be overwritten with the new value (which I wasn’t expecting). Use case is to set user property startDate, which indicates when user has started to use our app.

So the questions are:

  1. Is it intended behaviour for setOnce?
  2. If yes, is there any way to write a property for the user only once?

I am aware of Historical Count and Computed Properties, but those are unfortunately unavailable for free plans.

Thanks!

icon

Best answer by dimitri.fedorov 14 June 2022, 18:47

View original

10 replies

Userlevel 6
Badge +8

HI @dimitri.fedorov ,

 

Thanks for writing in! How are you calling the setOnce function? Is it with the HTTP API? What endpoint URL are you using and is it the latest? Or are you using an SDK?

 

  1. Is it intended behaviour for setOnce? - No, it should work as once you set it, that is it. 
  2. If yes, is there any way to write a property for the user only once? - setOnce should do that but if you can answer the above, I will look into it for you!

Kind Regards,
Denis

Badge +1

How are you calling the setOnce function? Is it with the HTTP API? What endpoint URL are you using and is it the latest? Or are you using an SDK?

Hey Denis, thanks for writing back so soon!

I am using latest com.amplitude:android-sdk:2.37.0, invocation code looks like this:

getClient().identify(new Identify()
.setOnce("startDate", DATE_FORMAT.format(now)));

 

Userlevel 6
Badge +8

Hi @dimitri.fedorov ,

 

I just tried the identify on the Android SDK but it seemed to set it just fine and I cannot change it. If you wish, I can copy and paste my MainActivity.java example for you in a PM? Maybe you can use it in a separate project and see if it works there so we can then see if it’s a coding or SDK issue. Would that be alright? 

Badge +1

@Denis Holmes Sure, feel free to do so.

I am using Ampli generated code together with Android SDK calls, because Ampli does not have setOnce. Maybe that is cause of the issue?

 

Anyway, would be happy to check your example.

Badge +1

Update after digging the issue with @Denis Holmes (many thanks!).

Somewhere in documentation (can’t find it right away) it is said property set with `setOnce` can be set to another value with any other method, i.e. with `set` or `unset`, etc. Only subsequent `setOnce` calls will be ignored.

In my case workflow was:

  1. Start anonymous session, no user ID set
  2. Set the property with `setOnce`
  3. Sign in user, set user ID
  4. Sessions and their properties are merged

Actual update of the property happens not by `setOnce`, but by merging sessions in the last step. 

It may be not obvious at the first glance, but in my case Amplitude behaves exactly as designed.

 

Maybe this behaviour will be changed in the future, maybe it won’t be changed but specified more prominently in the docs :) But as for now, please be advised.

Userlevel 6
Badge +8

Thank you for the summary @dimitri.fedorov ! I massively appreciate it!

 

@Jeremie Gluckman May I ask for you to mark Dimitri’s most recent reply as the answer? I don’t seem to have the function for some reason. 

Userlevel 6
Badge +9

Sure thing @Denis Holmes! Converted this thread to a question, enabling the ability to mark a best answer 😃

Userlevel 6
Badge +8

Ahhh I see! I’m a wee bit sleepy this week 😅 Thanks Jeremie!

Userlevel 6
Badge +9

No worries @Denis Holmes. Here to help! 

Badge +1

Update: this behaviour is explicitly mentioned in the documentation:

When users are merged, the user could "lose" user property values that were never meant to be changed (e.g. 'Start Version' or initial UTM parameters) because the new user property values will overwrite the original user property values. 

Reply