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.
We’ve just launched a brand new training on structured data and SEO. This training has an introductory price of $119. On July 2, this will jump to the regular price of $149.
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.
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.
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 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:
- Make structured data
- Create tags and triggers
- Create variables
- Trigger your code
- 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:
"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",
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￼
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.
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.
Once you’ve created these variables, you can use them in your tags.
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.
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!
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:
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’ »