{"id":924,"date":"2022-03-04T17:08:45","date_gmt":"2022-03-04T17:08:45","guid":{"rendered":"https:\/\/organicdigital.co\/blog\/?p=924"},"modified":"2024-06-10T15:13:05","modified_gmt":"2024-06-10T14:13:05","slug":"how-to-de-index-time-sensitive-content-using-google-tag-manager","status":"publish","type":"post","link":"https:\/\/daveashworth.co\/blog\/how-to-de-index-time-sensitive-content-using-google-tag-manager\/","title":{"rendered":"How To:\u00a0 De-Index Time Sensitive Content Using Google Tag Manager"},"content":{"rendered":"\n<p>Whilst doing some <a href=\"https:\/\/daveashworth.co\/skills\/google-tag-manager-consultant\/\">Google Tag Manager consultancy<\/a> recently, I was working on a client\u2019s website where a large number of events had been listed over a number of years.\u00a0 The vast majority of these had since passed so the content was effectively redundant.<\/p>\n\n\n\n<p>The client wanted to keep the events listed to show that their venue was a vibrant hub of activity (and because no one could be bothered taking the time to remove them all and redirect the old URLs).<\/p>\n\n\n\n<p>From my point of view, there was no reason to keep these events indexed as they were no longer relevant and de-indexing these would keep the indexed content on the site \u201cfresh\u201d and minimal, rather than having search engines crawl and index unnecessary pages.<\/p>\n\n\n\n<p>It was therefore decided to noindex any event that had now passed, though to go through each event in the CMS and noindex them individually would be an arduous task, and no doubt one which would soon be forgotten about leaving more redundant content indexed.<\/p>\n\n\n\n<p>So, I came up with a solution to use <a href=\"https:\/\/daveashworth.co\/skills\/google-tag-manager-consultant\/\">Google Tag Manager<\/a> to detect the date of the event, and if it had passed, then add a noindex tag to the page.<\/p>\n\n\n\n<p>The first step was to grab the event date and time from each page.&nbsp; This was done by getting the datetime attribute from the particular div on the page \u2013 to do this, we set a variable which pulled in the DOM element using a CSS selector as followed:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"738\" height=\"601\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_date.jpg\" alt=\"Event Date\" class=\"wp-image-926\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_date.jpg 738w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_date-300x244.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_date-150x122.jpg 150w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/figure>\n\n\n\n<p>I then created a trigger that detected when an event was being viewed as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"717\" height=\"398\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_url.jpg\" alt=\"Event URL\" class=\"wp-image-927\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_url.jpg 717w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_url-300x167.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/event_url-150x83.jpg 150w\" sizes=\"(max-width: 717px) 100vw, 717px\" \/><\/figure>\n\n\n\n<p>And that in turn fired the following custom html tag:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"693\" height=\"811\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/noindex_old_event.jpg\" alt=\"NoIndex Old Event\" class=\"wp-image-928\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/noindex_old_event.jpg 693w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/noindex_old_event-256x300.jpg 256w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2022\/03\/noindex_old_event-128x150.jpg 128w\" sizes=\"(max-width: 693px) 100vw, 693px\" \/><\/figure>\n\n\n\n<p>The code used is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nvar dateInPast = function(firstDate, secondDate) \n{\n  if (firstDate.setHours(0, 0, 0, 0) &lt;= secondDate.setHours(0, 0, 0, 0)) {\n    return true;\n  }\n  return false;\n};\n\nvar event_date = new Date({{Event Date}});\nvar today_date = new Date();\nvar in_the_past = dateInPast(event_date, today_date);\n\nif (in_the_past)\n{\t\n\tjQuery('meta&#91;name=\"robots\"]').remove();\n\tvar meta = document.createElement('meta');\n\tmeta.name = 'robots';\n\tmeta.content = 'noindex, follow, noarchive';\n\tjQuery('head').append(meta);\n}\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>Here, a function is created which passed in the event date and compares it to todays date.&nbsp; If that date is in the past, it returns true, and in turn, removes any existing robots tag and inserts a new one.<\/p>\n\n\n\n<p>If you site doesn\u2019t support JQuery, you can use this code instead to add the new robots tag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var metatag = document.createElement('meta');\nmetatag.setAttribute('name', 'robots');\nmetatag.content = 'noindex,follow';\ndocument.getElementsByTagName('head')&#91;0].appendChild(metatag);<\/code><\/pre>\n\n\n\n<p>And that\u2019s it.&nbsp;&nbsp; For us, this was a good all round solution that meant minimal housekeeping for the client, and kept it\u2019s index fresh and up to date.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A case study to show how to de-index content based on a certain date having passed.<\/p>\n","protected":false},"author":1,"featured_media":925,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13,8],"tags":[],"class_list":["post-924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-crawl-rate","category-on-page-seo","category-tag-manager"],"_links":{"self":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/comments?post=924"}],"version-history":[{"count":0,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/924\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media\/925"}],"wp:attachment":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media?parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/categories?post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/tags?post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}