Comparisons Alerts Webhook

Overview

This document provides a web-hook that can be used as it is or customized, to trigger alerts for Comparison results in Audits or Web Journeys.

Note: SSO users will want to modify several lines to ensure that the link in the email send takes them to the appropriate sub-domain.

Publishing & Deploying

To see instructions on how to publish and deploy a web-hook in Google Scripts, refer to the following help doc.

Webhook Script

Make sure to provide an API key, email addresses (separated by commas), and a customized subject line.

//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;

  // USERS MUST PROVIDE THE INFO BELOW

  let apiKey = 'XXXXXXXXXXXXXXXXXXXXX'; //insert your API key here
  let email = 'XXXXXXXXXXXXXXXXXXX'; //provide recipient emails here
  let subject = 'ObservePoint | Comparison Alert'; //provide a custom subject line here

  ///////////////////////////////////////////////////////////////////////////////////////
  
  let htmlBody; //this will be defined based on whether this alert is for an audit or web-journey

  //this flag will determine whether an email is sent or not
  let changes = false;

  if(itemType == 'web-audit') {

    //get audit comparisons data
    let endpoint = '/comparisons/audits/' + itemID + '/scores';
    let comparisonResults = apiRequestGET(endpoint, apiKey);
    let comparisonSummary = '';
    let match = comparisonResults.find(res => (res.runId == runID));

    if(match.tagParityScore != 100 || match.variableParityScore != 100 || match.variableValueParityScore != 100) {
      changes = true;
      comparisonSummary += '<br><br><b>Tag Parity:</b> ' + match.tagParityScore + '%<br><b>Variable Parity:</b> ' + match.variableParityScore + '%<br><b>Value Parity:</b> ' + match.variableValueParityScore + '%<br><br>';
    }

    //update htmlBody if changes occurred
    if(changes) {
      htmlBody = '<b>Comparison Summary</b><br><br>In a Web Audit that just completed scanning, changes were identified in your analytics implementation.' + comparisonSummary + 'To see details, click the following link to take you to the full report:';
      htmlBody += '<br><br><a href="https://next.observepoint.com/audit/' + itemID + '/run/' + runID +'/comparisons/results">https://next.observepoint.com/audit/' + itemID + '/run/' + runID +'/comparisons/results</a>';
    }
  } else if(itemType == 'web-journey') {

    //check for variable differences
    
    let endpoint = '/comparisons/web-journeys/' + itemID +'/runs/' + runID + '/variables'
    let response = apiRequestGET(endpoint, apiKey);
    
    if(response.length > 0) {
      changes = true;
    }

//check for tag differences
  let tagEndpoint = '/comparisons/web-journeys/' + itemID + '/runs/' + runID + '/tag-account-presence';
  let tagResponse = apiRequestGET(tagEndpoint, apiKey);

  tagResponse.forEach(res => {
      res.tagAccountPresenceDifferences.forEach(diff => {            
         if(diff.actualCount != diff.expectedCount) {
              changes = true;
         }
      });
  });

//update htmlBody if changes occurred
if(changes) {
  htmlBody = '<b>Alert Summary</b><br><br>In a Web Journey execution that just completed, changes were identified in your analytics implementation. To see details, click the following link to take you to the full report (<b>Note:</b> Use the "Only Show Changes toggle" to zoom in on changes):';
  htmlBody += '<br><br><a href="https://next.observepoint.com/web-journey/' + itemID +'/run/' + runID + '/results">https://next.observepoint.com/web-journey/' + itemID +'/run/' + runID + '/results</a>';
 }
}

//if differences exist, send alert
if(changes) {

//send email via Gmail API
  MailApp.sendEmail(
    email,
    subject,
    '',
    {
      htmlBody: htmlBody,
      noReply: true //all emails will be send from noreply@observepoint.com
    }
  );
 }

  return HtmlService.createHtmlOutput("post request received");
}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.