Setting a Canonical URL via Javascript

We use a product at work that doesn’t include a canonical URL on its webpages. This normally isn’t a problem, but our search engine has seeing pages with different url parameters (usually the utm parameters from Google Analytics) as different pages, leading to duplicates in search results.

After contacting the company to make sure there wasn’t some sort of simple variable I could include in the header templates, I had to look for a javascript solution. Some search engines probably won’t ever see it, since they won’t execute the javascript, but Google should.

©2022 Kyle Schmidt some rights reserved