wix-seo on dynamic page

Hi everyone,

I am looking to had structured data on dynamic pages.
When looking at the API, I found wix-seo, which should do exactly what I want.

This is the code provided by the API.

import wixSeo from 'wix-seo';

$w.onReady( () => {
  wixSeo.setStructuredData(
    [ 
      {
        "@context": "http://schema.org",
        "@type": "Organization",
        "name": "My Organization Name",
        "url": "https://www.myorgdomain.com"
      },
      {
        "@context": "http://schema.org",
        "@type": "Person",
        "email": "mailto:john.doe@somedomain.com",
        "jobTitle": "Professor",
        "name": "John Doe",
        "telephone": "(555) 555-555"
      }
    ]
  )
  .then( () => {
    console.log("structured data set");
  } )
  .catch( () => {
    console.log("failed setting structured data");
  } );
} );

Now, what I want to do is to load a different code for each dynamic page. And this is where I am stuck.

What do I have to add to the code in order to find which page the user is on, and add the specific code to it.

I have no idea what I need to add in code, and how to insert my code in my database.

On my dynamic page, I have my dataset used to build the page.
I think that I can have a second dataset on my dynamic page, with all the code for all my page inside. Then I need to add a code that use the data from the second dataset to add the structured code to the page…

I am quite lost… If anyone can help

Thanks

Hello Oscar

Here is a code sample that might help you


import wixSeo from 'wix-seo';

// on page ready 
$w.onReady(()=>{
  $w('#dynamicDataset').onReady(()=>{
    // get the current page "data"
    let data =  $w('#dynamicDataset').getCurrentData();
    wixSeo.setStructuredData(getStructure(data))
  });
});

// convt current page data to structure data
function getStructure(data) {
  let struc = [ 
      {
        "@context": "http://schema.org",
        "@type": "Organization",
        "name": data.title,
        "url": data.url
      },
      {
        "@context": "http://schema.org",
        "@type": "Person",
        "email": `mailto:${data.email}`,
        "jobTitle": data.jobTitle,
        "name": data.name,
        "telephone": data.phone
      }
    ]
console.log({struc})
  return struc;
}

Thanks a lot Salman, what you have add looks pretty clear to me.

One last question, is where I put my dynamic data…

I mean this :

let data =  $w('#dynamicDataset').getCurrentData();

Where do I put my data in order to be able to get it with this line ?

Oh wait… I think i got it…

in your code :

let struc = [ 
      {
        "@context": "http://schema.org",
        "@type": "Organization",
        "name": data.title,
        "url": data.url
      },
      {
        "@context": "http://schema.org",
        "@type": "Person",
        "email": `mailto:${data.email}`,
        "jobTitle": data.jobTitle,
        "name": data.name,
        "telephone": data.phone

I have to make a collection with those fields “@context”, “@type”, “name”, etc…
Then, the line above is used to get the information in those fields ?

@oscar-aveline you will need to have this fields in the database
title, url, email, jobTitle, name, phone

And it will return as structure data as below

[
{
@context”: “http://schema.org”,
@type”: “Organization”,
“name”: “Salman”,
“url”: “https://url-here.com
},
{
@context”: “http://schema.org”,
@type”: “Person”,
“email”: “mailto:email@email.com”,
“jobTitle”: “job title”,
“name”: “Salman” ,
“telephone”: “1209891237”
}
]
note:
you don’t need to create a column for @context or @title

@salman-hammed Absolutly awesome ! Thanks a lot !
If this was a film, you sure be credited !

@oscar-aveline haha thanks buddy. Feel free to tag me in future post so I can help :blush:

One last question now is about the “” around the data in the schema.

I see there are no “” around your code :

"name": data.title,
"url": data.url

For the code to look like yours :

"name": "Salman",
"url":  "https://url-here.com"

Do I need to put the “” in my database ?
Or maybe it will automatically do it…

As I know Google is very picky on its structured data… I prefer to ask :slight_smile:

Again, a very big thank you for this ! It is a real pleasure to find people ready to help as you are !

You don’t need to put the “” as it will indicates a string
and the output data should show in console

just so you can verify it laster
this code in the “getStructure” function > console.log(struc)
Which show the modified struc data in the console

Happy to help!

Ok I see, thanks again.

I have one last question that is more about the “arrangement” of all this.

I have a MainDataset. It is used to build the dynamic page it is on.

Now with your magic code, I can add this code to the page and the fileds required in my MainDataset. That said, I have multiple fields (for local business) and I don’t find the idea of having all the fields in the Main Dataset.

With that idea, I can create a second Dataset called JsonDataset on the page, which will contain the fields and data I need.

In the code, I think I just need to change

$w.onReady(()=>{
$w(‘# MainDataset’).onReady(()=>{
// get the current page “data”
let data = $w('# JsonDataset ').getCurrentData();
wixSeo.setStructuredData(getStructure(data))
});
});

My big concerned is about the repeated data. In my MainDataset, I already have the phone, adresse, url, etc…
How can I referenced the MainDataset fields in my JsonDataset in order to be sure that if I make a change in my MainDataset, the structured data will updated the right way ?

I am used to reference fileds in repeater, to display informations from multiple database in an object in the editor, but I am not sure I can achieve this by code.

Good thing is I am pretty confident it will work fine even if everything is in the MainDataset, so even if it may look less organized, I will begin with this solution and will keep searching a way to reference fields later.

Thanks again

Good catch on the database error. I do apologize for the problems it  caused. The previous versions used INFORMATION_PAGES_SEO_XREF_USE_UL'  but it was changed to INFORMATION_PAGES_SEO_FOOTER_XREF_USE_UL' to allow  handling the bod id's easier. And I see now that IO forgot to mention  that as part of the update.