Dynamic <script> tags in <head> not being executed

I am dynamically generating some tags (e.g. Google analytics) to go into the header.

I’m using the

{{ varname | raw }}

syntax to ensure no escape mangling takes place.

So, for example, the following is what I see generated:

<script async src="https://www.googletagmanager.com/gtag/js?id=XX-XXXXXXX-X"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', '1');</script>

But looking at the network monitor in the browser, I see zero calls being made to googletagmanager.com ?

If you manually put it in the template does it work the way you expect? What if you use a simplified script, like below?

<script>alert("Good here!");</script>
1 Like