Skip to content

Instantly share code, notes, and snippets.

@elmogallen
Created January 27, 2014 21:11
Show Gist options
  • Save elmogallen/8657389 to your computer and use it in GitHub Desktop.
Save elmogallen/8657389 to your computer and use it in GitHub Desktop.
How to determine whether a product is setup in the Bazaarvoice system, and then getting an accurate number of reviews minus those that are RatingsOnly. Asynchronously. You would see your product list first, then either the star ratings or "Read reviews", and then finally you might see "Read reviews" change to "Read 17 reviews" for example.
// Bazaarvoice Ratings and Reviews Div
liBody += "<div id='review-" + dealerId.toString() + "' style='margin-left:16px;'></div>";
// ...
// ... much later ...
// ...
// Bazaarvoice related configuration script
var participatingBVDealers = [];
// API METHOD: statistics -- the BV api url including all dealers we are showing on this page.
var url = "http://stg.api.bazaarvoice.com/data/statistics.json?apiversion=5.4&passkey={secret key}&filter=productid:" + dealerIds.join(',') + "&stats=Reviews";
// Get *all* products that match the productIds in the querystring
$.get(url, function( data ) {
// If none of these dealers exist in BV, TotalResults will be zero.
// If a dealer is setup in BV, but has no reviews yet, it will still be included appropriately in the count.
var totalResults = parseInt(data.TotalResults);
if (totalResults > 0) {
// Some dealers exist in Bazaarvoice
for (i = 0; i < data.Results.length; i++) {
// Each Results[index] object has miscellaneous data relating to the "product". Here, I'm grabbing its associated ProductId.
var productId = data.Results[i].ProductStatistics.ProductId;
// Note: the TotalReviewCount here is not quite accurate; it includes ratings as reviews too,
// so I'm going to get a more accurate one afterward. I'll just use this one to weed out the ones
// that have no reviews or ratings at all.
var numReviews = data.Results[i].ProductStatistics.ReviewStatistics.TotalReviewCount;
if (numReviews > 0) {
// Add to the array of participating BV dealers, which we'll use later to pass into the $BV.ui method as productIds
participatingBVDealers.push(productId); // Move this out of this if condition if you want to show "Write a review" when there are no reviews
// Fill the div's contents; NOTE: it would be better to *create* the div here and
// append it to the end of its container, but I had some trouble with it and the $BV.ui call. May investigate further.
var bvrrDiv = $('#review-' + productId);
bvrrDiv.append("<br class='clearboth'>");
bvrrDiv.append("<div id='BVRRInlineRating-" + productId + "'>");
bvrrDiv.append("<a class='bvReadReviews' data-productid='" + productId + "' href='' style='display:block;margin-top:4px;' onclick=\"openReviews('" + productId + "'); return false;\">Read reviews</a>");
}
}
// Now, let's make the number of reviews more accurate.
$("a.bvReadReviews").each(function (index, element) {
link = $(element);
// API METHOD: reviews -- Note the addition of &Filter=IsRatingsOnly:false to this querystring. That filter option is not available (or doesn't work) in the Statistics api call
var url = "http://stg.api.bazaarvoice.com/data/reviews.json?apiversion=5.4&passkey={secret key}&Filter=ProductId:" + link.attr("data-productid") + "&Filter=IsRatingsOnly:false&Limit=1";
// Get the specific product based on the productId specified in the querystring
$.get(url, function( data ) {
// Because we were able to use "IsRatingsOnly:false", it only returns those results that have actual reviews
var numReviews = data.TotalResults || 0;
link.text("Read " + numReviews + " reviews");
// You could check to see if the numReviews === 0 and change this to say "Write a review"
})
.fail(function( jqXHR, textStatus, errorThrown) {
// Getting a specific product based on the productId failed
console.log( textStatus + " " + errorThrown );
});
});
// Finally, call the javascript API that fills in the BVRRInlineRating-##### divs with the star ratings.
// At this point, participatingBVDealers only contains those dealers who are in the Bazaarvoice system.
$BV.ui('rr', 'inline_ratings', {
productIds: participatingBVDealers,
containerPrefix: 'BVRRInlineRating'
});
}
})
.fail(function( jqXHR, textStatus, errorThrown) {
// Getting all the products based on the list of productIds failed
console.log( textStatus + " " + errorThrown );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment