Search code examples
google-custom-search

How to Know Available Data from my website for my Google Custom Search


I'm trying to implement a Google Custom Search Engine into my website. So far, I've been able of changing my snippets layout, and show the variables Google shows in its examples:

http://googleajaxsearchapi.blogspot.com.es/2010/04/rendering-custom-data-in-custom-search.html

Well, in the examples, everything looks great, BECAUSE THEY KNOW THE VALUES THEY MAY PRINT. I mean, if you see this snippet:

<div id="mysite_thumbnail">
  <div data-if="Vars.thumbnail" class="gs-image-box gs-web-image-box">
    <a class="gs-image" data-attr="{href:url, target:target}">
      <img class="gs-image" data-attr="{src:thumbnail.src, width:48, height: 48}"/>
    </a>
  </div>
</div>

it's pretty clear that "Vars" is holding some data GSE is printing. My problem is that I don't know what "Vars" holds, and when developing my view I can't know if the value is there, and what is its name.

So, the question is: How can I print "Vars"? I suppose is a js variable you may obtain from the jsapi, but juss guessing, console.log() was not working for me, :(


Solution

  • Well, I finally found out how to post the data:

    From Google Search Engine Api documentation:

    https://developers.google.com/custom-search/docs/js/rendering?hl=es&csw=1#richsnip

    You only have to add the following code in your snippet:

    <span data-body="JSON.stringify(Vars)"></span>
    

    So, you'll have something like:

    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    <script type="text/javascript">
          // Load the Search API
          google.load('search', '1');
    
          // Set a callback to load the Custom Search Control when you page loads
          google.setOnLoadCallback(
          function(){
             new google.search.CustomSearchControl('XXXXXXXXXXXXXXX').draw('cse');
             google.search.Csedr.addOverride("mysite_");
          },
          true);
    
          console.log(google);
    </script>
    
       <div style="display:none">
          <div id="mysite_thumbnail">
    
             //This will show all Vars content
             <span data-body="JSON.stringify(Vars)"></span>
    
             <div data-if="Vars.thumbnail" class="gs-image-box gs-web-image-box">
                <a class="gs-image" data-attr="{href:url, target:target}">
                   <img class="gs-image" data-attr="{src:thumbnail.src, width:48, height: 48}"/>
                </a>
             </div>
             <div data-ifel="Vars.thumbnail == 0" class="gs-image-box gs-web-image-box">
                <a class="gs-image" data-attr="{href:url, target:target}">
                   <img class="gs-image" data-attr="{src:'XXXXX.png', width:115, height: 90}"/>
                </a>
             </div>
          </div>
    
          <div id="mysite_webResult">
             <div class="gs-webResult gs-result"
                data-vars="{longUrl:function() {
                var i = unescapedUrl.indexOf(visibleUrl);
                return i < 1 ? visibleUrl : unescapedUrl.substring(i);}}">
                <table>
                   <tr>
                      <td valign="top">
                         <div data-if="Vars.richSnippet" data-attr="0"
                            data-body="render('thumbnail',richSnippet,{url:unescapedUrl,target:target})"></div>
                      </td>
    
                      <td valign="top">
                         <div class="gs-title">
                            <a class="gs-title" data-attr="{href:unescapedUrl,target:target}"
                               data-body="html(title)"></a>
                         </div>
                         <div class="gs-snippet" data-body="html(content)"></div>
                      </td>
                   </tr>
                </table>
             </div>
          </div>
       </div>