https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
m

most-plumber-30247

08/24/2023, 10:42 AM
Hello! Can anyone help me out with forced rule ? I created a string feature
display-fees-v2
with the default value
showPriceFeesWithInfo
I added a forced rule that says : if
totalFees
0 then serve
showNoFees
(see screenshot) In my code I added the following :
Copy code
const displayFees = useFeatureIsOn('display-fees-v2');
  const { value } = useExperiment({
    key: 'display-fees-v2',
    variations: ['showPriceFeesWithInfo', 'showFeesWithInfo', 'showFeesWithoutInfo', 'showNoFees'],
  });
  const showPriceFeesWithInfo = value === 'showPriceFeesWithInfo';
  const showFeesWithInfo = value === 'showPriceFeesWithInfo';
  const showFeesWithoutInfo = value === 'showPriceFeesWithInfo';
  const showNoFees = value === 'showPriceFeesWithInfo';
  const growthbook = useGrowthBook();

  useEffect(() => {
    if (!growthbook) return;
    const attributes = growthbook.getAttributes();
    growthbook.setAttributes({
      ...attributes,
      totalFees: totalFees,
    });
    growthbook.refreshFeatures();
  }, [totalFees, growthbook]);
Once I go to my page I see the default variation
showPriceFeesWithInfo
while I want it to be
showNoFees
• How do I force showNoFees when my totalFees are zero ? Thanks!
h

happy-autumn-40938

08/24/2023, 7:22 PM
Hi, the
useExperiment()
hook actually is a bypass hook for just running an inline experiment independent of any associated feature flags. Since your override rules and experiment rules are in your feature flag, you should be able to just evaluate the feature flag directly without referencing the experiment in your code.
m

most-plumber-30247

08/25/2023, 8:46 AM
Hi Bryce, thanks your response. I have updated the code with
useFeature
instead of
useExperiment
as follow:
Copy code
const growthbook = useGrowthBook();
  const { value } = useFeature('display-fees-v2');

  const showPriceFeesWithInfo = value === 'showPriceFeesWithInfo';
  const showFeesWithInfo = value === 'showFeesWithInfo';
  const showFeesWithoutInfo = value === 'showFeesWithoutInfo';
  const showNoFees = value === 'showNoFees';

  useEffect(() => {
    if (!growthbook) return;
    growthbook.setAttributes({
      ...growthbook.getAttributes(),
      totalFees: totalFees,
    });
    growthbook.refreshFeatures();
  }, [totalFees, growthbook]);
However, if
totalFees: 0
I still get the
showPriceFeesWithInfo
instead of
showNoFees
And now I see no experiments at all. Is this normal? Do you know what am I doing wrong ?
h

happy-autumn-40938

08/25/2023, 6:04 PM
Odd. Can you confirm that your page/component is wrapped by the
<GrowthBookProvider growthbook={gb}>
component? If so, maybe try console logging inside your useEffect hook (before and after the
return
) to see whether that's even firing / updating the user attributes.
m

most-plumber-30247

09/11/2023, 9:23 AM
Hi Bryce, sorry for the late reply. I am seeing your reponse right now. Yes everything is wrapped by the
<GrowthBookProvider growthbook={gb}>
, I logged growthbook in the useEffect and I see my user attributes ebing updated right away, but I still get an empty array for experiments... I guess I will have to use
useExperiment
and implement an if else in my code to get the correct experiment, but it's a shame I don't get what I am doing wrong.
8 Views