function main() {
// Define the date range for retrieving call data
var startDate = getFormattedDate(30); // 30 days ago
var endDate = getFormattedDate(0); // today
// Construct the query string with proper date formatting
var query = 'SELECT CallStartTime, CampaignName, CallerCountryCode, CallDurationSeconds, CallStatus ' +
'FROM CALL_METRICS_CALL_DETAILS_REPORT ' +
'WHERE CallStartTime >= "' + startDate + '" AND CallStartTime <= "' + endDate + '"';
// Query to get call details
var report = AdsApp.report(query);
var rows = report.rows();
// Check if there are any rows to process
if (!rows.hasNext()) {
Logger.log('No call data found for the specified date range.');
return;
}
while (rows.hasNext()) {
var row = rows.next();
var callData = {
startTime: row['CallStartTime'],
campaign: row['CampaignName'],
countryCode: row['CallerCountryCode'],
duration: row['CallDurationSeconds'],
status: row['CallStatus']
};
// Send data to the webhook URL
sendToWebhook(callData);
}
}
function sendToWebhook(data) {
var url = '
https://services.leadconnectorhq.com/hooks/GWgJnGNYokMRdIAB4fm2/webhook-trigger/9ea53395-eb21-4cb4-9752-1c59033b44ca'; // Replace with your GoHighLevel webhook URL
var options = {
method: 'POST',
contentType: 'application/json',
payload: JSON.stringify(data)
};
try {
var response = UrlFetchApp.fetch(url, options);
Logger.log('Data sent to webhook: ' + response.getContentText());
} catch (e) {
Logger.log('Error sending data to webhook: ' + e.toString());
}
}
function getFormattedDate(daysAgo) {
var date = new Date();
date.setDate(date.getDate() - daysAgo);
var year = date.getFullYear();
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var day = ('0' + date.getDate()).slice(-2);
return year + '-' + month + '-' + day + 'T00:00:00'; // Format as 'YYYY-MM-DDTHH:MM:SS'
}
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure, or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. Thank you for respecting the privacy and security of the information contained in this email. Digital Presence Matters™.