I've tried to create a script that would take responsive ad customizer values from my csv file and update it. But after many tries it seems that I need some help. Could anyone review my code ant tell where the problem is? Or maybe anyone already solved this and have a functioning solution?
function main() {
// URL of the CSV file
var csvUrl = '
https://www.pasirinksparnus.lt/download/files/feeds/customizer_hotels.csv';
// Fetch and parse the CSV file
var response = UrlFetchApp.fetch(csvUrl);
var csvContent = response.getContentText();
var csvData = Utilities.parseCsv(csvContent);
// Extract headers and ensure 'Ad Group' is present
var headers = csvData[0];
var adGroupNameIndex = headers.indexOf('Ad Group');
if (adGroupNameIndex === -1) {
Logger.log('Ad Group column not found in the CSV file');
return;
}
// Create a map of ad customizer data
var customizerData = {};
for (var i = 1; i < csvData.length; i++) {
var row = csvData[i];
if (row.length !== headers.length) {
continue; // Skip malformed rows
}
var adGroupName = row[adGroupNameIndex];
var customizerValues = {};
for (var j = 0; j < headers.length; j++) {
if (j != adGroupNameIndex) {
var key = headers[j];
var value = row[j];
customizerValues[key] = value;
}
}
customizerData[adGroupName] = customizerValues;
}
// Iterate through ad groups and update ad customizers
var adGroups = AdsApp.adGroups().get();
while (adGroups.hasNext()) {
var adGroup = adGroups.next();
var adGroupName = adGroup.getName();
// Find the matching customizer data for the ad group
var customizerValues = customizerData[adGroupName];
if (customizerValues) {
// Assign customizer attributes to the responsive search ads
var ads = adGroup.ads().withCondition("Type = RESPONSIVE_SEARCH_AD").get();
while (ads.hasNext()) {
var ad = ads.next();
applyCustomParameters(ad, customizerValues);
}
}
}
}
function applyCustomParameters(ad, customizerValues) {
var customParameters = {};
for (var key in customizerValues) {
customParameters[key] = customizerValues[key];
}
ad.urls().setCustomParameters(customParameters);
}