Add scripts to header from string

Hi everyone I want to add a few scripts to my header, but I want to receive these scripts from Sanity CMS, so I will receive it as string. Any way to do it?
1 Reply
Daniel Sousa @TutoDS
I need to do this for google tag manager and Facebook meta pixel Any idea? But for example, how to do it for this 2 codes:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<id>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<id>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<id>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

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

gtag('config', '<id>');
</script>
That is what I didn't understand I can receive a string or not, I'm trying to give the flexibility to do it but I don't know how to do with the primitive I'm seeing this example but I don't have sure how to adapt it for these two scripts Yes I can Sorry to bother you @Tito, but can you please help me trasnforming one of the scripts into an example using the primitive you share? yes, I have a private repo on Github but it's a simple solid start app using data from Sanity CMS I'm adding the scripts manually instead on sanity to be easier no, I'm only don't know how to put the scripts using the primitive and type for example the window That part I understand
<script async src="https://www.googletagmanager.com/gtag/js?id=<id>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

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

gtag('config', '<id>');
</script>
This will be something like:
createScriptLoader({
src: https://www.googletagmanager.com/gtag/js?id=<id>,
onLoad: () => {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '<id>');
}
})
createScriptLoader({
src: https://www.googletagmanager.com/gtag/js?id=<id>,
onLoad: () => {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '<id>');
}
})
But whow you type the window to have the dataLayer? And how to do this one?
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<id>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<id>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
Solid start have html file? Bu that isn't only for SSR?
Want results from more Discord servers?
Add your server