Your comments

Managed to submit this before I had finished typing! 

This is the error that I get when trying to inject.

The script looks like it's trying to iterate through the window rather than the dataLayer.

i is also used as the variable for the container Id, so w[l][i] passes the logic but overwrites the container Id. I've got it working with

(function(w,d,s,l,i,a){w[l]=w[l]||[];var t=!1;for(a in w[l])if(w[l][a]['event']&&w[l][a]['event']==='gtm.js'){t=true;break}if(!t)w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});const f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=!0;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f)})(window,document,'script','dataLayer','GTM-XXXXXX')

Hope that all makes sense, it did in my head!