William
William

Reputation: 141

Google’s sitelinks search box for Google Custom Search

I’m completely flumped here on how to use Google’s sitelinks search box with Google’s custom search.

Would anyone know how I even begin connecting the 2?

For the site links search box Google give the following code:

<script type="application/ld+json">
{
   "@context": "http://schema.org",
   "@type": "WebSite",
   "url": "https://www.example-petstore.com/",
   "potentialAction": {
     "@type": "SearchAction",
     "target": "https://query.example-petstore.com/search?q={search_term_string}",
     "query-input": "required name=search_term_string"
   }
}
</script>

And then you have Google’s custom search code:

<script>
  (function() {
    var cx = '006674923042857018221:WMX2084923030';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>

How on earth do I connect the 2? Hope you can point me in the right direction.

Upvotes: 2

Views: 564

Answers (1)

Turadg
Turadg

Reputation: 7681

In the Javascript code that Google provides there is a var cx = 'YOUR_LONG_CX_KEY. Use that to fill in potentialAction.target with this URL pattern:

 "target": "http://www.google.com/cse?cx=YOUR_LONG_CX_KEY&q={search_term_string}",

You can confirm that URL works by hitting it in your browser with your own search term string.

However, I don't know if Google will accept this as their docs for the "target" field say, "This must be a URL pattern that points to an address on the same domain as the content being searched." On the other hand, the top of that same page says, "No search engine for your site? You can set one up with Google Custom Search Engine."

Upvotes: 1

Related Questions