Integrate a Custom Google Programmable Search Engine on Websites

In this tutorial, you can learn how to Integrate a Custom Google Programmable Search Engine for websites using JavaScript. The main purpose of this tutorial is to provide students and beginners with a reference for learning to integrate the google search engine into a website. Here is a simple web page script that demonstrates the integration of the google search engine into a simple website.

What is Google Programmable Search Engine?

The Google Programmable Search Engine was formerly called Google Co-op and Google Custom Search. It is a platform provided by Google that allows web developers to integrate and enhance their website search feature to provide the indexed web pages of their site to their visitors. Google also allows to developers to customize the Search Engine according to the site's needs.

Getting Started

Before we proceed to the coding part, we must create first the custom Programmable Search Engine in Google. To do that follow the instructions below.

How to Create a Google Programmable Search Engine?

  1. Make sure that you have already created a Google Account
  2. Log in to your Google Account in your browser.
  3. Browse https://programmablesearchengine.google.com/
  4. Next, click the Get Start button on the web page.
  5. Next, fill in all the required fields on the form like the following image.

    How to Create a Google Programmable Search Engine

  6. Then, Google will generate a code that you will need for integration.

    How to Create a Google Programmable Search Engine

  7. Click the Customize button if you want to customize the search engine.

How to use the Google Programmable Search Engine generated Code?

We can simply integrate the Google Programmable Search Engine that we created using the generated code that was provided after the successful creation of our Search Engine. The below script demonstrates the usage of the generated code.

Page Interface

Here's the HTML file script known as index.html. It contains the page layout elements and the generated code. Make sure to change the Search engine ID with the one you created.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Google Programmable Search Engine</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
  10. <script async src="https://cse.google.com/cse.js?cx=03a19a7f685254faf">
  11. </script>
  12. <link rel="stylesheet" href="style.css">
  13.  
  14. </head>
  15. <div id="wrapper">
  16. <div class="page-title">Google Programmable Search Engine</div>
  17. <hr style="margin:auto; width:25px">
  18. <div id="gpse-wrapper">
  19. <div class="gcse-search"></div>
  20. </div>
  21. </div>
  22. </html>

Stylesheet

Here's the custom CSS code for the design of the web page. The file is known as

class="text-danger">style.css.

  1. @import url('https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet');
  2. :root{
  3. --secular-font: 'Secular One', sans-serif;
  4. }
  5. *{
  6. box-sizing: border-box;
  7. }
  8. body *{
  9. font-family: 'Rubik', sans-serif;
  10. }
  11. /**
  12. Page Design
  13. */
  14. body,
  15. html{
  16. height: 100%;
  17. width: 100%;
  18. margin: 0;
  19. padding: 0;
  20. }
  21. body{
  22. background-color: #B9F3E4;
  23. }
  24. /* Page Wrapper */
  25. #wrapper{
  26. width: 100%;
  27. padding: 3em 5em;
  28. }
  29. .page-title{
  30. font-size: 2.5rem;
  31. font-weight: 500;
  32. color: #fff;
  33. letter-spacing: 3px;
  34. font-family: var(--secular-font);
  35. text-align: center;
  36. text-shadow: 0px 0px 3px #2020208c;
  37. }
  38. #gpse-wrapper {
  39. position: relative;
  40. width: 720px;
  41. margin: auto;
  42. margin-top: 3em;
  43. background: #fff;
  44. padding: 2em 2em;
  45. }

Here are the snapshots of the overall result of the script that I provided above.

Google Search Box on the Web Page

Sample Web Page with Programmable Search Engine

Google Search Result on the Web Page

Sample Web Page with Programmable Search Engine

How to Integrate Google Programmable Search Engine using JavaScript?

We can also integrate the Google Programmable Search Engine using JavaScript to place the search engine's search box and result in the custom element or specific area of the page. Check out the sample web page script below to know about how to achieve it using JavaScript.

Page Interface

Here is the HTML file script known as custom.html. It contains the page layout and custom search engine elements.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Google Programmable Search Engine</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
  10. <!-- Change cx parameter value with your created custom search engine ID -->
  11. <script async src="https://cse.google.com/cse.js?cx=03a19a7f685254faf">
  12. </script>
  13. <link rel="stylesheet" href="style.css">
  14.  
  15. </head>
  16. <div id="wrapper">
  17. <div class="page-title">Google Programmable Search Engine - Custom</div>
  18. <hr style="margin:auto; width:25px">
  19. <div id="gpse-wrapper">
  20. <div id="gpse-custom-search-box"></div>
  21. </div>
  22. <div id="gpse-result-wrapper">
  23. <div id="gpse-custom-result-box"></div>
  24. </div>
  25. </div>
  26. <script src="custom-gpse.js"></script>
  27. </html>

Stylesheet

Here's the CSS file script known as style.css. It contains the CSS code for the design of the page layout and custom search engine elements.

  1. @import url('https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet');
  2. :root{
  3. --secular-font: 'Secular One', sans-serif;
  4. }
  5. *{
  6. box-sizing: border-box;
  7. }
  8. body *{
  9. font-family: 'Rubik', sans-serif;
  10. }
  11. /**
  12. Page Design
  13. */
  14. body,
  15. html{
  16. height: 100%;
  17. width: 100%;
  18. margin: 0;
  19. padding: 0;
  20. }
  21. body{
  22. background-color: #B9F3E4;
  23. }
  24. /* Page Wrapper */
  25. #wrapper{
  26. width: 100%;
  27. padding: 3em 5em;
  28. }
  29. .page-title{
  30. font-size: 2.5rem;
  31. font-weight: 500;
  32. color: #fff;
  33. letter-spacing: 3px;
  34. font-family: var(--secular-font);
  35. text-align: center;
  36. text-shadow: 0px 0px 3px #2020208c;
  37. }
  38. #gpse-wrapper {
  39. position: relative;
  40. width: 720px;
  41. margin: auto;
  42. margin-top: 3em;
  43. background: #fff;
  44. padding: 2em 2em;
  45. }
  46. #gpse-result-wrapper {
  47. position: relative;
  48. width: 720px;
  49. margin: auto;
  50. margin-top: 1em;
  51. background: #fff;
  52. padding: 2em 2em;
  53. }
  54. #gpse-result-wrapper:has(#gpse-custom-result-box:empty){
  55. display: none;
  56. }

Rendering Google Programmable Search Engine

Here's the JavaScript file script known as custom-gpse.js. It contains the codes that render the search box and search result to the specific page element.

  1. /** Element Selector */
  2. const SearchBoxHolder = document.getElementById('gpse-custom-search-box')
  3. const SearchResultHolder = document.getElementById('gpse-custom-result-box')
  4. var params = new URLSearchParams(location.search)
  5.  
  6. window.onload = function(){
  7. if(document.readyState == 'complete'){
  8. /** Render Google Custom Searchbox */
  9. if(SearchBoxHolder !== null)
  10. renderSearchBox();
  11.  
  12. /** Render Google Custom Result Box if search query exsits */
  13. if(SearchResultHolder !== null && params.has('search') == true){
  14. renderResultBox();
  15. }
  16. }
  17. }
  18. const renderSearchBox = () => {
  19. /** Rendering Searchbox to custom Element */
  20. google.search.cse.element.render(
  21. {
  22. div: SearchBoxHolder.id,
  23. tag: 'searchbox-only',
  24. attributes: {
  25. resultsUrl: 'https://localhost/google-programmable-se/custom.html?search=gpse'
  26. }
  27. }
  28. );
  29. }
  30.  
  31. const renderResultBox = () => {
  32. /** Rendering Search Result to custom Element */
  33. google.search.cse.element.render(
  34. {
  35. div: SearchResultHolder.id,
  36. tag: 'searchresults-only'
  37. }
  38. );
  39. }

Here are the snapshots of the overall result of the web page scripts that I provided above.

The search box on the Web Page

How to Integrate Google Programmable Search Engine using JavaScript

Search Result on the Web Page

How to Integrate Google Programmable Search Engine using JavaScript

There you go! I have also provided the complete source code zip file on this site and it is free to download. Feel free to download and modify it. The download button is located below this tutorial's content.

That's it! I hope this Integrate a Custom Google Programmable Search Engine on Websites Tutorial will help you with what you are looking for and will be useful for your current and future website or web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment