0% found this document useful (0 votes)
30 views2 pages

Cod 4

This document contains code to retrieve campaign spend data from the Google Ads API for a specified date range, format it, and update a Google Sheet with the data. It defines functions to get the campaign spend data from the API, format it, and update the sheet. It retrieves the data, parses the JSON response, extracts the needed fields, formats it as an array, and calls the function to update the sheet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Cod 4

This document contains code to retrieve campaign spend data from the Google Ads API for a specified date range, format it, and update a Google Sheet with the data. It defines functions to get the campaign spend data from the API, format it, and update the sheet. It retrieves the data, parses the JSON response, extracts the needed fields, formats it as an array, and calls the function to update the sheet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

function main() {

// Define the Google Sheet where you want to update the data.
var spreadsheetUrl =
'https://docs.google.com/spreadsheets/d/1qmnY5fkvOFgEZHqQ9UFlOmRRjC0yTFPZTJfBHYad3D
E/edit#gid=2125645366';

// Open the specified Google Sheet.


var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);

// Select the sheet where you want to update data.


var sheet = spreadsheet.getSheetByName('Google'); // Replace 'Google' with your
sheet's name.

// Get the current date.


var currentDate = new Date();

// Calculate the start and end dates for the reporting period (e.g., the last 24
hours).
var endDate = new Date();
var startDate = new Date();
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';
var apiUrl =
'https://googleads.googleapis.com/v9/customers/618-717-4174/googleAds:searchStream'
;

// Define 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.
};

// Fetch the campaign spend data.


var startDate = new Date(currentDate); // Clone the currentDate to startDate.
startDate.setDate(startDat.getDate() - 1); // Adjusted for the previous day.

// Clear existing data in the sheet (optional).


sheet.clear();

// Update the Google Sheet with the retrieved data.


updateSheetWithSpendData(sheet, campaignSpendData);
}

// Function to retrieve campaign spend data from the Google Ads API.
var campaignSpendData = getCampaignSpend(apiUrl, headers, requestBody);
// Make a POST request to the Google Ads API using headers.
var response = UrlFetchApp.fetch(apiUrl, {
'method': 'post',
'headers': headers,
'payload': JSON.stringify(requestBody),
});

// Parse the JSON response.


var responseData = JSON.parse(response.getContentText());

// 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.
}

// Function to update the Google Sheet with campaign spend data.


function updateSheetWithSpendData(sheet, data) {
// Define the header row.
var headers = ['Campaign Name', 'Spend'];

// Insert the headers into the first row of the sheet.


sheet.getRange(1, 1, 1, headers.length).setValues([headers]);

// Insert the data starting from the second row.


if (data.length > 0) {
sheet.getRange(2, 1, data.length, data[0].length).setValues(data);
}
}

You might also like