Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bulletTrain.segment is throwing an error. #30

Closed
lomse opened this issue Aug 16, 2020 · 3 comments
Closed

bulletTrain.segment is throwing an error. #30

lomse opened this issue Aug 16, 2020 · 3 comments

Comments

@lomse
Copy link

lomse commented Aug 16, 2020

I'm using React v16.12.0

When I use bulletTrain.segment, I get the error below:
bullet_train_client__WEBPACK_IMPORTED_MODULE_1___default.a.segment

Full Code


const environmentID='dfsssffsdsfsfsdsdsggdsfs';

export const useBulletTrain = () => {
  const [hasLoaded, setHasLoaded] = useState(false);
  const [enableTimeAttendance, setEnableTimeAttendance] = useState(false);
  const [belongsToTrailUsers, setBelongToTrialUsers] = useState(false);
  
  useEffect(() => {

    bulletTrain.identify('yhjgjgjhgjgjgjg');
    bulletTrain.init({
      environmentID,
      defaultFlags: {
        enable_time_attendance: false
      },
      onChange: () => {
        try {
          bulletTrain.segment("trial_users");
        } catch (error) {
          console.log({error}) // error caught here 
        }

        setHasLoaded(true);
        setEnableTimeAttendance(bulletTrain.hasFeature("enable_time_attendance"));
        setBelongToTrialUsers(bulletTrain.segment("trial_users"));
      }
    });
  }, []);

  return { hasLoaded, enableTimeAttendance, belongsToTrailUsers };
};
@kyle-ssg
Copy link
Member

kyle-ssg commented Aug 16, 2020

You cannot get whether a user belongs to a segment directly, we may allow such things in the future but that is perhaps a misuse of the library.

The idea of segments is to control feature flags and remote config with segment overrides.

For your scenario of "trial users" you have 2 options:

Option 1: use setTrait and getTrait to detect and set whether the user is a "trial user".

Although this approach is simple it may not give you the most flexibility.

Suggested approach

It's better to create a feature called "trial_user" and override its enabled state for a segment. I'll explain with some screenshots here to give you a better idea of how this would work:

Step 1 : Create your feature flag for trial users, using whatever default you feel is best for on or off

image

Step 2: Create a segment that drives the feature flag

image

Step 3: Set the feature flag to be turned on for that segment

image

Your code would then be driven by bulletTrain.hasFeature. You'd also get the additional benefit of being able to override this feature on a per user basis / add other segments that have this feature enabled e.g by an email address regex.

@kyle-ssg
Copy link
Member

Hopefully this answers your question! If not feel free to reply and I'll get back to you.

@lomse
Copy link
Author

lomse commented Aug 18, 2020

Hi @kyle-ssg, thanks for suggesting this approach. Works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants