Connect your scripts with Google Sheets for data import/export, dynamic configuration, and collaborative campaign management.
// Basic Google Sheets integration example
function exportCampaignData() {
const SHEET_URL = 'https://docs.google.com/spreadsheets/d/YOUR_SHEET_ID/edit';
const sheet = SpreadsheetApp.openByUrl(SHEET_URL).getActiveSheet();
// Clear existing data (keeping headers)
const lastRow = sheet.getLastRow();
if (lastRow > 1) {
sheet.getRange(2, 1, lastRow - 1, sheet.getLastColumn()).clear();
}
// Set headers if first run
if (sheet.getLastRow() === 0) {
sheet.getRange(1, 1, 1, 6).setValues([[
'Campaign Name', 'Status', 'Budget', 'Cost', 'Conversions', 'ROAS'
]]);
}
// Export campaign data
const campaigns = AdsApp.campaigns().get();
const data = [];
while (campaigns.hasNext()) {
const campaign = campaigns.next();
const stats = campaign.getStatsFor('LAST_7_DAYS');
data.push([
campaign.getName(),
campaign.isEnabled() ? 'Active' : 'Paused',
campaign.getBudget(),
stats.getCost(),
stats.getConversions(),
stats.getConversions() > 0 ? stats.getConversionValue() / stats.getCost() : 0
]);
}
// Write data to sheet
if (data.length > 0) {
sheet.getRange(2, 1, data.length, 6).setValues(data);
}
Logger.log(`Exported ${data.length} campaigns to Google Sheets`);
}This example shows how to export campaign performance data to a Google Sheet for analysis and reporting.
// Read configuration from Google Sheets
function getConfigFromSheet() {
const CONFIG_SHEET_URL = 'https://docs.google.com/spreadsheets/d/YOUR_CONFIG_SHEET_ID/edit';
const sheet = SpreadsheetApp.openByUrl(CONFIG_SHEET_URL).getSheetByName('Config');
const config = {};
const data = sheet.getDataRange().getValues();
// Convert sheet data to config object
for (let i = 1; i < data.length; i++) {
const key = data[i][0];
const value = data[i][1];
config[key] = value;
}
return config;
}
function main() {
// Load dynamic configuration
const config = getConfigFromSheet();
const TARGET_ROAS = config.targetRoas || 3.0;
const MAX_BUDGET_INCREASE = config.maxBudgetIncrease || 0.2;
const MIN_CONVERSIONS = config.minConversions || 5;
Logger.log(`Using config: ROAS=${TARGET_ROAS}, Budget=${MAX_BUDGET_INCREASE}, Min Conv=${MIN_CONVERSIONS}`);
// Use configuration in optimization logic
const campaigns = AdsApp.campaigns()
.withCondition('Status = ENABLED')
.withCondition(`Conversions >= ${MIN_CONVERSIONS} DURING LAST_30_DAYS`)
.get();
while (campaigns.hasNext()) {
const campaign = campaigns.next();
const stats = campaign.getStatsFor('LAST_30_DAYS');
const roas = stats.getConversionValue() / stats.getCost();
if (roas >= TARGET_ROAS) {
const currentBudget = campaign.getBudget();
const newBudget = currentBudget * (1 + MAX_BUDGET_INCREASE);
campaign.setBudget(newBudget);
Logger.log(`Increased budget for ${campaign.getName()}: €${currentBudget} → €${newBudget}`);
}
}
}Create a "Config" sheet with two columns: "Setting" and "Value"