-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery_ProdReviewAPI_CustomTBDemo.html
54 lines (43 loc) · 1.96 KB
/
jQuery_ProdReviewAPI_CustomTBDemo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var bizUnitID = 'xxx'; // Your BIZUNIT HERE
var apiKey = 'xxx'; //Your Trustpilot API Key
var sku = getParameterByName('sku'); //Your Product SKU coming from URL request parameter e.g. http://ThisPageURL?sku=YOUR_SKU
var reqURL = "https://api.trustpilot.com/v1/product-reviews/business-units/"+bizUnitID+"/reviews";
$.ajax({url: reqURL+"?apikey="+apiKey+"&sku="+sku,
dataType: "json",
success: function(json) {
console.log("Overall Response Received: ", json.productReviews);
var prodR = json.productReviews;
for (var i = 0; i < prodR.length; i++) {
var html = 'Review Title: ' + prodR[i].content + '<br/>';
html += 'Review Rating: ' + prodR[i].stars + '<br/>';
html += 'Reviewer: ' + prodR[i].consumer.displayName + '<br/>';
html += 'Review Data: ' + prodR[i].createdAt + '<br/>';
$('#prodr').append(html); // TODO: Build any Custom Graphic for showing Product Reviews Data
}
}, error : function(json) {
console.log("CASE-1 ERROR: ######### " + json.responseText);
$.each(json, function(key, value) {
console.log("FAILURE");
console.log("CASE-0:: "+key +" : "+ value);
});
}
});
</script>
<div id="prodr">
<!-- Build any Custom Graphic here showing Product Reviews Data -->
</div>
</body>
</html>