Hello everyone! Before the holidays, I set up an e...
# ask-questions
d
Hello everyone! Before the holidays, I set up an experiment and added the SDK (script tag) via Google Tag Manager. The SDK was executed and triggered the
experiment_viewed
event in the dataLayer. Now, after my vacation, I wanted to continue working on the implementation of my first experiment. However, with the same code as before the holidays (which is also displayed in the GB Dashboard), I’m now getting the following error message in Chrome's console:
Copy code
Uncaught (in promise) Error: Missing clientKey
    at Yt.D (GrowthBook.ts:342:13)
    at Yt.init (GrowthBook.ts:269:43)
    at auto-wrapper.ts:349:4
    at auto-wrapper.ts:410:21
Even though a
clientKey
is clearly defined under
data-client-key
in the script for the SDK. I have double-checked the code by copying and pasting it again, and it is correctly implemented in Tag Manager. Unfortunately, the
experiment_viewed
event is no longer being pushed to the dataLayer. What could be causing this issue? If you need any more information, let me know. Thank you so much for your help.
s
Can you share the GTM tag code?
d
<script async data-api-host="https://cdn.growthbook.io" data-client-key="sdk-gne8v9YSAltG8it3" src="https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/auto.min.js"
</script>
Here you go! Thank you for your help.
s
So, this won't work in GTM, as it strips out data attributes for security reasons. Our docs cover how to do this, but here's what you need to do:
Copy code
<script>
(function(s) {
  s=document.createElement('script'); s.async=true;
  s.dataset.clientKey="YOUR_CLIENT_KEY_HERE";
  s.src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/auto.min.js>";
  document.head.appendChild(s);
})();
</script>
https://docs.growthbook.io/guide/google-tag-manager-and-growthbook#creating-a-gtm-tag-for-the-growthbook-sdk
d
thank you! very strange it has worked in gtm preview the first time as the expirement_viewed event was firing before. Either way, there's a strange behaviour in the dataLayer. the expirement_viewed event only fires after interaction with the site (click or scroll). the SDK is firing at event #9 consent_status. Before the experiment_viewed event is firing, the variable for the feature value stays undefined. JS to get the variable: function() { if (window._growthbook && window._growthbook.getFeatureValue) { return window._growthbook.getFeatureValue("trust_widget"); } return undefined; }
@strong-mouse-55694 is there anything wrong with the implementation or is this behaviour normal?