I have different “Head Content” for designing, vs production (an analytics code).
Is there a way, instead of having to modify the Head Content, back and forth, instead, to do an “Include” (or link etc), so I only have to change it in one place?
EG… maybe a variable to indiicate design vs prod conditional, would be great, if available… eg prod=0 or 1
Thanks!
Assuming that your analytics code is in js you could do the following
if (window.location.href.indexOf("productionsite.com") !== -1) {
// Your analytics code here if the url contains your production site domain name
} else {
// Optional: Do nothing or execute alternative code for developement site
}
I’m not sure I’m doing this right… am I supposed to put this all in the head still? Here’s what I’m trying but I’m getting gibberish printing out on my browser…
<script>
if (window.location.href.indexOf("blah.bss.design") !== -1) {
<script defer data-domain="blah.bss.design/mQQQ15" src="https://api.publytics.net/js/script.manual.tagged-events.min.js">
</script>
<script>
window.publytics = window.publytics || function() { (window.publytics.q = window.publytics.q || []).push(arguments) };
publytics('pageview');
</script>
} else {
console.log("Nope");
}
</script>
Not sure if you can see it all in my reply, because it seems like it hacked out most of it…
Try:
if (window.location.href.indexOf("productionsite.com") !== -1) {
const script = document.createElement('script');
script.defer = true;
script.dataset.domain = "blah.bss.design/mQQQ15";
script.src = "https://api.publytics.net/js/script.manual.tagged-events.min.js";
document.head.appendChild(script);
window.publytics = window.publytics || function() { (window.publytics.q = window.publytics.q || []).push(arguments) };
publytics('pageview');
}
else {console.log("Nope");}
Do I have to surround that whole thing with script tags?
If you’re putting it in the head contents then yes.
If you’re putting it in a file then no.
Works with tags in the head… THANKS!
1 Like