1

I'm setting up google structured data via javscript, using dom element

like this

    function setStructuredDataGeneral() {
        structuredData = '{"@context": "https://schema.org/", "@type": "Product", "name": "Ofertas", "image": "<?php echo $wLanding->getFSlideImage(); ?>", "offers": { "@type": "AggregateOffer", "lowPrice": "'+Math.min(...offerPrices)+'", "highPrice": "'+Math.max(...offerPrices)+'", "priceCurrency": "EUR"}}';

        var elementScriptStructuredData = document.getElementById("structuredDataGeneral");
        var text = document.createTextNode(structuredData);
        elementScriptStructuredData.appendChild(text); 
    }

then I have a sctipt tag where it goes

<script type="application/ld+json" id="structuredDataGeneral">
</script> 

when I check the website it works, generate the expected results, problem is when I try to validate the URL via google structured data tool (https://search.google.com/structured-data/testing-tool) the tag is empty.

Its because this tool doesn't load javascript or I'm doing it wrong?

unor
  • 92,415
  • 26
  • 211
  • 360
Cristian
  • 342
  • 4
  • 17
  • It probably related to the fact that `json/ld` should be part of the html that returns from the server, and not added dynamically. (no one except goolge runs the js of your page, and even google does it not so good.) – felixmosh Apr 16 '19 at 07:17
  • @felixmosh so you mean I generate the tag aswell via javascript and then add it to the html with document.write or something like that? – Cristian Apr 16 '19 at 07:34
  • No, to do it on the html that the server returns, if you are using some server-side language such as PHP, NodeJs, or similar. – felixmosh Apr 16 '19 at 08:49
  • Possible duplicate of [Does Schema.org markup work if markup is dynamically built with JavaScript?](https://stackoverflow.com/questions/29064209/does-schema-org-markup-work-if-markup-is-dynamically-built-with-javascript) – unor Apr 16 '19 at 12:55

1 Answers1

0

The Structured Data Testing Tool has very limited support for JavaScript. Use the Rich results Testing tool or the URL Inspector in the Google Search Console to make live tests that render JavaScript.

Tony McCreath
  • 2,882
  • 1
  • 14
  • 21
  • if I use inspector I can see my generated structured data on javascript, and I can place manually my code in to the google search tool, how can I know if google will see that structured data if the testing tool doesn't work correctly? – Cristian Apr 17 '19 at 09:56