flagsmith-js-client icon indicating copy to clipboard operation
flagsmith-js-client copied to clipboard

Feature: add setTraits, allow it to be called before init

Open OscarGalindo opened this issue 7 years ago • 14 comments

Hi

As the subject says, setTrait can not be called before init() as it says in the documentation.

If you do it, you will do requests to undefinedtraits/.

setTraits depends on api that is set during the initialization:

setTrait = (key, trait_value) => {
    const { getJSON, identity, api } = this;

    const body = {
        "identity": {
            "identifier": identity
        },
        "trait_key": key,
        "trait_value": trait_value
    }

    return getJSON(`${api}traits/`, 'POST', JSON.stringify(body))
        .then(this.getFlags)
};

OscarGalindo avatar Nov 27 '19 08:11 OscarGalindo

Hey @OscarGalindo Correct you can’t call setTrait before init, are you expecting to be able to? Or are you just wanting better error handling for this?

kyle-ssg avatar Nov 28 '19 09:11 kyle-ssg

Actually will be fine if I can set everything before initialisation and then the client sdk only do 1 single request instead of 3-4 (one per trait + init).

But in any case, at least, the documentation should be fixed

Thanks!

OscarGalindo avatar Nov 28 '19 09:11 OscarGalindo

Ah I believe it’s actually identify that can be called before init, which prevents fetching flags in init without the user, though I’ll add a note for the other calls like setTrait.

Yes batching traits and sending as an object would be useful, this is currently in our backlog.

I imagine it would something like bulletTrain.setTraits({username:’abc’, otherProp’xyz’})

Edit: I’ve renamed the issue to something more actionable, I’ll keep this open until setTraits is done and can be called before init

kyle-ssg avatar Nov 28 '19 09:11 kyle-ssg

Whats the progress on this? Every trait I set causes a rerender

adenta avatar Apr 29 '20 15:04 adenta

Hey @adenta, will have a discussion with backend team about getting this progressed this evening. Thanks for being patient!

kyle-ssg avatar Apr 29 '20 16:04 kyle-ssg

In this example in the documentation setTrait is called before init:

/*
Can be called both before or after you're done initialising the project.
Calling identify before will prevent flags being fetched twice.
*/
bulletTrain.identify("bullet_train_sample_user"}); //This will create a user in the dashboard if they don't already exist
bulletTrain.setTrait("favourite_colour","blue"); //This save the trait against the user, it can be queried with bulletTrain.getTrait 

//Standard project initialisation
bulletTrain.init({
...

It also seems to work when I try it. Has there been updates since this conversation? PS: We also really need a setTraits function since having getFlags called for every setTrait is really bad.

davidskaarup avatar May 04 '20 15:05 davidskaarup

In this example in the documentation setTrait is called before init:

/*
Can be called both before or after you're done initialising the project.
Calling identify before will prevent flags being fetched twice.
*/
bulletTrain.identify("bullet_train_sample_user"}); //This will create a user in the dashboard if they don't already exist
bulletTrain.setTrait("favourite_colour","blue"); //This save the trait against the user, it can be queried with bulletTrain.getTrait 

//Standard project initialisation
bulletTrain.init({
...

It also seems to work when I try it. Has there been updates since this conversation? PS: We also really need a setTraits function since having getFlags called for every setTrait is really bad.

Yeah none of the conversation really affects this, identify will still not hit the api if it is called before init even after this PR.

RE setTraits, the new API piece is on our staging environment, I'll be doing some final testing against it in the next couple hours.

I'll then be releasing this under version 0.1.0 for both the JS and react-native clients.

kyle-ssg avatar May 04 '20 17:05 kyle-ssg

Yeah none of the conversation really affects this, identify will still not hit the api if it is called before init even after this PR.

But it also calls setTrait just after identify, before init, isn't that what the conversation is about?

RE setTraits, the new API piece is on our staging environment, I'll be doing some final testing against it in the next couple hours.

I'll then be releasing this under version 0.1.0 for both the JS and react-native clients.

That sounds great!

davidskaarup avatar May 04 '20 18:05 davidskaarup

Right, sorry yep I see, there was a couple conversations going on here. In future I'll try to limit these issues to 1 topic so we have something that's concise and actionable.

The latest release 0.1.1 of the JS and React Native clients include the following

  • setTraits, allowing you to specify key value pairs for multiple traits, setting null as the value for any trait will remove the trait.
  • calling setTrait and setTraits will not attempt a call to getFlags if it is called before bulletTrain.init
  • documentation updated to describe how the above works.

I'll leave this issue open incase you guys have any feedback @davidskaarup @adenta @OscarGalindo.

kyle-ssg avatar May 06 '20 11:05 kyle-ssg

Thank you - I will try it out right away 👍

davidskaarup avatar May 06 '20 11:05 davidskaarup

@kyle-ssg I have a quick question about setTraits. In the documentation it says:

Setting a value of null for a trait will remove that trait.

Will that work the same way if the value is undefined?

davidskaarup avatar May 06 '20 12:05 davidskaarup

No it wont @davidskaarup, this is intentional.

kyle-ssg avatar May 06 '20 13:05 kyle-ssg

Going to close this, feel free to re-open if you guys have any issues.

kyle-ssg avatar May 09 '20 11:05 kyle-ssg

setTrait is still unable to be called before init. It looks like it's expecting api to be set and throwing an error if it isn't. Should that code fallback to the default api value if init hasn't been called?

mbugbee avatar Nov 03 '20 16:11 mbugbee