How to add rel=canonical with Google Tag Manager

How to add rel=canonical with Google Tag Manager

Anna Kowalska

It is possible to add a rel=canonical URL tag using Google Manager, making it far easier to insert this on every page within a site. This is especially useful for external pages that show your site but feature another URL. It may be another content aggregator, unique search engines or even Google’s own cache.

By ensuring a standard canonical across each and every URL, it actively identifies that content as the original. As the Google bots crawl your site, this content is instantly recognisable and you won’t face the penalties of content duplication.

That said, this quick and easy guide could prove to be very helpful. Although it is worth noting that using Google Tag Manger for this task is not the preferred solution. Ideally, the tag would be added to the <head> of the source code but that’s not always a possibility. When those situations arise, you’ll be thankful for this fix.

Using Google Tag Manager

On the pretence that you already have the canonical URL created dynamically, you’ll be ready to add using Google Tag Manager. As mentioned, conventionally it would be recommended to insert the following straight into the <head>:

 <link href=”{{Page URL Canonical}}” />

But that’s not going to be possible as the content in the HTML tags are only read by Google at the end of the </body>.

The solution is to use a JavaScript code to generate the tag and insert this into the <head>. This must be adapted in the following way:


 var c = document.createElement(‘link’);


 c.href = {{Page URL Canonical}};



From here, it’s simply a case of setting it to function across every page. To do this ensure that the settings for ‘Fire On’ are switched to ‘All Pages’. There’s no need to create exceptions. It couldn’t be easier!

Always check the rel=canonical works

It’s important to check that the rel=canonical is functioning as it should with code generated correctly on any given page. This can be done by utilising the DevTools Console in Chrome, or by downloading a browser plugin such as Firebug. This is especially useful as it returns the code found on a page in the Document Object Model (DOM).

The quick and easiest way to check is by opening chrome and pressing F12 before clicking on the first tab ‘Elements’. Bring up the search command by clicking Ctrl+F followed by the search query ‘canonical’. The correct URL should now appear at the end of the <head>. Give yourself a pat on the back if it does as you’ve successfully generated a rel=canonical using Google Tag Manager.

This easy and effective solution isn’t recommended protocol but it still gets the job done when conventional strategies aren’t possible.  Given the complex nature of SEO, it’s always good to have a convenient and speedy fix, leaving more time for the important stuff.