1

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:

  1. Reordering the scripts in the head
  2. Using j.async and j.defer in the GTM script
  3. 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);
  });
miiiracle
  • 11
  • 1
  • Delaying GTM is a bad idea. What are you trying to achieve? OneTrust is typically loaded from within GTM and being used there to block other tags from firing. It seems like you're making a design mistake. – BNazaruk Jan 10 '23 at 17:33
  • I'm trying to get my Onetrust scripts to fire before GTM in the head. Unfortunately because everything needs to be in the head, I can't load OneTrust from GTM because that would require using a Custom HTML tag and those automatically load in the body – miiiracle Jan 19 '23 at 17:58
  • Why do you think onetrust needs to be in the head? – BNazaruk Jan 19 '23 at 18:21
  • 1
    If its not in the head, then my cookie banner doesn't function properly. The user preferences aren't persistent – miiiracle Jan 19 '23 at 20:54
  • You're seriously mixing things up. We almost always implement onetrust and other consent managers through GTM. It causes no issues with user preferences persistency. – BNazaruk Jan 19 '23 at 21:54
  • 1
    That's interesting because OneTrust states that their Prod CDN script needs to be placed in the head. And when it's not, I have issues with the user preferences persistency. – miiiracle Jan 20 '23 at 17:34
  • Everyone and their mom says their script has to be put in head. But even if you want to keep onetrust separated from GTM, you can. You don't need to bother delaying GTM from loading though. You can just change GTM's triggers to, effectively, wait for onetrust. By adding another trigger that onetrust fires when it's loaded and by adding an exception to existing pageview triggers to not fire if onetrust's consent cookies are missing or denied. – BNazaruk Jan 20 '23 at 17:46
  • When you integrated OneTrust with GTM did you use the container file specified in this [guide](https://my.onetrust.com/articles/en_US/Knowledge/UUID-301b21c8-a73a-05e8-175a-36c9036728dc) or set up manually establish each trigger – miiiracle Jan 20 '23 at 18:08
  • I don't think onetrust had the capability to populate DL back when I used it, so I took the values from its cookies. Also, back then, onetrust didn't supply a DL event, but it did invoke JS events, so I had to deploy a listener for that. And yes, we were maintaining the blocking triggers pretty much manually. Well, we didn't need to create more tags really, so that wasn't bothering us too much. Now I would definitely try going with merging their container file and seeing what they have there. – BNazaruk Jan 20 '23 at 18:43
  • avoid trigger groups though. they're bugged. a trigger group can only fire once per real page, so if you're an SPA, don't use them. – BNazaruk Jan 20 '23 at 19:07
  • I'm relatively new to GTM & OneTrust can you explain your comment to me? How would I get OneTrust to fire a trigger when it's loaded if its not integrated with GTM – miiiracle Jan 24 '23 at 17:06
  • See, dataLayer is just a global array. It doesn't require GTM to function as such. Judging from the guide you linked, onetrust is very capable of pushing events into the DL. The guide even offers to download a gtm container that has necessary triggers and variables declared. Even when GTM loads after onetrust, it will still be capable of reacting to the old events in the DL. Onetrust's architecture is garbage however, it will need two consecutive network requests to kick in when there's no cache, so GTM will very very likely to be there before onetrust comes around with its callback. – BNazaruk Jan 24 '23 at 17:36

0 Answers0