Search code examples
javascriptjquerygoogle-apigoogle-custom-search

Is it possible to get Google Ads separately (to be used for scripting) for a specific search term using an API?


Aim: Getting Google ads separately to be embedded to a site.

What I tried so far:

1.) Google Custom Search API

→ does not deliver ads but search result

Coding information:
https://developers.google.com/custom-search/v1/cse/list

Code for getting search results:

 function requestSearchResult() {
    jQuery.get("https://www.googleapis.com/customsearch/v1/", {
            q: SearchTerm,
            cx: "01042***5fgs",
            key: "AL***Xra",
        },
        function(items, status) {
            document.getElementById("content").innerHTML += "<a href=\"" + items.items[0].link + "\">" + items.items[0].htmlTitle + "</a>";
            document.getElementById("content").innerHTML += "<br>" + items.items[0].displayLink;
            document.getElementById("content").innerHTML += "<br>" + items.items[0].htmlSnippet;
            document.getElementById('content').innerHTML += "<br>" + '<img src="' + items.items[0].pagemap.cse_thumbnail[0].src + '" alt="text mode" />';
            ...
            ...
        });

2.) Custom Search Element Control API

→ does not deliver ads separately but it is possible to embed a search box and/or search results together (not separately) with ads to a site, the search results and ads cannot be handled for further scripting

Coding information:
https://developers.google.com/custom-search/docs/element

Code for a search box:

<script>
  (function() {
    var cx = '01743***hxm';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchbox-only></gcse:searchbox-only>

Code for search results:

<script>
  (function() {
    var cx = '01743***hxm';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:searchresults-only></gcse:searchresults-only>

Solution

  • If you just need ads, take a look at https://developers.google.com/custom-search-ads/

    If you're looking to use Custom Search, but want more control over rendering, use the Custom Search Element Control API and the recently introduced callbacks feature: https://customsearch.googleblog.com/2019/06/introducing-callbacks-for-even-more.html

    Note that for policy reasons this does not provide any control over Ads rendering -- Ads must be rendered as provided.