Cod 2
Cod 2
Cod 2
// Define the Google Sheet where you want to update the data.
var spreadsheetUrl =
'https://docs.google.com/spreadsheets/d/1qmnY5fkvOFgEZHqQ9UFlOmRRjC0yTFPZTJfBHYad3D
E/edit#gid=2125645366';
// Calculate the start and end dates for the reporting period (e.g., the last 24
hours).
var endDate = new Date(25/09/2023); // Set it to the current date.
var startDate = new Date(24/09/2023); // Set it to the start of the previous day.
startDate.setDate(currentDate.getDate() - 1); // Adjusted for the previous day.
// Fetch the campaign spend data using the Google Ads API and your JWT token.
var jwtToken = 'jtKt1g1awWhCNRGCUJs8Uw'; // Replace with your actual JWT token.
var campaignSpendData = getCampaignSpend(startDate, endDate,
jtKt1g1awWhCNRGCUJs8Uw);
// Function to retrieve campaign spend data from the Google Ads API using JWT
token.
function getCampaignSpend(startDate, endDate, jtKt1g1awWhCNRGCUJs8Uw) {
// Use the JWT token for authentication with the Google Ads API.
// Make API requests to fetch campaign spend data.
// Function to retrieve campaign spend data from the Google Ads API using JWT
token.
function getCampaignSpend(startDate, endDate, jtKt1g1awWhCNRGCUJs8Uw) {
// Define the URL of the Google Ads API endpoint for the report you need.
var apiUrl =
'https://googleads.googleapis.com/v9/customers/YOUR_CUSTOMER_ID/googleAds:searchStr
eam';
// Set up the headers for the API request, including authorization with the JWT
token.
var headers = {
'Authorization': 'Bearer ' + jwtToken,
'Content-Type': 'application/json',
};
// Define the request parameters, such as the query and date range.
var requestBody = {
'query': 'SELECT campaign.id, campaign.name, metrics.impressions,
metrics.clicks, metrics.cost_micros ' +
'FROM keyword_view ' +
'WHERE segments.date DURING ' + startDate.toISOString() + ',' +
endDate.toISOString(),
'pageSize': 1000, // Adjust as needed.
};
// Extract the data you need from the API response and format it as an array.
var spendData = [];
for (var i = 0; i < responseData.length; i++) {
var row = responseData[i];
spendData.push([row.campaign.name, row.metrics.cost_micros / 1000000]); //
Convert micros to dollars.
}
return spendData;
}
}
}