-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
81 lines (68 loc) · 2.79 KB
/
test.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<link rel='canonical' href='https://www.crossrope.com/products/infinity-rope-system/get-strong-jump-rope-set' />
<script>
// url to get the yotpo review info
// var yotpo_review_link = 'https://api.yotpo.com/v1/widget/4EWWlhD7P1plMKwTP9oihVAQ5YUJD5gswjJ6ApPG/products/173/reviews.json';
var yotpo_review_link = 'https://api.yotpo.com/v1/widget/4EWWlhD7P1plMKwTP9oihVAQ5YUJD5gswjJ6ApPG/products/{{product.id}}/reviews.json';
// work out the products url
var url = window.location.href;
var canonicalTag = document.querySelector("link[rel='canonical']");
if (canonicalTag) {
url = canonicalTag.getAttribute("href");
}
// create basic json-ld for the product
var jsonLd = {
"@context": "http://schema.org/",
"@type": "Product",
"@id": url+"#Product",
"review": []
};
// make the request for the review info
const request = new XMLHttpRequest();
request.open("GET", yotpo_review_link);
request.responseType = 'json';
request.onload = function() {
if (request.status === 200) {
// good response, process it
var json = request.response;
// add totals
jsonLd.aggregateRating = {
"@type": "AggregateRating",
"worstRating": "1",
"bestRating": "5",
"ratingValue": json.response.bottomline.average_score,
"reviewCount": json.response.bottomline.total_review
};
// add reviews
json.response.reviews.forEach(function(review, index) {
jsonLd.review.push({
"@type": "Review",
"author": {
"@type": "Person",
"name": review.user.display_name
},
"reviewRating": {
"@type": "Rating",
"ratingValue": review.score
},
"name": review.title,
"reviewBody": review.content,
"datePublished": review.created_at
});
});
// add json-ld to the page
var aggregateRatingScriptElement = document.createElement('script');
aggregateRatingScriptElement.type = 'application/ld+json';
aggregateRatingScriptElement.setAttribute("id", "wsa-yotpo-json-ld");
var inlineScript = document.createTextNode(JSON.stringify(jsonLd));
aggregateRatingScriptElement.appendChild(inlineScript);
document.querySelector('head').appendChild(aggregateRatingScriptElement);
}
};
request.send();
</script>
</head>
<body>Hello</body>
</html>