busy-air-96466
03/08/2023, 4:02 PM<head>
<!-- Google dataLayer variables -->
<script>
window.dataLayer = window.dataLayer || [];
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'<https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f)>;
})(window,document,'script','dataLayer','GTM-xxxxxxxx');</script>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="<https://www.googletagmanager.com/ns.html?id=GTM-xxxxxxx>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
Based on the GB tutorial I’ve put this in index.js:
window.growthbook = new GrowthBook({
apiHost: "<http://localhost:4100>",
clientKey: "sdk-xxxxxx",
enableDevMode: true,
trackingCallback: (experiment, result) => {
// TODO: Use your real analytics tracking system
console.log("Viewed Experiment", {
experimentId: experiment.key,
variationId: result.key
});
// track using GA4
if ("gtag" in window) {
window.gtag("event", "experiment_viewed", {
event_category: "experiment",
experiment_id: experiment.key,
variation_id: result.variationId,
});
} else {
console.log("no gtag");
}
}
});
GrowthBook is working in the app; I created a feature in GrowthBook and can test for it in the app and display different UI 🙂 but the above always logs “no gtag”, and also it only runs once, on page load. Has anyone used GrowthBook with Google Tag Manager?fresh-football-47124
fresh-football-47124
fresh-football-47124
fresh-football-47124
fresh-football-47124
busy-air-96466
03/08/2023, 7:59 PMhappy-autumn-40938
03/08/2023, 10:18 PMwindow.gtag
to see whether it's actually (eventually) defined.happy-autumn-40938
03/08/2023, 10:31 PMbusy-air-96466
03/10/2023, 2:05 PMbusy-air-96466
03/10/2023, 2:21 PMbusy-air-96466
03/10/2023, 2:41 PMwindow.dataLayer.push({
event: 'login',
userId,
});
fresh-football-47124
busy-air-96466
03/13/2023, 4:11 PMuseFeatureIsOn('new-main-panel');
iin a component.
const growthbook = new GrowthBook({
apiHost: '<http://localhost:4100>',
clientKey: 'sdk-xxxxxx',
enableDevMode: true,
trackingCallback: (experiment, result) => {
console.log('Viewed Experiment', {
experimentId: experiment.key,
variationId: result.key,
});
// track using GA4
window.dataLayer.push({
event: 'experiment_viewed',
event_category: 'experiment',
experiment_id: experiment.key,
variation_id: result.variationId,
});
},
});
1. I may have missed something, but gtag doesn’t seem to exist if you follow the latest GTM instructions. Instead, you have to use window.dataLayer.push to send the tracking event to GA4. Maybe that should be in the docs?
2. Is there a way to call trackingCallback again in a session - if the user logs out, and a different user logs in?busy-air-96466
03/13/2023, 4:56 PMhappy-autumn-40938
03/13/2023, 5:41 PMIn some situations,may never become defined. You can use your browser's developer tools / JavaScript console to confirm this by typinggtag()
. If undefined, you may need to manually define it — either on your page or in a GTM custom HTML tag — using JavaScript. For example:window.tag
Copy codewindow.gtag = window.gtag || function() { dataLayer.push(arguments); };
busy-air-96466
03/13/2023, 6:37 PMalert-leather-41230
03/14/2023, 2:56 PMhappy-autumn-40938
03/14/2023, 3:16 PMalert-leather-41230
03/14/2023, 3:18 PMhappy-autumn-40938
03/14/2023, 3:20 PMOpen source platform for stress free deployments, measured impact, and smarter decisions.
Powered by