https://www.growthbook.io/ logo
m

melodic-ambulance-10712

02/20/2023, 1:16 PM
Is not there a JavaScript A/B test guideline in GrowthBook using only client side vainilla JavaScript in or I'm missing something?
f

future-teacher-7046

02/20/2023, 2:52 PM
We don't have a full example app using vanilla JavaScript, but our docs show the basic usage. https://docs.growthbook.io/lib/js
m

melodic-ambulance-10712

02/20/2023, 3:16 PM
I want to use GTM with one tag for the experiment with the diferent variants there but I don't understand how it works. I'm experienced GTM developer, but I needvto understand how it works in order to be able to implement it in GTM.
f

future-teacher-7046

02/20/2023, 3:32 PM
Easiest would be to use what we call "inline experiments" instead of feature flags. That's where all of the targeting conditions, traffic splits, etc. are defined directly in the javascript code instead of fetched from an API. I haven't test this within GTM, but something like this should work:
Copy code
<script src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/index.min.js>"></script>
<script>
var gb = new growthbook.GrowthBook({
  attributes: {
    id: "123" // TODO: Read user/device id from a cookie/datalayer
  },
  trackingCallback: function(experiment, result) {
    // TODO: track experiment impression in the datalayer
  }
});

var res = gb.run({
  key: "my-experiment",
  variations: ["A", "B", "C"],
  weights: [0.5, 0.25, 0.25], // Traffic split between the variations
  coverage: 1  // What percent of overall traffic to include
});

if (res.value === "A") {

} else if (res.value === "B") {

} else if (res.value === "C") {

}
</script>
❤️ 2
m

melodic-ambulance-10712

02/20/2023, 5:04 PM
Awesome. I think that this code must be in the docs. Don't you think?
f

future-teacher-7046

02/20/2023, 5:08 PM
Yeah, it should be. I can add an example today.
🙌 1
13 Views