Skip to content

Google Webhook Script


Below is the script for pasting into the Apps Script Code.gs file for linking Google Ads lead form ad extension to Google Sheets. Navigate back to blog: Connecting Google Ads Leads Form To Google Spreadsheets

function init() {    tableId = ‘1r2yb8a4qwSDfDvrE13cv2pjYBjPK8sx48To1w3YsCW0’;       googleKey = “w3bh00kId”;    ss = SpreadsheetApp.openById(tableId);    leadsSheet = ss.getSheetByName(‘Leads’);   }
function doPost(e){    init();      let dataFromGoogleAds = JSON.parse(e.postData.contents);    let date = new Date().toISOString().slice(0,10);    let userColumnData = dataFromGoogleAds.user_column_data;    let formId = dataFromGoogleAds.form_id;    let adgroupId = dataFromGoogleAds.adgroup_id;    let creativeId = dataFromGoogleAds.creative_id;    let gclid = dataFromGoogleAds.gcl_id;    let header = getCurrentTableHeader();    let userColumnDataNew = changeDataArray(userColumnData);    if (dataFromGoogleAds.google_key == googleKey)    {       let row = [date,formId,adgroupId,creativeId,gclid];             header = updateHeader(header, userColumnData);           for(i=5;i<header.length;i++)      {        row.push(userColumnDataNew.hasOwnProperty(header[i]) ? userColumnDataNew[header[i]] : “”);      }                leadsSheet.appendRow(row);      }     }
function getCurrentTableHeader(){   init();   let lastColumn = leadsSheet.getLastColumn();     let tableHeader = [];   if(lastColumn == 0)   {     tableHeader = [“Date”, “Form ID”, “Adgroup ID”,”Creative ID”,”Gclid”];   }   else   {     let range = leadsSheet.getRange(1,1,1,lastColumn);     let values = range.getValues();     tableHeader = values[0];   }     return tableHeader; }
function updateHeader(header, userColumnData){   for(i=0;i<userColumnData.length;i++)        {          if(!header.includes(userColumnData[i].column_id))          {            header.push(userColumnData[i].column_id);                     }         }     let values = [];   values[0] = header;   leadsSheet.getRange(1, 1, 1, header.length).setValues(values);   return header;     }
function changeDataArray(userColumnData){      let userColumnDataNew = [];    for(i=0;i<userColumnData.length;i++)    {      userColumnDataNew[userColumnData[i].column_id] = userColumnData[i].string_value;    }    return userColumnDataNew; }

Navigate back to blog: Back to Blog

2 thoughts on “Google Webhook Script”

Leave a Reply

Your email address will not be published. Required fields are marked *