Hi all, I believe I have everything set up as des...
# experimentation
i
Hi all, I believe I have everything set up as described in the docs, using an html tag on the front-end, but I don't seem to be getting an assignment callback If I query the request URL it seems like the feature is defined, but the experiments array is empty:
Copy code
% curl <https://gb-api.spencerkohan.com/api/features/sdk-CtRoip48W39SQYvy>
{
  "status":200,
  "features":{"show-delivery-time":{"defaultValue":false,"rules":[{"coverage":1,"hashAttribute":"id","seed":"b045dc6d-dbbd-431b-9f0b-f303dd7886cc","hashVersion":2,"variations":[false,true],"weights":[0.5,0.5],"key":"test-experiment","meta":[{"key":"0","name":"contriol"},{"key":"variant-a","name":"variant-a"}],"phase":"0","name":"Test Experiment"}]}},
  "experiments":[],
  "dateUpdated":"2024-10-03T15:01:28.770Z"
}
My experiment is running - is there any step I might be missing to get the assignment? One detail I wasn't sure about: I set my assignment attribute to "id" because I saw this was automatically set by growthbook, but I wasn't sure if I have to do something else to trigger the assignment
f
the experiment array that's empty there is only for visual editor experiments
there is an experiment as a feature flag rule that I see in that response - and thats is "show-delivery-time"
i
ah ok - thanks for clarifying do you know when the tracking callback should fire exactly? I have it defined like this:
Copy code
window.growthbook_config.trackingCallback = (experiment, result) => {
  console.log('reporting assignment event');
  fetch(trackingEndpoint, {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      shop_id: "6d259ec9-47dd-4e7e-8be6-cbce1b4e7b29",
      event_name: "userAssigned",
      parameters: {
        experiment_id: experiment.key,
        variation_id: result.key,
      },
    }),
  });
};
but the callback doesn't seem to be firing, even if the feature flag is available I also tried clearing the cookies to get a new assignment and it didn't help
f
it should fire as soon as a user is assigned
I take it you're not getting the console.log either?
i
nope nothing
f
can you share the full SDK implementation you're using?
i
sure, here's the script:
Copy code
window.growthbook_config = window.growthbook_config || {};
window.growthbook_queue = window.growthbook_queue || {};

window.growthbook_queue.push((gb) => {
  // Do whatever you need with the GrowthBook instance here
  console.log('growthbook loaded');
  console.log(gb.getAttributes());
  let attributes = gb.getAttributes();
  gb.updateAttributes({
    id: attributes.id,
    anonymous_id: attributes.id,
  });

  if(gb.isOn("show-delivery-time")) {
    console.log('show-delivery-time');
  } else {
    console.log('don\'t show-delivery-time');
  }

  document.dispatchEvent(new CustomEvent("growthbookrefresh"));

  
});
window.growthbook_config.trackingCallback = (experiment, result) => {
  console.log('reporting assignment event');
  fetch(TRACKING_ENDPOINT, {
    method: "POST",
    headers: {
      "Content-Type": "application/json", // specify the content type, if necessary
    },
    body: JSON.stringify({
      shop_id: "6d259ec9-47dd-4e7e-8be6-cbce1b4e7b29",
      event_name: "userAssigned",
      // anonymous_id: experiment
      parameters: {
        experiment_id: experiment.key,
        variation_id: result.key,
      },
    }),
  });
};
And then this is added at the end of the
head
tag:
Copy code
<script async
      data-api-host=API_HOST
      data-client-key=CLIENT_KEY
      src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/auto.min.js>"
    ></script>
I was trying the refresh event and setting an attritbute to see if it would trigger the callback but I don't think it's needed
f
ya, I dont think it is
I wonder if your isOn check is happening before the payload is loaded
i
should I try removing it?
oh one question - does the async script have to be the last in the head, or is it just important that it's after the rest of the growthbook_config setup? I notice that there are a lot of other scripts injected after it when the page is rendered
c
did you ever get this working? having the same issue
in case it helps, the problem for me was that gb.isOn(“show-delivery-time”) is not evaluated when the data is loaded. Once i put it in a callback to call it when the data is loaded, it works.
isOn
and
getFeatureValue
apparently actually initiate the experiment. https://github.com/growthbook/growthbook/issues/3121 So I believe updating the queue push to this would work.
Copy code
window.growthbook_queue.push((gb) => {
  // Do whatever you need with the GrowthBook instance here
  console.log('growthbook loaded');
  console.log(gb.getAttributes());
  let attributes = gb.getAttributes();
  gb.updateAttributes({
    id: attributes.id,
    anonymous_id: attributes.id,
  });
  
  const applyResult = () => {

    if(gb.isOn("show-delivery-time")) {
      console.log('show-delivery-time');
    } else {
      console.log('don\'t show-delivery-time');
    }
  }
  document.addEventListener("growthbookdata", applyResult)
  document.dispatchEvent(new CustomEvent("growthbookrefresh"));

  
});