-
Notifications
You must be signed in to change notification settings - Fork 3
/
seo-rich-snippets-yotpo-api.html
125 lines (107 loc) · 4.33 KB
/
seo-rich-snippets-yotpo-api.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<script data-cfasync="false">
/*
Web Site Advantage: Yotpo via API [v1.1]
Gathers Yotpo reviews via their API and creates structured data for th SEO Rich Snippets app
https://bigcommerce.websiteadvantage.com.au/
Copyright (C) 2023 Web Site Advantage
*/
{{#if page_type '===' 'product' }}
!function(w,d){
var yotpoAppKey = ''; // if left blank the code will attempt to discover the app key from the Yotpo widget
var productId = {{{json product.id}}};
var productUrl = {{{json product.url}}};
// let someone specify the key from outside the script
if (w.wsa_yotpoAppKey && !yotpoAppKey) {
yotpoAppKey = w.wsa_yotpoAppKey;
}
function getYotopAppKey() {
// V3 script
var scriptElement = document.querySelector('script[src*="yotpo.com/v1/loader/"]');
if (scriptElement) {
var src = scriptElement.getAttribute('src');
return src.split('/').pop();
}
// older script
var scripts = document.getElementsByTagName('script');
var pattern = /\/\/staticw2\.yotpo\.com\/(.*?)\/widget\.js/g;
for (var i = 0; i < scripts.length; i++) {
var scriptContent = scripts[i].textContent || scripts[i].innerText;
var match;
while ((match = pattern.exec(scriptContent)) !== null) {
var key = match[1];
return key;
}
}
}
function getReviews() {
var apiUrl = 'https://api-cdn.yotpo.com/v3/storefront/store/' + yotpoAppKey + '/product/' + productId +'/reviews?page=1&perPage=5&sort=date,images,badge,rating';
fetch(apiUrl, {
method: 'GET'
})
.then(function (response) {
return response.json();
})
.then(function (result) {
if (result.reviews.length > 0) {
let jsonLd = {
"@context": "https://schema.org/",
"@type": "Product",
"@id": productUrl + "#Product" + w.wsa_jsonLdIdPostfix, // add postfix
"aggregateRating": {
"@type": "AggregateRating",
"worstRating": "1",
"bestRating": "5",
"ratingValue": result.bottomline.averageScore,
"reviewCount": result.bottomline.totalReview
},
"review": []
};
for (var i = 0; i < result.reviews.length; i++) {
var review = result.reviews[i];
jsonLd.review.push({
"@type": "Review",
"author": {
"@type": "Person",
"name": review.user.displayName
},
"reviewRating": {
"@type": "Rating",
"ratingValue": review.score
},
"name": review.title,
"reviewBody": review.content,
"datePublished": review.createdAt
});
}
console.log('jsonLd', jsonLd);
if (!webSiteAdvantage.excludeProduct) {
let aggregateRatingScriptElement = d.createElement('script');
aggregateRatingScriptElement.type = 'application/ld+json';
aggregateRatingScriptElement.setAttribute("id", 'wsa-rich-snippets-jsonld-yotpo-product' + w.wsa_jsonLdIdPostfix.toLowerCase());
let inlineScript = d.createTextNode(JSON.stringify(jsonLd));
aggregateRatingScriptElement.appendChild(inlineScript);
d.querySelector('head').appendChild(aggregateRatingScriptElement);
}
}
});
}
if (yotpoAppKey) {
getReviews();
}
else {
yotpoAppKey = getYotopAppKey();
if (yotpoAppKey) {
getReviews();
}
else {
addEventListener('DOMContentLoaded', function() {
yotpoAppKey = getYotopAppKey();
if (yotpoAppKey) {
getReviews();
}
});
}
}
}(window, document)
{{/if}}
</script>