-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsa-yotpo-rich-snippets.js
345 lines (283 loc) · 13.5 KB
/
wsa-yotpo-rich-snippets.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Web Site Advantage: Adding Schema.org rating and review markup to Yotpo [v2.3]
https://websiteadvantage.com.au/Yotpo-Product-Rating-Review-Rich-Snippets
Copyright (C) 2016 Web Site Advantage
*/
var wsa_yotpoPlaceHolder = ".yotpo-main-widget";
var wsa_yotpoSdFormat = "json-ld"; // microdata, json-ld
// -HEADING-
// Place the minified version of this after the placeholder element for Yotpo has been created. e.g. in the footer.
// The reason is that this code has to listen for Yotpo adding its stuff to the placeholder. So it has to attach to the placeholder before Yotpo has a go at it.
// ***** To make it more accurate I suggest changing the display date format to YYYY-MM_DD in the Yotpo Widget General Settings.
// to minify: https://websiteadvantage.com.au/JavaScript-Compressor
// We had a case where the main widget element was added by JavaScript and getting results was intermitent. Seems the Yotpo widget sometimes failed to get added (Google Rendering)
// bacuse its code ran before the one that added the element.
// For BigCommerce Stencil place in the Scripts Manager inside script tag and {{#if page_type '===' 'product'}}...{{/if}} ?????
// Yotpo installation guide: https://support.yotpo.com/en/article/bigcommerce-stencil-installing-yotpo-on-stencil
var webSiteAdvantage = webSiteAdvantage || {};
webSiteAdvantage.yotpoRichSnippets = webSiteAdvantage.yotpoRichSnippets || [];
webSiteAdvantage.yotpoRichSnippets = function (yotpoPlaceHolder) {
this.initialise(yotpoPlaceHolder);
};
webSiteAdvantage.yotpoRichSnippets.prototype = {
// constants
yotpoPlaceHolder: wsa_yotpoPlaceHolder,
format: wsa_yotpoSdFormat, // microdata, json-ld
// call to enable the widget
constructor: webSiteAdvantage.yotpoRichSnippets,
initialise: function (yotpoPlaceHolder) {
// Has to be run after the placeholder is added to the dom, but before Yotpo runs
// try and find the placeholder if the constant does not work.
try {
if (typeof yotpoPlaceHolder !== 'undefined' && yotpoPlaceHolder != null)
this.yotpoPlaceHolder = yotpoPlaceHolder;
if (document.querySelector(this.yotpoPlaceHolder) == null) {
console.log('yotpoRichSnippets: Could not find yotpoPlaceHolder '+this.yotpoPlaceHolder);
this.yotpoPlaceHolder = '.yotpo.placeholder';
console.log('yotpoRichSnippets: Trying '+this.yotpoPlaceHolder);
};
this._yotpoPlaceHolderElement = document.querySelector(this.yotpoPlaceHolder);
if (this._yotpoPlaceHolderElement != null) {
var self = this; // so when observer function is called it can refer back to this object
this._observer = new MutationObserver(function( mutations ) {
mutations.forEach(function( mutation ) {
var newNodes = mutation.addedNodes; // DOM NodeList
if( newNodes !== null ) { // If there are new nodes added
for (i = 0; i < newNodes.length; i++) {
var node = newNodes[i];
if(typeof node.classList !== 'undefined' && node.classList.contains("yotpo-display-wrapper") && self._observer != null ) {
// once only event, so remove
self._observer.disconnect();
self._observer = null;
self._addMarkup();
}
}
}
});
});
this._observer.observe(this._yotpoPlaceHolderElement, this._observerConfig);
}
else {
console.log('yotpoRichSnippets: Could not find yotpoPlaceHolder '+this.yotpoPlaceHolder);
console.log('yotpoRichSnippets: Failed');
}
// also observe changes to the head section
this._observerHead = new MutationObserver(function( mutations ) {
mutations.forEach(function( mutation ) {
var newNodes = mutation.addedNodes; // DOM NodeList
if( newNodes !== null ) { // If there are new nodes added
for (i = 0; i < newNodes.length; i++) {
var node = newNodes[i];
// if( node.nodeName === "SCRIPT" && node.getAttribute('type') === 'application/ld+json' && !node.id.startsWith("wsa-")) { // way to remove all bout ours
if(node.nodeName === "SCRIPT" && node.getAttribute('type') === 'application/ld+json' && typeof node.classList !== 'undefined' && node.classList.contains("y-rich-snippet-script") ) {
node.parentNode.removeChild(node);
}
}
}
});
});
// not sure. There may ba a chance that the widget is present before we observe.
this._observerHead.observe(document.querySelector('head'), this._observerConfig);
} catch(err) {
console.log('yotpoRichSnippets: initialise Error: '+err.message); // not critical, as long as Googlebot does not cause it
}
},
// private variables and functions
_markupAdded: false,
_observerHead: null,
_observer: null,
_observerConfig: {
attributes: true,
childList: true,
characterData: true,
subtree: true
},
_yotpoPlaceHolderElement: null,
_getCount: function (rating) {
// This only works for the main widget.
var sElement = document.querySelector('.yotpo-distibutions-sum-reviews span[data-score-distribution="'+rating+'"]');
if (sElement != null) {
var s = sElement.textContent;
if (s.length >= 3)
return parseInt(s.substr(1, s.length-2)); // remove quotes around the text.
}
// console.log('yotpoRichSnippets: Failed to find count for rating '+rating+'. Using 0');
return 0;
},
_getReviewCount: function () {
// This only works for the main widget. Could potentially find cound from the smaller top widget as well.
var sElement = document.querySelector('.yotpo-reviews-nav-tab-sum');
if (sElement != null) {
var s = sElement.textContent;
if (s.length >= 3)
return parseInt(s.substr(1, s.length-2)); // remove quotes around the text.
}
console.log('yotpoRichSnippets: Failed to find reviewCount. Using 0');
return 0;
},
_addMarkup: function () {
if (this._markupAdded) return;
this._markupAdded = true;
try {
var stars5 = this._getCount(5);
var stars4 = this._getCount(4);
var stars3 = this._getCount(3);
var stars2 = this._getCount(2);
var stars1 = this._getCount(1);
var reviewCount = stars5+stars4+stars3+stars2+stars1;
var ratingValue = (stars5*5+stars4*4+stars3*3+stars2*2+stars1*1)/reviewCount;
if (reviewCount == 0 ) {
// try without the widget. gets to the nearest 0.5
reviewCount = this._getReviewCount();
// ISSUE: This searches the whole dom so could pick up multiple widgets of data
ratingValue = document.querySelectorAll('.yotpo-stars-and-sum-reviews .yotpo-stars .yotpo-icon-star').length + document.querySelectorAll('.yotpo-stars-and-sum-reviews .yotpo-stars .yotpo-icon-half-star').length/2;
}
// console.log('yotpoRichSnippets: Reviews '+reviewCount);
// console.log('yotpoRichSnippets: Rating '+ratingValue);
// document.querySelectorAll("head")[0].setAttribute("reviewCount", reviewCount);
// document.querySelectorAll("head")[0].setAttribute("ratingValue", ratingValue);
if (reviewCount > 0) {
var url = window.location.href;
var canonicalTag = document.querySelector("link[rel='canonical']");
if (canonicalTag) {
url = canonicalTag.getAttribute("href");
}
var jsonLd = {
"@context": "http://schema.org/",
"@type": "Product",
"@id": url+"#Product",
"review": []
};
if (this.format == "microdata") {
var aggregateRatingElement = document.querySelector('.yotpo-stars-and-sum-reviews');
if (aggregateRatingElement != null) {
aggregateRatingElement.setAttribute('id','yotpo-aggregate-rating');
aggregateRatingElement.setAttribute('itemprop','aggregateRating');
aggregateRatingElement.setAttribute('itemscope','');
aggregateRatingElement.setAttribute('itemtype','http://schema.org/AggregateRating');
}
else
console.log('yotpoRichSnippets: Failed to find aggregateRating Element');
this._addItemProps(aggregateRatingElement, '.based-on', 'reviewCount', reviewCount);
this._addItemProps(aggregateRatingElement, '.yotpo-stars-and-sum-reviews .yotpo-stars', 'ratingValue', ratingValue);
this._appendItemPropMetaTag(aggregateRatingElement, 'worstRating', '1');
this._appendItemPropMetaTag(aggregateRatingElement, 'bestRating', '5');
}
if (this.format == "json-ld") {
jsonLd.aggregateRating = {
"@type": "AggregateRating",
"worstRating": "1",
"bestRating": "5",
"ratingValue": ratingValue,
"reviewCount": reviewCount
};
}
var reviewElements = this._yotpoPlaceHolderElement.querySelectorAll('.yotpo-review');
if (reviewElements.length == 0)
console.log('yotpoRichSnippets: Failed to find any reviews');
for (r = 0; r < reviewElements.length; r++) {
var reviewElement = reviewElements[r];
if (!reviewElement.classList.contains( "yotpo-hidden" )) {
if (this.format == "microdata") {
reviewElement.setAttribute('itemprop','review');
reviewElement.setAttribute('itemscope','');
reviewElement.setAttribute('itemtype','http://schema.org/Review');
}
var reviewRatingElement = reviewElement.querySelector(".yotpo-review-stars");
var rating = "";
if (reviewRatingElement != null) {
if (this.format == "microdata") {
reviewRatingElement.setAttribute('itemprop','reviewRating');
reviewRatingElement.setAttribute('itemscope','');
reviewRatingElement.setAttribute('itemtype','http://schema.org/Rating');
}
rating = reviewRatingElement.querySelectorAll(".yotpo-icon-star").length;
// reviewRatingElement.querySelector(".sr-only").innerHTML // this returns something like "5 star ratings" so could be an alternate way to get the numbers
if (this.format == "microdata")
this._appendItemPropMetaTag(reviewRatingElement, 'ratingValue', rating);
}
else
console.log('yotpoRichSnippets: Failed to find reviewRating Element for review '+i);
if (this.format == "microdata") {
this._addItemProps(reviewElement, 'div[itemprop="review"] > .yotpo-header .yotpo-user-name', 'author');
this._addItemProps(reviewElement, 'div[itemprop="review"] > .yotpo-header .content-title .yotpo-font-bold', 'name');
this._addItemProps(reviewElement, 'div[itemprop="review"] > .yotpo-main .content-review', 'reviewBody');
this._addItemProps(reviewElement, 'div[itemprop="review"] > .yotpo-header .yotpo-review-date', 'datePublished');
}
if (this.format == "json-ld") {
var author = this._getInnerHTML(reviewElement, 'div > .yotpo-header .yotpo-user-name');
var name = this._getInnerHTML(reviewElement, 'div > .yotpo-header .content-title .yotpo-font-bold');
if (name === "") {
name = this._getInnerHTML(reviewElement, 'div > .yotpo-main .content-title.yotpo-font-bold'); // this may be the new way they structure the reviews
}
var reviewBody = this._getInnerHTML(reviewElement, 'div > .yotpo-main .content-review');
// have it stop at the first <span
var reviewBodyEndIndex = reviewBody.indexOf("<span");
if (reviewBodyEndIndex > -1)
{
reviewBody = reviewBody.substring(0, reviewBodyEndIndex);
}
var datePublished = this._getInnerHTML(reviewElement, 'div > .yotpo-header .yotpo-review-date');
jsonLd.review.push({
"@type": "Review",
"author": {
"@type": "Person",
"name": author
},
"reviewRating": {
"@type": "Rating",
"ratingValue": rating
},
"name": name,
"reviewBody": reviewBody,
"datePublished": datePublished
});
}
}
}
if (this.format == "json-ld") {
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);
}
}
// now an observer removes thse as they get added.
// var jsons = document.querySelectorAll("script[type='application/ld+json'].y-rich-snippet-script");
// console.log('yotpoRichSnippets: jsons: '+jsons.length);
// document.querySelectorAll("head")[0].setAttribute("jsons", jsons.length);
// for (i = 0; i < jsons.length; ++i) {
// var json = jsons[i];
// json.parentNode.removeChild(json);
// }
} catch(err) {
console.log('yotpoRichSnippets: _addMarkup Error: '+err.message); // not critical, as long as Googlebot does not cause it
}
},
_appendItemPropMetaTag: function (parentElement, name, content) {
var metaElement = document.createElement("meta");
metaElement.setAttribute('itemprop',name);
metaElement.setAttribute('content',content);
parentElement.appendChild(metaElement);
},
_addItemProps: function (baseElement, selector, name, content) {
var elements = baseElement.querySelectorAll(selector);
for (i = 0; i < elements.length; i++) {
var element = elements[i];
element.setAttribute('itemprop',name);
if (typeof content !== 'undefined' && content != null)
element.setAttribute('content',content);
}
},
_getInnerHTML: function (baseElement, selector) {
var elements = baseElement.querySelectorAll(selector);
if (elements.length > 0) {
return elements[0].innerHTML;
}
return "";
}
};
// let's get this party started. Can pass the selector for the placeholder here.
new webSiteAdvantage.yotpoRichSnippets();