How to create conditional Canonical tag in CMS

This is incredibly helpful, Dave. Thank you so much.

Do you mind if I ask some follow-up questions?

  1. To ensure Google never gets confused about which of my domains is the Canonical, I would just need to be really consistent about always using the *https://www.*goodgoodgood.co version of the link in the Canonical tag on each page, right? Anything else I should be mindful of so I don’t dilute across multiple domains we own?

  2. For our CMS articles pages, I have a code that says, “If there’s a URL in the Canonical collection field… then use that as the Canonical URL. If not, leave blank.” Do you know how I could write that as “… If not, then make the URL of the current page the Canonical URL”? If I could figure that out, I could put that in the Custom Code section for the whole site — and it would solve all of my problems without room for mistake.

If it helps, here’s the current code:

<script>
const curl = "CANONICAL URL"
if (curl != "") {
  link=document.createElement('link');
  link.href='{{wf {&quot;path&quot;:&quot;canonical-url&quot;,&quot;type&quot;:&quot;Link&quot;\} }}';
  link.rel='canonical';
  document.getElementsByTagName('head')[0].appendChild(link);
}
</script>

Thank you again for your help!