I tried to add the brand tag for structured data in all products pages (e.g. this page https://info512261.wixsite.com/gastone/product-page/Generatore-di-corrente-inverter-I65 )
My code is base on this reference https://www.wix.com/velo/reference/wix-seo/structureddata
This is my code:
import wixLocation from 'wix-location';
import wixSeo from 'wix-seo';
$w.onReady(async function () {
initBrand();
wixLocation.onChange(async (location) => {
initBrand();
})
})
async function initBrand() {
let structuredData = wixSeo.structuredData;
structuredData["brand"]="Black+Decker";
wixSeo.setStructuredData(structuredData);
}
If i try to check the structured data with Google tools ( https://search.google.com/test/rich-results?utm_campaign=sdtt&utm_medium=url&url=https://info512261.wixsite.com/gastone/product-page/Generatore-di-corrente-inverter-I65 ) i cant see the brand tag that i have added.
The code not works for store product pages? Can someone help me?
ty.
The returned structured data is an array of objects, so you most likely need to specify on which entry you want to change the brand tag:
Something like this:
structuredData[1]["brand"] = "Black+Decker";
In order to know what the structured data looks like, you should add console.log(structuredData) to inspect the returned object:
async function initBrand(){
let structuredData = wixSeo.structuredData;
console.log('structuredData); // inspect structuredData
// change structuredData here
await wixSeo.setStructuredData(structuredData);
}
I also noticed that you have a number of errors in your code:
It is incorrect to have more than one onReady() function. You should delete the first empty one.
You are calling an initReview() function that does not exist, and you are not calling initBrand().
Hello,
i have a similar problem, with this code the console give to me this error:
Someone can help me?
Cannot read property ‘aggregateRating’ of undefined (oin this line singoleRecensioni [ 0 ][ “aggregateRating” ][ “@type” ]= “AggregateRating” ; )
import wixLocation from 'wix-location';
import wixSeo from 'wix-seo';
import wixData from 'wix-data';
$w.onReady(async function () {
initBrand();
wixLocation.onChange(async (location) => {
initBrand();
})
})
async function initBrand() {
let structuredData = wixSeo.structuredData;
console.log(structuredData); // inspect structuredData
let singoleRecensioni=[];
let mediaVoti=0;
let totaleVoti=0;
wixData.query("Recensioni")
.eq("lingua", "IT")
.eq("prodotto", "Omega 15")
.find()
.then( (results) => {
totaleVoti=results.items.length;
let somma=0;
for (let i=0; i<totaleVoti; i++ ){
singoleRecensioni[i+1]["@type"]="Review";
singoleRecensioni[i+1]["name"]=results.items[i].titolo;
singoleRecensioni[i+1]["reviewBody"]=results.items[i].descrizione;
singoleRecensioni[i+1]["reviewRating"]["@type"]="Rating";
singoleRecensioni[i+1]["reviewRating"]["ratingValue"]=results.items[i].valutazione;
singoleRecensioni[i+1]["datePublished"]=results.items[i].data.getFullYear()+"-"+results.items[i].data.getMonth()+"-"+results.items[i].data.getDay();
singoleRecensioni[i+1]["author"]["@type"]="Person";
singoleRecensioni[i+1]["author"]["name"]=results.items[i].autore;
singoleRecensioni[i+1]["publisher"]["@type"]="Organization";
singoleRecensioni[i+1]["publisher"]["name"]="Omega Stations";
somma+=results.items[i].valutazione;
}
mediaVoti=somma/totaleVoti;
console.log(singoleRecensioni);
} )
.catch( (err) => {
let errorMsg = err;
} );
singoleRecensioni[0]["aggregateRating"]["@type"]="AggregateRating";
singoleRecensioni[0]["aggregateRating"]["bestRating"]=5;
singoleRecensioni[0]["aggregateRating"]["worstRating"]=1;
singoleRecensioni[0]["aggregateRating"]["ratingCount"]=totaleVoti;
singoleRecensioni[0]["aggregateRating"]["ratingValue"]=mediaVoti;
structuredData[0] +=singoleRecensioni;
console.log(structuredData); // inspect structuredData
await wixSeo.setStructuredData(structuredData);
}
@info23579 The array singoleRecensioni is undefined since the code above in the query’s .then() function hasn’t run yet. You should move those lines of code into the .then() function after the for loop where you build the singoleRecensioni array.
The .then() function is run after the query’s Promise is fulfilled, that is, when the query returns its results.
See the following for more information about Promises:
I edited the code but not works. There are no errors, but not edit the JSON.
I try to debug the code with 3 console.log but only the first print the value.
Why? Thank you .
This is the new code:
// API Reference: https://www.wix.com/velo/reference/api-overview/introduction
// “Hello, World!” Example: https://learn-code.wix.com/en/article/1-hello-world
import wixLocation from ‘wix-location’ ;
import wixSeo from ‘wix-seo’ ;
import wixData from ‘wix-data’ ;
$w . onReady ( async function () {
initBrand ();
wixLocation . onChange ( async ( location ) => {
initBrand ();
})
})
async function initBrand () {
let structuredData = wixSeo . structuredData ;
console . log ( structuredData ); // inspect structuredData
let singoleRecensioni =;
let mediaVoti = 0 ;
let totaleVoti = 0 ;
wixData . query ( “Recensioni” )
. eq ( “lingua” , “IT” )
. eq ( “prodotto” , “Omega 15” )
. find ()
. then ( ( results ) => {
totaleVoti = results . items . length ; console . log ( totaleVoti ); //print the correct number
let somma = 0 ;
for ( let i = 0 ; i < totaleVoti ; i ++ ){
singoleRecensioni [ i + 1 ][ “@type” ]= “Review” ;
singoleRecensioni [ i + 1 ][ “name” ]= results . items [ i ]. titolo ;
singoleRecensioni [ i + 1 ][ “reviewBody” ]= results . items [ i ]. descrizione ;
singoleRecensioni [ i + 1 ][ “reviewRating” ][ “@type” ]= “Rating” ;
singoleRecensioni [ i + 1 ][ “reviewRating” ][ “ratingValue” ]= results . items [ i ]. valutazione ;
singoleRecensioni [ i + 1 ][ “datePublished” ]= results . items [ i ]. data . getFullYear ()+ “-” + results . items [ i ]. data . getMonth ()+ “-” + results . items [ i ]. data . getDay ();
singoleRecensioni [ i + 1 ][ “author” ][ “@type” ]= “Person” ;
singoleRecensioni [ i + 1 ][ “author” ][ “name” ]= results . items [ i ]. autore ;
singoleRecensioni [ i + 1 ][ “publisher” ][ “@type” ]= “Organization” ;
singoleRecensioni [ i + 1 ][ “publisher” ][ “name” ]= “Omega Stations” ;
somma += results . items [ i ]. valutazione ; console . log ( i ); //print anyting
}
mediaVoti = somma / totaleVoti ;
console . log ( singoleRecensioni );
singoleRecensioni [ 0 ][ “aggregateRating” ][ “@type” ]= “AggregateRating” ;
singoleRecensioni [ 0 ][ “aggregateRating” ][ “bestRating” ]= 5 ;
singoleRecensioni [ 0 ][ “aggregateRating” ][ “worstRating” ]= 1 ;
singoleRecensioni [ 0 ][ “aggregateRating” ][ “ratingCount” ]= totaleVoti ;
singoleRecensioni [ 0 ][ “aggregateRating” ][ “ratingValue” ]= mediaVoti ; console . log ( singoleRecensioni );
structuredData [ 0 ] += singoleRecensioni ;
console . log ( structuredData ); // inspect structuredData //print anyting
wixSeo . setStructuredData ( structuredData );
} )
. catch ( ( err ) => {
let errorMsg = err ;
} );
}
@yisrael-wix can you help me?
i try to do this:
async function initBrand() {
let singoleRecensioni=[];
let mediaVoti=0;
let totaleVoti=0;
singoleRecensioni.push( {
"@context": "https://schema.org/",
"@type": "Product",
"name": "Cocktail Station portatile 150cm",
"image": "https://files.omegastations.com/product/omega15-infopage.jpg",
"description": "Niente compromessi. Pensata per gli hotel e i catering di livello, per chi vuole deliziare i suoi clienti con l’arte dello slow cocktail all’esterno del proprio locale, per chi vuole un bar mobile intorno al mixologist. Creata dai bartender per i bartender, è la postazione barman completa di tutto. Omega 15 ha tutto lo spazio che ti serve per dare sfogo alla tua arte, pensata per i professionisti del bartending che vogliono ogni cosa al posto giusto",
"sku": "O040000",
"offers": {
"@type": "Offer",
"url": "http://www.omegastations.com/omega-15-bar-mobile-150-cm",
"priceCurrency": "EUR",
"price": "1827",
"availability": "https://schema.org/InStock",
"itemCondition": "https://schema.org/NewCondition"
}
});
wixData.query("Recensioni")
.eq("lingua", "IT")
.eq("prodotto", "Omega 15")
.find()
.then( (results) => {
totaleVoti=results.items.length;
console.log("stampa 1: "+totaleVoti); //print the correct number
let somma=0;
for (let i=0; i<totaleVoti; i++ ){
//console.log("stampa 1.1");
singoleRecensioni.push({
"@type":"Review",
"name":results.items[i].titolo,
"reviewBody":results.items[i].descrizione,
"reviewRating":{
"@type":"Rating",
"ratingValue":results.items[i].valutazione
},
"datePublished":results.items[i].data.getFullYear()+"-"+results.items[i].data.getMonth()+"-"+results.items[i].data.getDay(),
"author":{
"@type":"Person",
"name":results.items[i].autore
},
"publisher":{
"@type":"Organization",
"name":"Omega Stations"
}
});
somma+=results.items[i].valutazione;
}
mediaVoti=somma/totaleVoti;
console.log("log all reviews array: ");
console.log(singoleRecensioni);
let newStructuredData = wixSeo.structuredData;
console.log("log page original structured data:");
console.log(newStructuredData);
newStructuredData.push(singoleRecensioni);
wixSeo.setStructuredData(newStructuredData);
console.log("log new structured data");
console.log(wixSeo.structuredData);
} )
.catch( (err) => {
let errorMsg = err;
} );
}
But checking the page (https://www.omegastations.com/omega-15-bar-mobile-150-cm) with the google tool doensn’t find my new JSON why?
https://search.google.com/test/rich-results?id=GrWVnm4mIzgKkXs9KhVf6g
Note that setStructuredData() returns a Promise that needs to be handled as is shown in the sample code snippet.
Perhaps someone here on the forum has experience working with the Google Rich Results Test tool and can help.