Skip to content

Instantly share code, notes, and snippets.

@vwo-kb
Last active May 13, 2025 11:13
Show Gist options
  • Select an option

  • Save vwo-kb/198b5886b42dddd96bc2d154b4164acd to your computer and use it in GitHub Desktop.

Select an option

Save vwo-kb/198b5886b42dddd96bc2d154b4164acd to your computer and use it in GitHub Desktop.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABCD1EFG2H"></script> // In the case of GA4, this script should be loaded, replacing G-ABCD1EFG2H with your GA4 measurement ID.
<script>
(function(nonVWOScriptsToExecute, VWO_HARD_TIMEOUT) {
window._vwo_evq = window._vwo_evq || [];
var queue = window._vwo_evq;
for (var i = 0; i < queue.length; i++) {
if (queue[i][0] === 'rD') {
window.willRedirectionOccur = true;
break;
}
}
var _vwoOldPush = window._vwo_evq.push;
window._vwo_evq.push = function() {
var eventData = arguments[0],
willRedirectionOccur;
if (eventData[0] === 'rD') {
willRedirectionOccur = eventData[1];
// It will be set to true if a redirection is going to be done by VWO.
window.willRedirectionOccurByVWO = willRedirectionOccur;
}
_vwoOldPush.apply(window._vwo_evq, [].slice.call(arguments));
};
var hardLimitTimedout = false;
function tryNonVWOTracking(nonVWOScriptsToExecute) {
if ((window._vwo_code && (window._vwo_code.finished() || window._vwo_code.libExecuted)) || hardLimitTimedout) {
if (!window.willRedirectionOccurByVWO) {
clearTimeout(hardLimitTimer);
for (var i = 0; i < nonVWOScriptsToExecute.length; i++) {
try {
nonVWOScriptsToExecute[i]();
} catch (e) {}
}
return;
}
}
setTimeout(function() {
tryNonVWOTracking(nonVWOScriptsToExecute)
}, 500)
}
var hardLimitTimer = setTimeout(function() {
hardLimitTimedout = true;
}, VWO_HARD_TIMEOUT);
tryNonVWOTracking(nonVWOScriptsToExecute);
})([function() {
// GA4 code started
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-ABCD1EFG2H'); //Replace given measurement ID with your GA4 Measurement ID
// GA4 code End/Closed
}], 5000 /*MAXIMUM TIME IN SECONDS IN WHICH GA CODE WILL EXECUTE ANYWAY*/ );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment