Snippets showing your reviews under a search result are designed to give users more information about this page and why it’s relevant to their query. This is a very attractive way to draw attention to your offers:
In order to add your stars as a review snippet to your shops listings in the organic search engine results on Google, some extra lines of structured data markup have to be embedded to your website. Your current rating and number of reviews within this markup can be retrieved using the Trusted Shops API.
With your reviews prepared like this, Google crawler can fetch the data automatically while visiting
your website.
Please note that even with the correct integration of a valid markup, Google does not guarantee that the
stars and grade will be displayed on search results pages.
Following Google’s guidelines, it is not recommended to embed rich snippets on your home page. Instead, product and category pages, as well as an extra content page, will be the right places.
For more information on Google’s rich snippets we suggest the following reading:
https://support.google.com/webmasters/answer/99170?hl=en
https://developers.google.com/search/docs/data-types/product
The APIs are limited to 120 requests within 60 minutes.
You can see the limit by controlling the response headers:
X-Rate-Limit-Limit: (int) current limit
X-Rate-Limit-Remaining: (int) the amount of requests left until the limit is reached
X-Rate-Limit-Reset: unix timestamp for limit expiration.
Upon reaching the limit the response "429 Too Many Requests" will be sent.
The PHP code below implements a call to the Trusted Shops API to retrieve the current grade and number
of reviews.
A caching routine saves the data locally, so no unnecessary API calls have to be made.
Finally, a markup according to the guidelines above is generated.
To verify a successful implementation, please use Rich Snippets Testing Tools.
<?php
/**
This is an example of integrating Trusted Shops Customer Reviews
marked with structured data into your website
Make sure you replace value of $tsid with your Trusted Shops ID
Make sure you have a writing permission to the
folder set in $cacheFileName
Requires version PHP 5.+
*/
$tsId = 'PASTE_YOUR_TS-ID_HERE';
$cacheFileName = '/tmp/' . $tsId . '.json';
$cacheTimeOut = 43200; // half a day
$apiUrl = 'http://api.trustedshops.com/rest/public/v2/shops/' . $tsId . '/quality/reviews.json';
$reviewsFound = false;
if (!function_exists('trustedshopscachecheck')) {
function trustedshopscachecheck($filename_cache, $timeout = 10800) {
if (file_exists($filename_cache) && time() - filemtime($filename_cache) < $timeout) {
return true;
}
return false;
}
}
// check if cached version exists
if (!trustedshopscachecheck($cacheFileName, $cacheTimeOut)) {
// load fresh from API
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
$output = curl_exec($ch); curl_close($ch);
// Write the contents back to the file
// Make sure you can write to file's destination
file_put_contents($cacheFileName, $output);
}
if ($jsonObject = json_decode(file_get_contents($cacheFileName), true)) {
$result = $jsonObject['response']['data']['shop']['qualityIndicators']['reviewIndicator']['overallMark'];
$count = $jsonObject['response']['data'] ['shop']['qualityIndicators']['reviewIndicator']['activeReviewCount'];
$shopName = $jsonObject['response']['data']['shop']['name'];
$max = "5.00";
if ($count > 0) {
$reviewsFound = true;
}
}
if ($reviewsFound) {
// Output aggregated reviews markup:
$ts_snippet = '<script type="application/ld+json">';
$ts_snippet .= '{';
$ts_snippet .= '"@context": "http://schema.org",';
$ts_snippet .= '"@type": "Organization",';
$ts_snippet .= '"name": "'.$shopName.'",';
$ts_snippet .= '"aggregateRating" : {';
$ts_snippet .= '"@type": "AggregateRating",';
$ts_snippet .= '"ratingValue" : "'.$result.'",';
$ts_snippet .= '"bestRating" : "'.$max.'",';
$ts_snippet .= '"ratingCount" : "'.$count.'"';
$ts_snippet .= '}}';
$ts_snippet .= '</script>';
echo $ts_snippet;
}
Please note that caching of API calls is essential. Without caching your website, it will be directly affected in case of a downtime of the API or poor network performance.
As data like overall mark and number of reviews returned by the Trusted Shops API are calculated on a daily basis, it is sufficient to update your cache once or twice a day.
Please note that Trusted Shops may limit the number of API calls per day and website without notice.