All Collections
Integrations
Webhooks
Integrating with Google Sheets via Webhooks
Integrating with Google Sheets via Webhooks
Victor Ekelund avatar
Written by Victor Ekelund
Updated over a week ago

This is a guide for setting up an Albacross Workflow in order to automatically export your leads to Google Sheets via a Webhook. This is a simple solution that requires no experience or knowledge in programming.

Want to learn more about Webhooks? Check out this article
Want to read the default Webhook Workflow integration guide? You'll find it here

To get started with a Workflow to Google Sheets via Webhooks:

0. Make sure that you have access to a Gmail or G Suite account. 

1. Create a Google Sheet file and from the Extensions menu select Apps Script

2. You will be taken to a new web page where you can see a short code template. Erase the code in the editor and paste the code snippet from below. 

// This variable could be set to false if contacts are not needed
var INCLUDE_CONTACTS = true

//The code between line 9-39 and 45-53 can be reordered and removed in order to
//create your preferred output format in the sheet
function getLeadData(requestData) {
  return [
    new Date(requestData.timestamp),
    requestData.name,
    requestData.website,
    requestData.phone_number.number,
    requestData.country,
    requestData.address.address,
    requestData.address.city,
    requestData.address.zip_code,
    requestData.financial_report.revenue_from,
    requestData.financial_report.revenue_to,
    requestData.financial_report.currency,
    requestData.employees.from,
    requestData.employees.to,
    requestData.founded_at,
    requestData.industry_category,
    requestData.nace_text,
    requestData.alexa_rank,
    requestData.socials.linkedin_url,
    requestData.duration,
    requestData.visits,
    requestData.pages_visited,
    requestData.pages.length > 0 ? requestData.pages[0].url : '',
    requestData.pages.length > 0 ? requestData.pages[0].page_views : '',
    requestData.pages.length > 1 ? requestData.pages[1].url : '',
    requestData.pages.length > 1 ? requestData.pages[1].page_views : '',
    requestData.pages.length > 2 ? requestData.pages[2].url : '',
    requestData.pages.length > 2 ? requestData.pages[2].page_views : '',
    requestData.pages.length > 3 ? requestData.pages[3].url : '',
    requestData.pages.length > 3 ? requestData.pages[3].page_views : '',
    requestData.pages.length > 4 ? requestData.pages[4].url : '',
    requestData.pages.length > 4 ? requestData.pages[4].page_views : '',
    requestData.contacts_url,
    requestData.profile_url
  ];
}

function getContactData(contact) {
  return [
    contact.email || '',
    contact.type || '',
    contact.first_name || '',
    contact.last_name || '',
    contact.department || '',
    contact.position || '',
    contact.seniority || '',
    contact.phone_number ? "'" + contact.phone_number : '',
    contact.linkedin || ''
  ]
}

//This function fires when receiving a GET request
function doGet(e) {
  return HtmlService.createHtmlOutput("Request received");
}

//This function fires when receiving a POST request
function doPost(e) {
var lock = LockService.getScriptLock();
lock.waitLock(30000);
  var requestData = JSON.parse(e.postData.contents)
  var outputData = getOutputData(requestData, INCLUDE_CONTACTS)
  var sheet = SpreadsheetApp.getActiveSheet()
  appendDataInSheet(sheet, outputData)
  SpreadsheetApp.flush();
lock.releaseLock();
  return HtmlService.createHtmlOutput("Request received");
}

//The functions below are helpers used in doPost()
function getContactsData(contacts, leadData) {
  return contacts.map(function (contact) {
    return leadData.concat(getContactData(contact))
  })
}

function getOutputData(requestData, includeContacts) {
  var leadData = getLeadData(requestData)
  var hasContacts = requestData.contacts.length
  if (hasContacts && includeContacts) {
    return getContactsData(requestData.contacts, leadData)
  }
  return [leadData]
}

function appendDataInSheet(sheet, rows) {
  var lastRow = Math.max(sheet.getLastRow(), 1);
  rows.forEach(function (row, idx) {
    insertRow(sheet, lastRow + idx, row)
  })
}

function insertRow(sheet, rowIdx, row) {
  sheet.insertRowAfter(rowIdx)
  row.forEach(function (value, idx) {
    insertCellValue(sheet, rowIdx + 1, idx + 1, value)
  })
}

function insertCellValue(sheet, rowIdx, colIdx, value) {
  sheet.getRange(rowIdx, colIdx).setValue(value === undefined ? '' : value)
}

As seen in the comments of the code, you can reorder or remove code between line 9-39 and 45-53 to set your preferred output columns in Google Sheet.

3. When you are happy with the code it's time to deploy it. Click the Deploy button and select New deployment and select type as Web app

4. You will now be prompted to choose a name for the Project. You will then get the option to choose deployment settings. Make sure that you Execute the app as yourself and select that Anyone has access to the app. (Note that this option might not be available in some instances of Google Apps. If so, please reach out to your Google Apps administrator or use a personal Gmail account)

If you are updating the code in the project, you will need to go through this step again. We recommend to always set the Project version to New in order to avoid versioning problems.

5. Finish the deployment of the web app by clicking Deploy. You will now see the URL where you can reach the web app. Copy this URL and make sure that you can paste it as you will need it when setting up the Workflow in Albacross.

5b. You can test the web app by pasting the URL in a browser. You should see a message saying Request received.

6. Now go back to the Albacross platform. When logged in to Albacross, click Workflows and select New Workflow.

7. Choose Webhooks in the popup.

8. Add a name for your Workflow and the URL you want to send data to. This is where you paste the URL that you copied from step 5. 

9. Add the conditions applied to the workflow in order to filter out which leads you want your Workflow to send. This filtering is added through the saved Segments from your account. If you want to create a new Segment, you can do so from the main leads list.

You will also have the option to choose if the workflow should only send a lead once or if it should send updates as well. If you choose to send updates as well, you will receive the same lead with updated activity data if that lead visits your webpage again. For the Google Sheet integration we recommend to only send the leads once.

10. Choose the output of data you want to be sent to your URL, for example only Company for the company lead or Company + Contacts for the company lead and contacts employed at the company. If you choose to include Contacts, you have the option of filtering the contact details on 'Keywords' and required fields.

11. When you are happy with the configuration of your workflow you need to save it. Click on the button Save changes.

12. The installation is done and you will now start to receive leads in your Google Sheet account as soon as they are processed. 🚀

Did this answer your question?