Adding Schema to Dynamic Pages

Question:
I am struggling to add schema which is in my cms into my dynamic pages in Wix Editor. I have used the code below but when placed in velo and then doing a google structured data test, none was detected. Can anyone help with this please? I have over 600 location pages so this is important for my clients seo. Thank you!

import wixSeo from ‘wix-seo’;

$w.onReady(function () {
$w(“#locations”).onReady(() => {
let item = $w(“#locations”).getCurrentItem();

    if (item && item.LBschema) {
        try {
            let schemaJson = JSON.parse(item.LBschema);
            console.log("Schema Data:", schemaJson);
            wixSeo.setStructuredData(schemaJson);
        } catch (error) {
            console.error("Invalid JSON in LBschema:", error);
        }
    } else {
        console.warn("No schema found in LBschema field.");
    }
});

});

Hi there,

To add structured data (Schema markup) to dynamic pages in Wix Studio using Velo by Wix, follow these steps:

Step 1: Add the Schema Markup to Your Dynamic Page Using Velo
• Go to your Wix dashboard.
• Open your site in the Editor.
• Enable Velo by going to Dev Mode and turning it on.

Step 2: Add Your Schema to the Head Tag
$w.onReady(function () {
// Fetch dynamic data
const title = $w(“#dynamicDataset”).getCurrentItem().title;
const description = $w(“#dynamicDataset”).getCurrentItem().description;
const imageUrl = $w(“#dynamicDataset”).getCurrentItem().imageUrl;

// Create JSON-LD script
const schema = {
    "@context": "https://schema.org",
    "@type": "Article",
    "headline": title,
    "description": description,
    "image": imageUrl,
    "author": {
        "@type": "Person",
        "name": "John Doe"
    },
    "datePublished": new Date().toISOString()
};

// Add the script to the head
let script = document.createElement("script");
script.type = "application/ld+json";
script.text = JSON.stringify(schema);
document.head.appendChild(script);

});

Step 3: Test Your Structured Data
After publishing your site:

  1. Go to Google’s Rich Results Test https://search.google.com/test/rich-results
  2. Enter the URL of your dynamic page and check for errors.