#Script tag meta variable in Head

1 messages · Page 1 of 1 (latest)

boreal sleet
#

I have a script tag in the head of my HTML to handle Google tag manager

<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer',`${meta.gTagId}`);</script>

But Astro doesn't like the variable I'm trying to pass in : ${meta.gTagId}
If I hard code that to a string, such as GTM-KMS3B59, it works fine.

Is there a way to pass in a variable here? I'm probably missing a syntax nuance
Cheers
Dwayne

signal jackal
#

Which error do you get?

boreal sleet
#

with the below there is no error, but the script tag doesnt appear in the Head at all

... 'script','dataLayer',`${meta.gTagId}`);</script>
signal jackal
#

Maybe try to use the is:inline directive on the script tag so it isn't processed

boreal sleet
#

with this option, i get no tag either

,'script','dataLayer',meta.gTagId);</script>
boreal sleet
#

and with, this option i get an error

,'script','dataLayer',{meta.gTagId});</script>

13:44:16 [vite] Internal server error: Transform failed with 1 error:
C:/Dev/Blackfinch.Websites/ui/src/layout/default.astro?astro&type=script&index=0&lang.ts:1:328: ERROR: Expected "}" but found "."
  Plugin: vite:esbuild
  File: C:/Dev/Blackfinch.Websites/ui/src/layout/default.astro?astro&type=script&index=0&lang.ts
  
  Expected "}" but found "."
  1  |  (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer', {meta.gTagId});
#

ok, ill try inline

#

what syntax should be working do you think?

#

for passing the parameter I mean

signal jackal
#

Something like this

<script is:inline>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer', `${meta.gTagId}`);</script>
boreal sleet
#

hmm, with the above, it seems like it's not interpreting the variable and I got the following script tag in build

<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer', `${meta.gTagId}`);</script>
signal jackal
#

When using is:inline, the script tag is inlined without any modifications. What would your working result look like?

boreal sleet
#

meta.gTagId would change, depending on what that prop was

#

something like

...'script','dataLayer', 'GTM-KMS3B59');</script>
#

currently if I dont have the inline attribute, the script tag is not included at all

signal jackal
boreal sleet
#

Yeah that worked a treat cheers!

#

interesting that I couldnt pass in an object, such as meta.tagId