Google Script Custom Webhook

Overview

Webhooks are powerful tools used to trigger actions via API. Within Audits and Journeys users can add custom web-hook URLs to be triggered at the completion of the audit/journey. 

The web-hook itself can be customized to do virtually anything you want with ObservePoint's API and any other API you have access to. One common use case is using a web-hook to send alerts to Slack instead of to your email.

Google Script Webhook

Webhooks can be created with a variety of tools, but our favorite (which is free) is Google Script. 

  1. Visit script.google.com. If needed, login to your Google account.
  2. Click New Project.
  3. Name the project, and then add the following code snippet to the default .gs file.

    // This is a function that fires when the webapp receives a GET request
    function doGet(e) {
      return HtmlService.createHtmlOutput("request received");
    }
    // This is a function that fires when the webapp receives a POST request
    function doPost(e) {
      var myData = JSON.parse(e.postData.contents);
    
      var itemType = myData.itemType; // The item type will be a 'web-journey', 'web-audit', or 'app-journey'.
      var itemId = myData.itemId; // The item ID will contain the ID of the Web Audit, Web Journey, or App Journey.
      var runId = myData.runId; // The run ID will contain the ID for the specific run of the Web Audit, Web Journey, or App Journey.
    
    // DO WHATEVER YOU WANT!!!
    
    }
    	
  4. Make it your own! ObservePoint provides the web-hook script with the necessary IDs to leverage our API.

    For example, with the Audit ID and the Run ID, you can trigger a specific export every time an audit completes.

    For another example, demonstrated below, you can send a custom email using Gmail's API.
    //this is a function that fires when the webapp receives a GET request
    function doGet(e) {
      return HtmlService.createHtmlOutput("request received");
    }
    
    //this is a function that fires when the webapp receives a POST request
    function doPost(e) {
      var myData = JSON.parse(e.postData.contents);
      
      var itemType = myData.itemType;
      var itemID = myData.itemId;
      var runID = myData.runId;
    
      var email = 'test@observepoint.com';
      var subject = 'Webhook Email Subject';
      var htmlBody = 'This is where you will put a custom message. </br>' + 'Item Type: ' + itemType + ', Item ID: ' +  itemID + ', Run ID: ' + runID;
    
    //send email via Gmail API
      MailApp.sendEmail(
        email,
        subject,
        '',
        {
          htmlBody: htmlBody,
        }
      );
      return HtmlService.createHtmlOutput("post request received");
    }
    	
  5. Next, click Publish and Deploy as web app.
  6. Next, select a project version, add a version description, select your google account as the executor, and give access to 'Anyone, even anonymous' (so ObservePoint can properly trigger it).
  7. Finally click Deploy and copy the Current web app URL, then paste it into the audit or journey web-hook field.
Note: Each time you update the web-hook script, you will need to change the project version to New or the changes will not be published. Also, the URL does not need changed, so you don't have to worry about updating audit and journey configurations.

Debugging

Debugging web-hooks is tricky. You can either use an email integration to send logs via email to yourself or send logs to a spreadsheet.

The spreadsheet method is the fastest. Simply create a new Google Sheet, and grab the Spreadsheet ID from the URL.

https://docs.google.com/spreadsheets/d/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/edit#gid=0

Then use re-use this code snippet to modify any cells you want:

SpreadsheetApp.openById('XXXXXXXXXXXXXXX').getSheets()[0].getRange('a1').setValue('Put your debugging logs here');

For further help or questions, reach out to our customer success team at support@observepoint.com or our in-app chat feature.

Using ObservePoint's API

We provide great API documentation for your reference when building these web-hooks. You'll also find this help doc with pre-written functions for GET and POST requests very useful.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.