Sorting product Google CSE results with a custom attribute

5/11/2015 12:00:00 PM

Google Custom Search Engine

Google offers a custom search engine service for business and private users to easily integrate the Google search engine into their own websites. You can implement this in multiple ways going from only adding a search box in your website to implementing your own and sending JSON/Atom requests, styling the result yourself. This makes it easy to integrate the power of Google search into your own website without having to spend a lot of money.

But what if you want to sort these results in an E-commerce environment based on custom data, for example on product type or price. That is what this post is about and in this example I will add an attribute "weight" to the product pages and use that to sort my search results.

Adding the attribute to the product pages

The first thing you need to do is tell Google what data it needs to index so it can provide it to you again for filters and sorting. There are 2 ways to do this without manually submitting the data, but in my experience only one works.

The sitemap

The first way to do it is to add the attribute to your sitemap:

<url>
  <loc>
    https://your-site.com/page.html
  </loc>
  <changefreq>monthly</changefreq>
  <priority>0.8</priority>
  <pagemap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0">
    <dataobject type="document">
      <attribute name="weight">1</attribute>
    </dataobject>
  </pagemap>
</url>

To add more data to a page you need to add "structured data". In sitemaps you do this by adding a "pagemap" element to it with a child element called "dataobject". The "dataobject" is the container for all your attributes that you want to add for a specific type. There are more types available in the pagemap, but for this example we only use "document".

I added an attribute named "weight" as a custom attribute in the sitemap. Although this is an example that you can find on the Google support page this did not work for me and did not end up showing in my query results.

The HTML page

The second way to add structured data to your search results is to add the pagemap data directly into your HTML page like this:

<head>
 <!--
 <PageMap>
  <DataObject type="document">
   <Attribute name="weight">1</Attribute>
  </DataObject>
 </PageMap>
 -->
</head>

You need to add the same thing as in the sitemap (some small changes are required though) to the <head> of your website. You can test if the structure is ok by making use of the Google structured data tester.

Google structured data tester

If your filters show up in the same format as in the above image, your data has been implemented correctly.

Sorting your results

Now all you need to do is wait for Google to re-index your website with this new data. Once that is done you will see the following in your JSON responses:

"items":[  
 {  
          "pagemap":{  
            "document":[  
               {  
                  "weight":"1"
               }
            ]
 }

Now the last step: you add the "sort" parameter to your request URL like this:

https://www.googleapis.com/customsearch/v1?key=INSERT_YOUR_API_KEY&cx=017576662512468239146:omuauf_lfve&q=searchterm&sort=document-weight

Now the results with the highest weight will be shown on top and the lowest at the bottom, hope this post helped implementing this in your website!

You Might Also Like

0 reacties