Linus Larsson

Tracking meta tags with Google Tag Manager

This is a very useful function that I have used quite a lot. Maybe you want to import the meta description for each page to Google Analytics or perhaps the developers have made some variables available in meta tags instead of using a data layer. Of course you could make the information available in the data layer as well but that means implementing a new function in the code and a new deploy. By using the following implementation in GTM you could instead import the information that is already available in the meta information.

Create a new custom JavaScript variable. The script should look like this:

function (){
  var metaName = "author";
  var metas = document.getElementsByTagName('meta');
  var content = undefined;
  for (i = 0; i < metas.length; i++){
     if (metas[i].getAttribute("name") == metaName){
          content = metas[i].getAttribute("content");
  return content;

What this script does is that it creates an array of all meta tags. Then it checks each element in the array. If an element matches the meta name that we are looking for then the variable ‘content’ will be given the content information in the meta tag. So in order for this script to work you only have to change the variable 'metaName' to the meta tag you want to track. In the example above I have used the meta tag for author.

Some useful cases:

  • Check if a page is indexed or blocked (robots)
  • Check who the author of an article (author)
  • Check the meta description (description)
  • Check a custom meta tag (custom name)

You could also change the attribute of the meta tag in the if statement above to track meta property instead of meta name. Simply change “name” to “property” and you could use the variable to track e.g. information of how a page will be presented on social media by using the following strings in 'metaName':

  • og:title
  • og:description
  • og:site_name
  • og:url

The variable could then be used in whatever way you want. An example could be to add it as a custom dimension in the Google Analytics page view tag. Make sure that the meta tag will have time to be read in the page view state of GTM, otherwise it will be set to 'undefined'.


Leave a Reply

Your email address will not be published. Required fields are marked *


Cookie Settings

© Copyright - - 2018-2024