I am writing a website in which I need to extract data from google reviews. I used the Google Maps library to get the places but it only returns 5 reviews (the place I am getting the data from has over 70)
reviews.html
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
function fetchGoogleReviews(placeId) {
var service = new google.maps.places.PlacesService(document.createElement('div'));
var request = {
placeId: placeId,
fields: ['name', 'rating', 'reviews']
};
function fetchReviews(nextPageToken) {
request.pageToken = nextPageToken || "";
service.getDetails(request, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var allReviews = place.reviews || []; // Initialize empty array if no reviews in first response
if (nextPageToken) {
// Append reviews from subsequent requests
allReviews = allReviews.concat(reviewsView.reviews); // Replace reviewsView.reviews with your existing array to store all reviews
}
const stars = place.rating;
document.getElementById("stars").setAttribute("data-star", stars);
document.getElementById("stars-text").innerHTML = `${stars} stars`;
const comments = document.createElement("div");
comments.id = `reviews-number`;
comments.innerHTML = `${allReviews.length} reviews`;
const googleReviewsView = document.createElement("a");
googleReviewsView.href = `url`
const googleReviews = document.createElement("img")
googleReviews.src = `/media/assets/google-reviews.png`
googleReviewsView.appendChild(googleReviews)
comments.appendChild(googleReviewsView);
reviewsView.appendChild(comments);
displayReviews(allReviews);
// Check for next page token and make another request if available
if (place.next_page_token) {
fetchReviews(place.next_page_token);
}
} else {
console.error('Error fetching place details:', status);
}
});
}
fetchReviews(); // Initial request without nextPageToken
}
// Function to display reviews on the webpage
function displayReviews(reviews) {
for(let i = 0; i < reviews.length; i++){
reviewsView.appendChild(newReview(reviews[i]))
}
}
// Call the fetchGoogleReviews function with your business's place ID
fetchGoogleReviews('PLACEID');
what can I do to get all the reviews?