How to get meta tag content values and average them [closed] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!jQuery get specific option tag textHow do you get a timestamp in JavaScript?How to get the children of the $(this) selector?How can I get query string values in JavaScript?How to get the value from the GET parameters?Get selected value in dropdown list using JavaScript?How do I get the current date in JavaScript?<meta charset=“utf-8”> vs <meta http-equiv=“Content-Type”>Proper format for Google Schema?Offers in Products (schema.org / rich snippets)
How come Sam didn't become Lord of Horn Hill?
What initially awakened the Balrog?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
Significance of Cersei's obsession with elephants?
Performance gap between vector<bool> and array
How does light 'choose' between wave and particle behaviour?
Take 2! Is this homebrew Lady of Pain warlock patron balanced?
Is there hard evidence that the grant peer review system performs significantly better than random?
What is a fractional matching?
Illegal assignment from sObject to Id
What is "gratricide"?
What do you call the main part of a joke?
Is grep documentation about ignoring case wrong, since it doesn't ignore case in filenames?
Using audio cues to encourage good posture
How to install press fit bottom bracket into new frame
What is the difference between globalisation and imperialism?
How to compare two different files line by line in unix?
What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?
Why do we bend a book to keep it straight?
Why do we need to use the builder design pattern when we can do the same thing with setters?
Did Deadpool rescue all of the X-Force?
Generate an RGB colour grid
Why is it faster to reheat something than it is to cook it?
Is there any word for a place full of confusion?
How to get meta tag content values and average them [closed]
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!jQuery get specific option tag textHow do you get a timestamp in JavaScript?How to get the children of the $(this) selector?How can I get query string values in JavaScript?How to get the value from the GET parameters?Get selected value in dropdown list using JavaScript?How do I get the current date in JavaScript?<meta charset=“utf-8”> vs <meta http-equiv=“Content-Type”>Proper format for Google Schema?Offers in Products (schema.org / rich snippets)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm working on a website that uses Schema.org microdata for structured content product listings and apparently Google requires an aggregate rating of all the reviews. How can I grab all of the <meta itemprop="ratingValue" content="#" />
tags and then average them into a variable I can output to:
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="average #" />
<meta itemprop="reviewCount" content="# reviews" />
</div>
using JQuery or JavaScript? (Where # = the rating value)
javascript html5 meta-tags schema.org structured-data
closed as too broad by Quentin, SherylHohman, Dale Burrell, greg-449, Mark Rotteveel Mar 10 at 10:33
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I'm working on a website that uses Schema.org microdata for structured content product listings and apparently Google requires an aggregate rating of all the reviews. How can I grab all of the <meta itemprop="ratingValue" content="#" />
tags and then average them into a variable I can output to:
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="average #" />
<meta itemprop="reviewCount" content="# reviews" />
</div>
using JQuery or JavaScript? (Where # = the rating value)
javascript html5 meta-tags schema.org structured-data
closed as too broad by Quentin, SherylHohman, Dale Burrell, greg-449, Mark Rotteveel Mar 10 at 10:33
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I'm working on a website that uses Schema.org microdata for structured content product listings and apparently Google requires an aggregate rating of all the reviews. How can I grab all of the <meta itemprop="ratingValue" content="#" />
tags and then average them into a variable I can output to:
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="average #" />
<meta itemprop="reviewCount" content="# reviews" />
</div>
using JQuery or JavaScript? (Where # = the rating value)
javascript html5 meta-tags schema.org structured-data
I'm working on a website that uses Schema.org microdata for structured content product listings and apparently Google requires an aggregate rating of all the reviews. How can I grab all of the <meta itemprop="ratingValue" content="#" />
tags and then average them into a variable I can output to:
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="average #" />
<meta itemprop="reviewCount" content="# reviews" />
</div>
using JQuery or JavaScript? (Where # = the rating value)
javascript html5 meta-tags schema.org structured-data
javascript html5 meta-tags schema.org structured-data
edited Mar 9 at 11:28
Md Johirul Islam
3,32221542
3,32221542
asked Mar 8 at 20:24
KevinKevin
62
62
closed as too broad by Quentin, SherylHohman, Dale Burrell, greg-449, Mark Rotteveel Mar 10 at 10:33
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Quentin, SherylHohman, Dale Burrell, greg-449, Mark Rotteveel Mar 10 at 10:33
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Yes, this is not too difficult using jQuery. This code will grab all the meta contents, convert them to integers, find the average rating, and then append some HTML
at the bottom of your head
element.
// first gather all the meta elements with an itemprop value of "ratingValue"
var metas = $('meta[itemprop="ratingValue"]').get();
// convert the content values of these elements to integers and put them in an array
var ratings = metas.map((m) => ~~m.content);
// calculate and round the average rating value
var average = ~~(ratings.reduce((a,b) => a + b) / ratings.length + 0.5);
// create the HTML for the aggregateRating parent div
var aggregateRating = '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
// create ratingValue meta HTML using the average rating
var ratingValue = '<meta itemprop="ratingValue" content="average ' + average + '" />';
// create aggregateRating meta HTML using the rating count
var reviewCount = '<meta itemprop="reviewCount" content="' + ratings.length + ' reviews" />';
// combine these strings and a closing tag, then append the HTML to the end of head
$('head').append(aggregateRating + ratingValue + reviewCount + '</div>');
or you could even use the Bernard method
$('head').append(['<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">', '</div>'].join([
['ratingValue', 'average ' + ~~((r = $('meta[itemprop="ratingValue"]').get().map((m) => ~~m.content)).reduce((a, b) => a + b) / r.length + .5)],
['reviewCount', r.length + ' reviews']
].map((a, b) => ['<meta itemprop="', '" content="', '">'].map((c, d) => [c, a[d]]))).replace(/,/g, ''));
1
Thank you Omikes! Your code was very clear and I really appreciate the commented lines. I modified the jQuery to select a specific div id I needed to inject into and append there. Also removed the words "average" and "reviews" - that was partially my fault for including that inside the code example; I just wanted to be clear about what I was asking for, but only needed an integer there. :P
– Kevin
Mar 11 at 23:39
oops, haha. yeah i guess that makes more sense!
– omikes
Mar 12 at 0:05
add a comment |
var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
var scores = 0;
var n = 0;
for (var i = 0; i < aggregates.length; i++)
scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
scores += scoreCurr;
n += nCurr;
alert(scores/n);
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="35" />
<meta itemprop="reviewCount" content="7" />
</div>
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="42" />
<meta itemprop="reviewCount" content="10" />
</div>
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, this is not too difficult using jQuery. This code will grab all the meta contents, convert them to integers, find the average rating, and then append some HTML
at the bottom of your head
element.
// first gather all the meta elements with an itemprop value of "ratingValue"
var metas = $('meta[itemprop="ratingValue"]').get();
// convert the content values of these elements to integers and put them in an array
var ratings = metas.map((m) => ~~m.content);
// calculate and round the average rating value
var average = ~~(ratings.reduce((a,b) => a + b) / ratings.length + 0.5);
// create the HTML for the aggregateRating parent div
var aggregateRating = '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
// create ratingValue meta HTML using the average rating
var ratingValue = '<meta itemprop="ratingValue" content="average ' + average + '" />';
// create aggregateRating meta HTML using the rating count
var reviewCount = '<meta itemprop="reviewCount" content="' + ratings.length + ' reviews" />';
// combine these strings and a closing tag, then append the HTML to the end of head
$('head').append(aggregateRating + ratingValue + reviewCount + '</div>');
or you could even use the Bernard method
$('head').append(['<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">', '</div>'].join([
['ratingValue', 'average ' + ~~((r = $('meta[itemprop="ratingValue"]').get().map((m) => ~~m.content)).reduce((a, b) => a + b) / r.length + .5)],
['reviewCount', r.length + ' reviews']
].map((a, b) => ['<meta itemprop="', '" content="', '">'].map((c, d) => [c, a[d]]))).replace(/,/g, ''));
1
Thank you Omikes! Your code was very clear and I really appreciate the commented lines. I modified the jQuery to select a specific div id I needed to inject into and append there. Also removed the words "average" and "reviews" - that was partially my fault for including that inside the code example; I just wanted to be clear about what I was asking for, but only needed an integer there. :P
– Kevin
Mar 11 at 23:39
oops, haha. yeah i guess that makes more sense!
– omikes
Mar 12 at 0:05
add a comment |
Yes, this is not too difficult using jQuery. This code will grab all the meta contents, convert them to integers, find the average rating, and then append some HTML
at the bottom of your head
element.
// first gather all the meta elements with an itemprop value of "ratingValue"
var metas = $('meta[itemprop="ratingValue"]').get();
// convert the content values of these elements to integers and put them in an array
var ratings = metas.map((m) => ~~m.content);
// calculate and round the average rating value
var average = ~~(ratings.reduce((a,b) => a + b) / ratings.length + 0.5);
// create the HTML for the aggregateRating parent div
var aggregateRating = '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
// create ratingValue meta HTML using the average rating
var ratingValue = '<meta itemprop="ratingValue" content="average ' + average + '" />';
// create aggregateRating meta HTML using the rating count
var reviewCount = '<meta itemprop="reviewCount" content="' + ratings.length + ' reviews" />';
// combine these strings and a closing tag, then append the HTML to the end of head
$('head').append(aggregateRating + ratingValue + reviewCount + '</div>');
or you could even use the Bernard method
$('head').append(['<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">', '</div>'].join([
['ratingValue', 'average ' + ~~((r = $('meta[itemprop="ratingValue"]').get().map((m) => ~~m.content)).reduce((a, b) => a + b) / r.length + .5)],
['reviewCount', r.length + ' reviews']
].map((a, b) => ['<meta itemprop="', '" content="', '">'].map((c, d) => [c, a[d]]))).replace(/,/g, ''));
1
Thank you Omikes! Your code was very clear and I really appreciate the commented lines. I modified the jQuery to select a specific div id I needed to inject into and append there. Also removed the words "average" and "reviews" - that was partially my fault for including that inside the code example; I just wanted to be clear about what I was asking for, but only needed an integer there. :P
– Kevin
Mar 11 at 23:39
oops, haha. yeah i guess that makes more sense!
– omikes
Mar 12 at 0:05
add a comment |
Yes, this is not too difficult using jQuery. This code will grab all the meta contents, convert them to integers, find the average rating, and then append some HTML
at the bottom of your head
element.
// first gather all the meta elements with an itemprop value of "ratingValue"
var metas = $('meta[itemprop="ratingValue"]').get();
// convert the content values of these elements to integers and put them in an array
var ratings = metas.map((m) => ~~m.content);
// calculate and round the average rating value
var average = ~~(ratings.reduce((a,b) => a + b) / ratings.length + 0.5);
// create the HTML for the aggregateRating parent div
var aggregateRating = '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
// create ratingValue meta HTML using the average rating
var ratingValue = '<meta itemprop="ratingValue" content="average ' + average + '" />';
// create aggregateRating meta HTML using the rating count
var reviewCount = '<meta itemprop="reviewCount" content="' + ratings.length + ' reviews" />';
// combine these strings and a closing tag, then append the HTML to the end of head
$('head').append(aggregateRating + ratingValue + reviewCount + '</div>');
or you could even use the Bernard method
$('head').append(['<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">', '</div>'].join([
['ratingValue', 'average ' + ~~((r = $('meta[itemprop="ratingValue"]').get().map((m) => ~~m.content)).reduce((a, b) => a + b) / r.length + .5)],
['reviewCount', r.length + ' reviews']
].map((a, b) => ['<meta itemprop="', '" content="', '">'].map((c, d) => [c, a[d]]))).replace(/,/g, ''));
Yes, this is not too difficult using jQuery. This code will grab all the meta contents, convert them to integers, find the average rating, and then append some HTML
at the bottom of your head
element.
// first gather all the meta elements with an itemprop value of "ratingValue"
var metas = $('meta[itemprop="ratingValue"]').get();
// convert the content values of these elements to integers and put them in an array
var ratings = metas.map((m) => ~~m.content);
// calculate and round the average rating value
var average = ~~(ratings.reduce((a,b) => a + b) / ratings.length + 0.5);
// create the HTML for the aggregateRating parent div
var aggregateRating = '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
// create ratingValue meta HTML using the average rating
var ratingValue = '<meta itemprop="ratingValue" content="average ' + average + '" />';
// create aggregateRating meta HTML using the rating count
var reviewCount = '<meta itemprop="reviewCount" content="' + ratings.length + ' reviews" />';
// combine these strings and a closing tag, then append the HTML to the end of head
$('head').append(aggregateRating + ratingValue + reviewCount + '</div>');
or you could even use the Bernard method
$('head').append(['<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">', '</div>'].join([
['ratingValue', 'average ' + ~~((r = $('meta[itemprop="ratingValue"]').get().map((m) => ~~m.content)).reduce((a, b) => a + b) / r.length + .5)],
['reviewCount', r.length + ' reviews']
].map((a, b) => ['<meta itemprop="', '" content="', '">'].map((c, d) => [c, a[d]]))).replace(/,/g, ''));
edited Mar 10 at 0:30
answered Mar 8 at 22:32
omikesomikes
3,71682442
3,71682442
1
Thank you Omikes! Your code was very clear and I really appreciate the commented lines. I modified the jQuery to select a specific div id I needed to inject into and append there. Also removed the words "average" and "reviews" - that was partially my fault for including that inside the code example; I just wanted to be clear about what I was asking for, but only needed an integer there. :P
– Kevin
Mar 11 at 23:39
oops, haha. yeah i guess that makes more sense!
– omikes
Mar 12 at 0:05
add a comment |
1
Thank you Omikes! Your code was very clear and I really appreciate the commented lines. I modified the jQuery to select a specific div id I needed to inject into and append there. Also removed the words "average" and "reviews" - that was partially my fault for including that inside the code example; I just wanted to be clear about what I was asking for, but only needed an integer there. :P
– Kevin
Mar 11 at 23:39
oops, haha. yeah i guess that makes more sense!
– omikes
Mar 12 at 0:05
1
1
Thank you Omikes! Your code was very clear and I really appreciate the commented lines. I modified the jQuery to select a specific div id I needed to inject into and append there. Also removed the words "average" and "reviews" - that was partially my fault for including that inside the code example; I just wanted to be clear about what I was asking for, but only needed an integer there. :P
– Kevin
Mar 11 at 23:39
Thank you Omikes! Your code was very clear and I really appreciate the commented lines. I modified the jQuery to select a specific div id I needed to inject into and append there. Also removed the words "average" and "reviews" - that was partially my fault for including that inside the code example; I just wanted to be clear about what I was asking for, but only needed an integer there. :P
– Kevin
Mar 11 at 23:39
oops, haha. yeah i guess that makes more sense!
– omikes
Mar 12 at 0:05
oops, haha. yeah i guess that makes more sense!
– omikes
Mar 12 at 0:05
add a comment |
var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
var scores = 0;
var n = 0;
for (var i = 0; i < aggregates.length; i++)
scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
scores += scoreCurr;
n += nCurr;
alert(scores/n);
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="35" />
<meta itemprop="reviewCount" content="7" />
</div>
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="42" />
<meta itemprop="reviewCount" content="10" />
</div>
add a comment |
var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
var scores = 0;
var n = 0;
for (var i = 0; i < aggregates.length; i++)
scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
scores += scoreCurr;
n += nCurr;
alert(scores/n);
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="35" />
<meta itemprop="reviewCount" content="7" />
</div>
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="42" />
<meta itemprop="reviewCount" content="10" />
</div>
add a comment |
var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
var scores = 0;
var n = 0;
for (var i = 0; i < aggregates.length; i++)
scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
scores += scoreCurr;
n += nCurr;
alert(scores/n);
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="35" />
<meta itemprop="reviewCount" content="7" />
</div>
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="42" />
<meta itemprop="reviewCount" content="10" />
</div>
var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
var scores = 0;
var n = 0;
for (var i = 0; i < aggregates.length; i++)
scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
scores += scoreCurr;
n += nCurr;
alert(scores/n);
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="35" />
<meta itemprop="reviewCount" content="7" />
</div>
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="42" />
<meta itemprop="reviewCount" content="10" />
</div>
var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
var scores = 0;
var n = 0;
for (var i = 0; i < aggregates.length; i++)
scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
scores += scoreCurr;
n += nCurr;
alert(scores/n);
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="35" />
<meta itemprop="reviewCount" content="7" />
</div>
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="42" />
<meta itemprop="reviewCount" content="10" />
</div>
var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
var scores = 0;
var n = 0;
for (var i = 0; i < aggregates.length; i++)
scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
scores += scoreCurr;
n += nCurr;
alert(scores/n);
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="35" />
<meta itemprop="reviewCount" content="7" />
</div>
<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="42" />
<meta itemprop="reviewCount" content="10" />
</div>
answered Mar 9 at 12:22
morimori
996
996
add a comment |
add a comment |