Is not there a JavaScript A/B test guideline in Gr...
# ask-questions
m
Is not there a JavaScript A/B test guideline in GrowthBook using only client side vainilla JavaScript in or I'm missing something?
f
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
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
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
Awesome. I think that this code must be in the docs. Don't you think?
f
Yeah, it should be. I can add an example today.
🙌 1