Skip to main content
All CollectionsStandards
Comparisons Alerts Webhook
Comparisons Alerts Webhook
Luiza Gircoveanu avatar
Written by Luiza Gircoveanu
Updated over 9 months ago

Overview

This document provides a webhook that can be used as it is or customized, to trigger alerts for Comparison results in Audits or 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 webhook 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 == 'audit') {

//get audit comparisons data
let endpoint = '/comparisons/audits/' + itemID + '/scores';
let comparisonResults = apiRequestGET(endpoint, apiKey);
let comparisonSummary = '';

comparisonResults.forEach(cr => {

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

});

//update htmlBody if changes occurred
if(changes) {
htmlBody = '<b>Comparison Alert 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://app.observepoint.com/audit/' + itemID + '/run/' + runID +'/comparisons/results">https://app.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);

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

//update htmlBody if changes occurred
if(changes) {
htmlBody = '<b>Comparison 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://app.observepoint.com/web-journey/' + itemID +'/run/' + runID + '/results">https://app.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 [email protected]
}
);
}
return HtmlService.createHtmlOutput("post request received");
}

function apiRequestGET(endpoint,apiKey) {
var url = 'https://api.observepoint.com/v3' + endpoint;
var options = {
'method': 'GET',
'headers': {
Authorization: 'api_key ' + apiKey
},
'contentType': 'application/json',
'muteHttpExceptions': true
}
var ret = UrlFetchApp.fetch(url, options);
return JSON.parse(ret);

}
Did this answer your question?