freezing-ram-13953
02/29/2024, 12:05 PMasync
in the implementation instructions, which adds significant rc/timing complications, especially as DOM changes also need to wait for DOMContentLoaded.
For example, the following HEAD script, based on the sample code, is brittle when the script is loaded async
, but fine when loaded blocking:
<script async
data-api-host="<https://cdn.growthbook.io>"
data-client-key="sdk-abc123"
src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/auto.min.js>"
></script>
<script>
window.addEventListener('DOMContentLoaded', function() {
// This code is brittle, even if called inside a DOMContentLoaded listener...
// It can return false or fail depending on status of GB
if (window._growthbook?.isOn("my-feature")) {
console.log("Feature is enabled!");
}
});
</script>
While async is preferred for performance, the current script doesn’t seem to offer any easy way to register an onReady callback. Be great if a jQuery / GTM / Wistia push style loader could be used to simplify the use of async.
For example, it would be great to be able to implement the above code as follows, where onReady fires after features are loaded:
<script async
data-api-host="<https://cdn.growthbook.io>"
data-client-key="sdk-abc123"
src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/auto.min.js>"
></script>
<script>
window.addEventListener('DOMContentLoaded', function() {
window._growthbook = window._growthbook || [];
_growthbook.push({onReady: function(){
// Should be able to rely on features being loaded here
if (window._growthbook?.isOn("my-feature")) {
console.log("Feature is enabled!");
}
}});
});
</script>
Otherwise, it might be better to change the documentation/examples to include a ready-check.
Eg something like:
<script async
onload="gbReady()"
data-api-host="<https://cdn.growthbook.io>"
data-client-key="sdk-abc123"
src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/auto.min.js>"
></script>
<script>
function gbReady() {
window._growthbook.loadFeatures().then(function() {
if (document.readyState !== "loading") {
onReady();
} else {
document.addEventListener("DOMContentLoaded", onReady);
}
});}
function onReady() {
// Do all your feature checking here...
if (window._growthbook?.isOn("my-feature")) {
console.log("Feature is enabled!");
}
}
</script>
Or maybe there’s something already available I’m missing?
Thanks
Robfuture-teacher-7046
freezing-ram-13953
03/01/2024, 10:47 AMwindow.dataLayer.push(["event", "experiment_viewed", n]),
A valid event push would be like so:
window.dataLayer.push({
'event': 'experiment_viewed',
'experiment_id': experiment.key,
'variation_id': result.key
});
Cheers
Robpolite-afternoon-32745
04/12/2024, 10:04 AM