Closed smblee closed 1 year ago
This is the intended behavior. Top-level variables in the component's main <script>
block do not define global variables - and when you inject a script via <svelte:head>
, that script is seen as-is by the browser, and so tries to refer to a global variable. If you need to inject a literal script with <svelte:head>
, then you need to make sure you are defining a global variable that it has access to, for example by doing window.Var = 'hi'
.
@Conduitry Hmm i think it's a bit confusing then since
<script
async
src="https://www.googletagmanager.com/gtag/js?id={GOOGLE_ANALYTICS_MEASUREMENT_ID}"
>
</script>
This actually gets injected correctly with the variable to src="https://www.googletagmanager.com/gtag/js?id=Hi"
I get that this would work since it's part of the src
tag instead of within the script
tag, but syntactically they look like they should work. Maybe putting a compiler error/warning would be good esp since this is the official implementation guide for GA, and also by the top google search results for sveltekit add google analytics
Describe the bug
This would output
console.log(VAR);
as a raw string. instead ofAST Output:
This use case comes up when installing Google Analytics with their default instruction, and you want to use a env var for the analytics id (code below)
Reproduction
https://svelte.dev/repl/a4d646c2b73b4bf28959310d7ed1c4bf?version=3.54.0
Logs
No response
System Info
Severity
annoyance
Additional Information
No response