How to Insert Canonical Tags Using Google Tag Manager

You may have read the horror stories about duplicate content. You may have read stories of despair from website owners who have seen their blogs penalized for no reason only to find that their site is flagged for different versions of their website indexed in Google. It all came from one website but in different versions. According to Javier Lorente in the 2015 SEO Salad event in Zaragoza, Spain, Google has the tendency to index various versions of one URL. This can come from the protocol (http, https, www) or it can be because of some version of the URL generated for SEO purposes and split testing. All of these can be indexed automatically and is really beyond the control of the website owner. From here, it may seem as if no website is safe from the duplicate content penalty. As long as Google keeps indexing our website in different versions, there is always the danger of getting penalized.

But there is one way to avoid this. When thousands of website owners cried at the injustice of the duplicate content penalty, Google listened. And then they implemented the canonical tag.

Here’s what they have to say. If you think that your content will get penalized, put a canonical tag in your website. This will help the search engine robots identify the original content.

Simple, right?


It may not be that simple. Even if Google managed to give website owners a way out of their problem, implementing it is a different story.

Implementing a canonical URL may seem easy to those who know their HTML. It is all about inserting the <rel=”canonical”> tag in yourtag.

But if you have an old website with over a thousand pages, this may not be as simple. Think about it. This means that you need to go through each page in your website and add the canonical tag in each one. Not only is this tiring, it is also quite mind-numbing.

What can you do?

Well, you can hire someone to do it for you. There are plenty of virtual assistants that you can hire for mundane jobs such as this.


You can just create these tags dynamically. This way, the tags are automatically created for you.

How can you do that?

You can do it with Google Tag Manager.

In this article, you’ll learn how to create the canonical tag in Google Tag Manager. You’ll learn how to automatically insert this tag into every page in your website.

STEP 1: Sign Up For Google Tag Manager

Visit the Google Tag Manager website and sign up. It is free and you can instantly get in if you have a Google account.

STEP 2: Create an Account

Then, you’ll be asked to create an account for your website. First, you’ll need an Account name.

Once you click ‘Continue’, you just need to input your website URL and click ‘Create’.

STEP 3: Define Variables 

Next, you’ll need to define the variables for your canonical URL. For this tutorial, you will create the following variables:

  1. The Page URL Canonical
  2. The Page Protocol
  3. The Page Hostname Canonical
  4. The Page Path 

Here’s how you should create each.

Page Protocol Variable

Let’s start with the page protocol variable.

You can do this by first clicking on ‘Variables’ on the left menu.

Then, click on ‘New’ under User-Defined Variables.

Why do we need to create a user-defined variable rather than use the built-in variable called ‘Page URL’?

We do this to prevent errors. There may be some problems with the protocols and hostname if we don’t define it independently. There is a tendency to treat URLs with WWW and without WWW as the same.

Click on the pencil icon to edit the variable.

This will reveal these options. Choose ‘URL’

And then configure the variable and choose ‘Protocol’

NOTE: Don’t forget to name your variable as Page Protocol.

Page Hostname Canonical

For this, we need to create two variables. One is named ‘Page Hostname without WWW’ and the other is named ‘Page Hostname with WWW’. It has the following factors. It is a URL with a Hostname configuration.

Then, you need to create another variable that links to the first one. Instead of a URL, it is a constant.

It should look like this.

Page Path

For this variable, you just need to select it from the choices. Choose ‘Page Path’ in the ‘Variables’ dashboard. You already have this variable so you don’t need to create it.

Page URL Canonical

Again, create a constant variable.

You can do this by clicking on the pencil icon.

And then selecting ‘Constant’ under Utilities.

Then copy and paste the text below in the Value Bar.

{{Page Protocol}}://{{Page Hostname Canonical}}{{Page Path}}

It should look like this.

Congratulations! You have successfully created a Page URL Canonical variable in Google Tag Manager.

Now, let’s add it to your website.

Common advice will tell you to add the tag by copying and pasting the following into thetag.

But it seems that this is not advisable. While you can add it to the head tag directly via Google Tag Manager, adding it directly may result to the code being transferred to the end of the tag. If that happens, then Google will not be able to see the canonical tag that you have just created.

What can you do?

Generate the tag through Javascript. Here’s how. Just copy and paste the code below into yourtag.

That’s it!

To check if it is working, simply use DevTools Console in Chrome. This will let you see the code inside a website. Just click CTRL+’F’ and type ‘canonical’. This will instantly take you to the canonical tag.

What do you think?

Do you find it easy to implement Canonical Tags using Google Tag Manager?

Tell me all about your thoughts below.

Free guide on how to market your ecommerce store

Click here to subscribe to this blog

5 Response to "How to Insert Canonical Tags Using Google Tag Manager"

  1. Comment From Jill Gregory

    I have indeed heard the horror stories about duplicate content penalties, so I want to avoid these penalties at all cost. Google tag manage is a great tool to keep all my code organized.

  2. Comment From Vincent Neusome

    I love all these free tools that google provides. Who could ask for anything more. This step by step guide is super helpful.

  3. Comment From Ben Hillman

    The good thing about these tags is you can easily check to make sure that your code is correct.

  4. Comment From Chris France

    I never knew that Implementing a canonical URL was so tricky with my old site. I guess I have too many pages to easily get this done.

  5. Comment From Rebecca Thomas

    I never knew that Google could index various versions of the same URL. This is really good to know.

Comments are closed.