Cookie Consent by Free Privacy Policy Generator

Deploying Noindex Meta Robots Tag with JavaScript with Tag Manager - 2 Methods

This tutorial provides insights into deploying the noindex tag using JavaScript, helping you efficiently handle SEO for thin or low-quality pages, duplicate content URLs, or rogue facetted/parameter URLs that may otherwise negatively impact your site's rankings.

noindex-javascript.png


Managing the visibility of your web pages can be a daunting task, especially when you don't have direct access to alter the DOM head on a website. In such cases, understanding how to use tools like Google Tag Manager and the noindex meta robots tag can be invaluable.

We'll also delve into the use of lookup tables within Google Tag Manager, an efficient method to implement noindex tags across multiple URLs simultaneously. So, whether you're working with WordPress, Shopify, or any other platform, these tips will guide you towards effectively managing your site's visibility in search engines.

Method 1: Deploying Noindex Meta Robots Tag with JavaScript
In the vast and fast-paced realm of digital marketing and SEO, you may often find yourself without the necessary permissions to edit the DOM head on a client's website. However, there's no need to despair. There's a handy solution that allows you to deploy the noindex meta robots tag on a URL, despite these restrictions.

This guide will walk you through using Google Tag Manager (GTM) and native JavaScript to implement the noindex tag. This tag is particularly useful when you want to prevent search engines from indexing certain pages—such as those with thin or low-quality content, duplicate content URLs, or rogue facetted/parameter URLs. This method, not only injects a fresh meta robots tag if there was none in the DOM head but also overrides any existing meta robots tag.

Method 2: Deploying Noindex Meta Robots Tag with Lookup Table
When it comes to managing SEO across a number of pages on a website, efficiency and scalability become essential. If you need to apply a noindex tag on multiple URLs, then using a lookup table in Google Tag Manager (GTM) can prove to be a lifesaver.

This guide will cover the process of creating a lookup table for deploying the noindex tag on various URLs without having to manually set up individual tags for each. You'll learn how to create a mapping of input values to output values in GTM and how to incorporate this lookup table into your noindex meta robots tag deployment process. This approach not only saves time but also simplifies the management of your noindex tags across numerous URLs.

Deploying the noindex tag on a single URL (Method 1)
Firstly, log into your Google Tag Manager account and establish a new tag. For the purpose of clarity, we're going to call this tag "NoIndex MetaRobots". However, feel free to name it something that suits your preferences. The tag configuration type you want to go for is "Custom HTML". Once that's sorted, copy the following JavaScript snippet and paste it into the text editor box:

Code:
<script>
var metas = document.getElementsByTagName('meta');
for(var i=0; i<metas.length; i++) {
    if(metas[i].getAttribute("name") == "robots") {
        document.getElementsByTagName('head')[0].removeChild(metas[i]);
    }
}

var meta = document.createElement('meta');
meta.name = 'robots';
meta.content = 'noindex';
document.getElementsByTagName('head')[0].appendChild(meta);
</script>

Step 2: Define the Firing Trigger Configuration

Next up, we need to configure a trigger. We're going to label this one "NoIndex MetaRobots trigger". When setting up the trigger type, select "Page View DOM Ready". You'll want the trigger to fire on "Some DOM ready events" (make sure not to choose 'All DOM ready events'). Set the event conditions to 'Page URL' > 'Contains' > /url-goes-here/, and then hit save on the trigger.

Step 3: Publish the New Tag and Trigger

Now, it's time to put your new tag and trigger into action. Publish them to your live Google Tag Manager container. To check how the tag is performing, have a look at the rendered HTML on the page you deployed the tag on. Remember to refresh your browser window if it's already open. Note that viewing the page source is different from viewing the rendered HTML.


Deploying the noindex tag on multiple URLs (Method 2)
If you're looking to deploy the noindex tag on several URLs, using a lookup table could save you some time. A Lookup Table is a feature within Google Tag Manager that allows you to create a simple mapping of input values to output values. This feature is quite useful if you need to define different outputs based on the current page URL or any other variable you might use. When it comes to deploying a noindex tag across multiple URLs, this becomes particularly handy.

Creating a Lookup Table for Noindex Tag

Let's follow the steps to create a lookup table:

Step 1: Create a New Variable

  1. In Google Tag Manager, go to 'Variables' and click 'New'.
  2. Name the variable something appropriate like "Lookup Table - Noindex URLs".
Step 2: Setup the Lookup Table

  1. In the Variable Configuration, select "Lookup Table".
  2. Choose the Input Variable. For our case, this will be "Page URL".
  3. Then define your input and output pairs. The input would be the URL where you want to apply the noindex tag, and the output should be 'noindex'.
Step 3: Adjust Your Tag

  1. Go back to your NoIndex MetaRobots tag.
  2. Replace the hardcoded 'noindex' in the script with your new variable like so:
Code:
<script>
var metas = document.getElementsByTagName('meta');
for(var i=0; i<metas.length; i++) {
    if(metas[i].getAttribute("name") == "robots") {
        document.getElementsByTagName('head')[0].removeChild(metas[i]);
    }
}

var meta = document.createElement('meta');
meta.name = 'robots';
meta.content = {{Lookup Table - Noindex URLs}};
document.getElementsByTagName('head')[0].appendChild(meta);
</script>

Remember, the {{ }} notation is used to refer to the variable you've created.

Step 4: Check and Publish

Finally, preview and test your setup before hitting the 'Publish' button to push these changes live. With this setup, you can manage the deployment of your noindex tag across multiple URLs directly from this lookup table variable, making the entire process more scalable and easy to manage.
 
Back
Top