Articles:
- Data export to Google Sheets
- Connecting the FB Pixel to a project on Kwiga
- Sending a webhook to transfer data to another service
- Zoom integration and usage essentials
- Generating payment links with pre-filled data
- Handling a button click in the widget
- Sending a webhook to KeyCRM with separation of the application and payment.
We will show you how to set up automatic data export from Kwiga to Google Sheets.
You can set up data export for various events using automation (you can specify contact added, tag added, successful or failed purchase, etc. as the event; the full list of events is available in the article).
Additionally, you can use the option to send a webhook when using widgets; the workflow remains the same.
Main steps:
-
Create a file and add the script to it
-
Create a web app and get the link for sending the webhook
-
Set up webhook sending using automation in Kwiga or form submission in the widget.
The entire process is shown in the video and does not require programming knowledge. Just follow the steps provided, and if something does not work out, contact support.
Step 1: Create a file where data from Kwiga will be sent
Open Google Sheets and create a new file, then open the script editor.
A new window will open, and you need to paste the script into its editor. You can download the script via the link (or copy the text at the bottom of the article). You need to delete all the text that is already in the editor.
Final result:
Step 2: Create a web app and get the link for sending the webhook
In the same script editor, find the Deploy button and select New deployment
Select Web app as the type, and in the Who has access dropdown set it to Everyone, then click Deploy.
As a result, you will get the Web App URL, which you then need to add in Kwiga.
Step 3: Set up data sending in Kwiga
Click the All solutions button in the top menu and select Automation. Create a new trigger and choose the event that will launch the automation. This can be contact added, tag added, payment, or any other event.
After that, scroll down and select Webhook - Send data in the actions. In the link field, specify the Web App URL, request type POST, and select the fields that should be exported to the table.
Save it, and all steps are completed.
Alternatively, you can set up webhook sending from the widget; this is available when using the Form element. Check the Additionally send to a third-party service option, then in the form that opens add the link, request type POST, and select the parameters to send. Only the parameters that are present in the form (this widget) will be available in this list.
How the Google Sheets script for webhooks works
- On the first webhook call, table headers are automatically created based on the keys of incoming data
- If the table already has headers and you need to start receiving new data fields, add these headers to the table manually
- Headers can be rearranged in the table - the script will automatically determine their positions
- By default, the script adds new entries each time a webhook is received
- If fields are specified in keyColumns, the script will update existing entries or add new ones if no match is found
- ⚠️ Important: After changing the script, you need to deploy it again in Google Apps Script and update the webhook link
Selecting a sheet:
- By default, the active sheet is used
- It can be overridden through webhook parameters:
- "sheet_name": "Sheet Name" - to select a sheet by name
- "sheet_index": 2 - to select a sheet by number (starting from 1)
// ============================================================
// SCRIPT SETTINGS
// ============================================================
// Duplicate check - specify fields to search for existing records
// Usage examples:
// var keyColumns = ["email"]; - check by email
// var keyColumns = ["order_id"]; - check by order ID
// var keyColumns = ["email", "order_id"]; - check by multiple fields
var keyColumns = []; // Duplicate check disabled
// ============================================================
// Function for sending a response
function sendResponse(success, message) {
var response = {
"status": success ? "success" : "error",
"message": message
};
return ContentService.createTextOutput(JSON.stringify(response))
.setMimeType(ContentService.MimeType.JSON);
}
// Getting the target page for data entry
function getTargetSheet(hook_data) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Checking if the page name is specified
if (hook_data && hook_data.sheet_name) {
try {
var sheet = spreadsheet.getSheetByName(hook_data.sheet_name);
if (sheet) {
return sheet;
}
} catch (error) {
// Page not found
}
}
// Checking if the page number is specified
if (hook_data && hook_data.sheet_index && !isNaN(hook_data.sheet_index)) {
try {
var sheets = spreadsheet.getSheets();
var index = parseInt(hook_data.sheet_index) - 1; // Converting to index from 0
if (index >= 0 && index < sheets.length) {
return sheets[index];
}
} catch (error) {
// Invalid page index
}
}
// By default, use the active page
return SpreadsheetApp.getActiveSheet();
}
// Creates headers in the table based on the provided data
function populateHeaders(hook_data, sheet) {
if (!hook_data || typeof hook_data !== 'object') {
return false;
}
if (!sheet) {
sheet = getTargetSheet(hook_data);
}
var last_row = Math.max(sheet.getLastRow(), 1);
sheet.insertRowAfter(last_row);
var headers_row = ['timestamp'];
var hook_data_keys = Object.keys(hook_data);
for (var i = 0; i < hook_data_keys.length; i++) {
headers_row.push(hook_data_keys[i]);
}
sheet.appendRow(headers_row);
SpreadsheetApp.flush();
return true;
}
// Processing incoming webhook
function doPost(e) {
var hook_load = null;
// Checking for POST data
if (!e || !e.postData || !e.postData.contents) {
return sendResponse(false, "No data to process");
}
// Parsing JSON data
try {
hook_load = JSON.parse(e.postData.contents);
} catch (error) {
return sendResponse(false, "JSON data parsing error");
}
// Validation of received data
if (!hook_load || typeof hook_load !== 'object' || Object.keys(hook_load).length === 0) {
return sendResponse(false, "Incorrect or empty data received");
}
try {
// Getting the target page
var sheet = getTargetSheet(hook_load);
if (!sheet) {
return sendResponse(false, "Failed to find the specified page");
}
// Creating headers if the table is empty
if (sheet.getLastColumn() === 0) {
populateHeaders(hook_load, sheet);
}
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
if (headers.length === 0 || headers[0] === "") {
populateHeaders(hook_load, sheet);
headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
}
// Duplicate check
var data = [];
var targetRow = -1;
var numRows = sheet.getLastRow() - 1;
if (keyColumns.length > 0 && numRows > 0) {
data = sheet.getRange(2, 1, numRows, sheet.getLastColumn()).getValues();
for (var r = 0; r < data.length; r++) {
var row = data[r];
var match = true;
for (var k = 0; k < keyColumns.length; k++) {
var key = keyColumns[k];
var idx = headers.indexOf(key);
if (idx === -1 || row[idx] != hook_load[key]) {
match = false;
break;
}
}
if (match) {
targetRow = r + 2; // header accounting
break;
}
}
}
// Preparing data for entry
var new_sheet_row = new Array(headers.length).fill('');
for (var h = 0; h < headers.length; h++) {
var key = headers[h];
if (key === 'timestamp') {
new_sheet_row[h] = new Date();
} else if (hook_load.hasOwnProperty(key)) {
new_sheet_row[h] = hook_load[key];
}
}
// Saving data to the table
if (targetRow > -1) {
sheet.getRange(targetRow, 1, 1, new_sheet_row.length).setValues([new_sheet_row]);
} else {
sheet.appendRow(new_sheet_row);
}
SpreadsheetApp.flush();
return sendResponse(true, "Data successfully saved");
} catch (error) {
return sendResponse(false, "Error saving data: " + error.message);
}
}
Didn't find the answer? Contact the support service via online chat or Telegram