JSON-LD for your website

Adding JSON-LD structured data with Google Tag Manager

Adding JSON-LD structured data with Google Tag Manager

June 30th, 2017 – 14 Comments

You’ve probably heard us talk a lot about structured data, Schema.org and JSON-LD. Schema structured data on your site can result in highlighted search results. In this article, we’ll show you how to implement structured data using the JSON-LD Schema.org markup on the pages of your site. Here, we’ll take a closer look at how to implement structured data with Google Tag Manager.

Increase chances of Google showing rich snippets of your site in the search results: learn how to add structured data with our training on structured data and SEO

Want rich snippets for your site? Try our Structured data training »

Structured data training Info

Structured data with Google Tag Manager

Google Tag Manager is a tool that can take your marketing to the next level without the need of a developer. It’s a tool that can easily add scripts or pieces of code to a page. There are several advantages to using Tag Manager to implement structured data.

For one, you can generate tags, triggers, and variables, which means that you can apply the same code again and again on different pages. For instance, if you have loads of recipes, you can create a tag with the variable “preparation time”, so the preparation time of every recipe will be taken from a recipe page. This means you won’t have to add the preparation time manually to the code of every single page. In the end, this will save you a lot of work.

In addition, Tag Manager features a preview mode, which allows you to check whether you successfully implemented your data immediately. Read the post Google Tag Manager: An Introduction to get started.

How Google Tag Manager works

First, you need to know about three important elements: Variables, Triggers and Tags. You can find these elements on the left-hand side of your workspace. A workspace is a place where you work on creating and adding pieces of code to your pages.

google tag manager structured data workspace

Tags

A tag is a piece of code that can be fired on a page of your website. You can put many things in a tag. For instance, you can add the Google Analytics tracking code in a tag. This tag will enable Google Analytics to track your website. Similarly, you can put your structured data code in a tag. In other words: a tag contains information as to what you want to add to a page.

Triggers

Tags only work when there’s a trigger attached. You need a way of telling Google Tag Manager under which condition a tag must be used, or fired, as we call it. If you have a structured data tag, the trigger tells Tag Manager on which pages to fire that tag. This is because it’s possible that not all your pages need a recipe structured data markup, for instance. Simply put, a trigger tells Tag Manager: “Please fire this tag on these pages, but not on these pages”.

Variables

Variables serve two functions. Firstly, triggers need variables to know whether or not to fire. Suppose Tag Manager runs on your page. If the value of the variable meets the conditions you set, the trigger will fire. This, in turn, allows the tag to work. Secondly, the variable provides Google Tag Manager with variable information. This means that the information can be different in different contexts. A Date Published, for example, will be different for every eBook you publish. If the trigger fires, Google Tag Manager will then fetch the specific value from the specific page it visits.

An example of a variable is the URL of a page, but you can use any element of a page as a variable. It could be an ‘Add to cart’ button, or the H1 of a page, for example. The most commonly used variables are predefined in Google Tag Manager. But things like buttons or the H1 are variables you have to define yourself. With variables, you can edit your code in such a way that it will take elements from the current page to use in a tag.

Adding JSON-LD to your site step by step

We’re going to guide you through implementing structured data on your pages. We’ll take the Schema.org type Course as an example. As stated, we’ll use JSON-LD markup. There are five steps to take:

  1. Make structured data
  2. Create tags and triggers
  3. Create variables
  4. Trigger your code
  5. Validate and publish

Step 1: Creating the structured data code

Produce structured data JSON-LD code, either by hand or by using Google’s Markup Helper. In this example, we’re using Course markup, which looks like this:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Course",
  "name": "Site structure training",
  "description": "Learn how to create site structure for your site that makes Google understand your site and makes visitors go where they need to be",
  "provider": {
    "@type": "Organization",
    "name": "Yoast",
    "sameAs": "https://yoast.com/"
  },
      "offers": {
        "@type": "Offer",
        "price": "99",
        "priceCurrency": "USD"
  }
}
</script>

After you’ve created your markup, you have to get it ready for Google Tag Manager with Yoast’s JSON-LD Script Helper tool. Paste your code and hit Submit. The tool will create a piece of code you can use in Google Tag Manager. Copy it. You’ll need it for your new tag.

Step 2: Creating tags in Tag Manager

You’re ready to make your tags and triggers. Follow the steps below:

  • Make a new tag and give it a name (Site structure training, for instance)
  • Click Tag Configuration and choose tag type: Custom HTML
  • Paste code from the script helper tool
  • Check Support document.write
  • Hit Save

google tag manager structured data tag configuration

Step 3: Creating triggers

You need to add a trigger, so it knows when to fire the tag. You can do this on the same screen you see in the screenshot above, or directly from the Triggers screen in the Workspace. Click on the Triggering space in your new tag and choose the correct Page View. Hit Save. Your snippet is now implemented as is (see below for working with variables).

If there are no triggers yet, you can add them on the same screen. If you want a trigger to a specific page, you can copy the relevant piece of the URL and add it to a new trigger. So if you only want to trigger a tag on this page: https://yoast.com/academy/course/site-structure-training/, you need to copy the part /academy/course/site-structure-training/.

Hit the New or + button to add a new trigger. Give it a name and click on Trigger Configuration. Choose Page View from the list of trigger types and click on Some Page Views. You can now choose when the tag should trigger and which conditions should be met before it’s possible. In our case, we want to trigger the tag on https://yoast.com/academy/course/site-structure-training/. That’s why we’ll choose Page Path and Equals from the dropdown, and paste the URL into the empty box.

google tag manager structured data trigger configuration

Step 4: Creating variables

Variables make it much easier to implement the same structured data on many different sites. The variables can be found on the left-hand side of the workspace as well. You’ll see all predefined variables. There’s also an option for user-defined variables. To create a variable, click on New. After that, take the following steps:

  • Name the variable
  • Click on Variable Configuration
  • Choose Variable type
  • In this example: DOM Element

The fourth step depends on the type of tag or trigger you want to create. In this example, we’ll use a DOM Element. A DOM Element is a piece of your page, like a DIV, HTML and BODY. In this example, the DOM Element is the H1, which is the most important heading of the page.

Once you’ve clicked on the DOM Element, you need to choose which method you want to use to select a page element with. In this case, we’ll use a CSS Selector. By simply entering h1 into the Element Selector, you’ve created a variable that takes the H1 of a page.

If you want to use the meta description of a page, enter meta[name=”description”] and that variable will add the meta description of your pages.

google tag manager structured data variable configuration

Once you’ve created these variables, you can use them in your tags.

google tag manager structured data meta description

As you can see, you can use the H1 variable for the “name” and Meta description variable for the “description”. Now, the Course Schema.org markup sends the right name and description to Google.

Variables make this method of implementing structured data flexible and scalable. This way, you produce code that can be used in many places, without having to add it manually or change it for every instance. You only have to set up the tags once.

Want rich snippets for your site? Try our Structured data training »

Structured data training Info

Step 5: Test, saving and publishing

You’re ready to test your code. Tag Manager has a Preview mode that lets you test code before you publish it on a live site. Go to your Workspace to activate that mode.

In your browser, go to the page you’re implementing structured data on and refresh. You’ll see the Preview tab appear and this should show you the tags that fired. If you want to know more, you can go to the Window Loaded screen to see if your variables were executed properly. If all is well, your H1 variable should now show the same value that’s visible on the site (the title). Always test your code before publishing!

google tag manager structured data preview

If all the information displayed on this screen is correct, you can publish your tag. If there are still some flaws, go through the steps again.

To publish your tag, hit the Submit button you see at the top right. Give your version a descriptive name and press Publish. Once you’ve published your structured data tag, go to the Structured Data Testing Tool and enter the URL of the page that should now contain structured data. With this tool you can check if the structured data is implemented correctly:

google tag manager structured data end result

See no errors and warnings? Well done! If you do see errors, dive in more deeply and read what Google has to say about it.

Want to learn more about structured data? Try our brand new Structured data course!

Read more: ‘Structured data with Schema.org and JSON-LD: the ultimate guide’ »


14 Responses to Adding JSON-LD structured data with Google Tag Manager

  1. digisang
    By digisang on 11 July, 2017

    Great article.
    It is very interesting to read it in retrospective.
    Thanks for sharing!

  2. Photo Shirts
    By Photo Shirts on 11 July, 2017

    I have been reading a lot about SEO lately, trying to improve my web presence using more then my one good keyword and this seems to be the most informative site yet.
    Thanks for all the good info,
    John

  3. michelle
    By michelle on 5 July, 2017

    Great Article. I had just listened to a podcast about the benefits of adding structure data through GTM, but they didn’t go into detail about how to do it. Thanks for the step-by-step.

  4. Inesdi
    By Inesdi on 5 July, 2017

    I love your articles as much as the plugin, a reference in the SEO market

  5. Sawan
    By Sawan on 5 July, 2017

    It sounds like interesting and fruitful information that you have shared in this post, a simple guide to put it into action as well. Thank you for sharing

  6. priya
    By priya on 4 July, 2017

    Great article! That is the kind of information that are supposed to be shared across the internet. Thanks for sharing.

  7. Beontop
    By Beontop on 3 July, 2017

    Thank for manual. Before we did in our html code directly.

  8. Matias
    By Matias on 3 July, 2017

    Wooow! awesome guide, i didnt know this tool,

    Thanks for sharing

  9. website design
    By website design on 3 July, 2017

    Great post, thanks for share it.

  10. Ambalal Solanki
    By Ambalal Solanki on 3 July, 2017

    Well this is perfect guidelines to learn about structured data & Google tag manager. I have read whole article but i could not understand few of the things. May be i will have read it twice or thrice to understand it correctly.

    • Annelieke
      By Annelieke on 3 July, 2017

      Hi Ambalal, which part did you not understand? Perhaps I can help you?

  11. Punit Kumar
    By Punit Kumar on 3 July, 2017

    Can you suggest any other tutorial please?

  12. onetrend
    By onetrend on 2 July, 2017

    I like it, More useful for me
    Thanks Yoast


Check out our must read articles about Technical SEO