I'm trying to reorder the execution of scripts in my codebase. What I need is for the OneTrust scripts to fire before my GTM scripts and for whatever reason I can't seem to get that to happen.
I've tried:
- Reordering the scripts in the head
- Using
j.async
andj.defer
in the GTM script - Using the addEventListener method to attach a load event listener to the OneTrust script, and then include the Google Tag Manager script inside the event listener function.
document.addEventListener('DOMContentLoaded', function() {
var gtmScript = document.createElement('script');
gtmScript.src = 'googletagmanager.js';
document.body.appendChild(gtmScript);
});