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 Tools menu select Script editor

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 8-35 and 41-49 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.phone_number.country_code + requestData.phone_number.number : '',
    requestData.country,
    requestData.address.address,
    requestData.address.city,
    requestData.address.zip_code,
    requestData.financial_report.revenue,
    requestData.financial_report.currency,
    requestData.employees.from,
    requestData.employees.to,
    requestData.founded_at,
    requestData.industry_category,
    requestData.nace_text,
    requestData.duration,
    requestData.visits,
    requestData.pages_visited,
    requestData.pages.length > 0 ? requestData.pages[0].key : '',
    requestData.pages.length > 0 ? requestData.pages[0].value : '',
    requestData.pages.length > 1 ? requestData.pages[1].key : '',
    requestData.pages.length > 1 ? requestData.pages[1].value : '',
    requestData.pages.length > 2 ? requestData.pages[2].key : '',
    requestData.pages.length > 2 ? requestData.pages[2].value : '',
    requestData.pages.length > 3 ? requestData.pages[3].key : '',
    requestData.pages.length > 3 ? requestData.pages[3].value : '',
    requestData.pages.length > 4 ? requestData.pages[4].key : '',
    requestData.pages.length > 4 ? requestData.pages[4].value : ''
  ];
}

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 requestData = JSON.parse(e.postData.contents)
  var outputData = getOutputData(requestData, INCLUDE_CONTACTS)
  var sheet = SpreadsheetApp.getActiveSheet()
  appendDataInSheet(sheet, outputData)
  SpreadsheetApp.flush();
  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)
}

As seen in the comments of the code, you can reorder or remove code between line 8-35 and 41-49 to set your preferred output columns in Google Sheet.

3.
When you are happy with the code it's time to deploy it. From the Publish menu select Deploy 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, even anonymous 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 when the workflow is triggered. If your use case requires activity data such as visit duration or if your chosen segment uses exclusion filters for activity data, we recommend that you use Export lead after visit end as this will allow the workflow to be filtered based on data for the entire visit. 

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 criteria such as 'Seniority' or 'Department'.

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?