Skip to content Skip to sidebar Skip to footer

How To Use Google Tag Manager With Multiple Schema Product Reviews Using Json-ld And Variables

So I'm using Google Tag Manager to add Schema JSON-LD Product reviews to some pages and I'm stuck and unable to find any resources on the issue. The issue that I'm having is how c

Solution 1:

You can use custom JavaScript variables to store arrays of values and then set elements of these arrays as values in JSON-LD.

For example, to get the text of H1 tag you can create custom JavaScript variable with this function:

function () {returndocument.querySelector('h1').innerText;}

Let's say you will name this variable h1. Then you can use this variable as {{h1}} in JSON-LD.

JSON-LD code can be stored in custom HTML tag and filled with variables mentioned above.

If you have more then one H1 on the page you can use this variable:

function () {returndocument.querySelectorAll('h1');}

It will return an array of elements and you can loop through this array to get innerText or just look at each element like:

{{h1}}[0].innerText

Let me show my idea on this blog post example - https://netpeak.net/blog/modify-your-ppc-strategy-to-suit-voice-search/. You can get all H2 headers (or reviews or anything) from the page like this:

var allh2 = document.querySelectorAll('h2');

In terms of GTM it could be custom JavaScript variable or you can just build one custom HTML tag with JSON-LD. Next you create empty array to store inner text of these headers and push all values as object in JSON-LD notation:

var reviews = [];
for(i = 0; i < allh2.length; i ++){ 
  reviews.push({'review':allh2[i].innerText});
}

Now you have reviews variable that can be used as a value in JSON-LD markup for "reviews" key.

Post a Comment for "How To Use Google Tag Manager With Multiple Schema Product Reviews Using Json-ld And Variables"