|
function onOpen() { |
|
var ui = SpreadsheetApp.getUi(); |
|
ui.createMenu('Search Console GC Extension') |
|
.addItem('Get Data by Month', 'getDataByMonth') |
|
.addToUi(); |
|
} |
|
|
|
function getDataByMonth() { |
|
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('raw_kw'); |
|
var urls = sheet.getRange("B2:B" + sheet.getLastRow()).getValues().flat().filter(String); |
|
|
|
var ui = SpreadsheetApp.getUi(); |
|
var siteUrl = ui.prompt('Enter site URL (e.g. www.example.com):').getResponseText(); |
|
var startDate = ui.prompt('Enter start date (yyyy-mm-dd):').getResponseText(); |
|
|
|
Logger.log('Site URL: ' + siteUrl); |
|
Logger.log('Start Date: ' + startDate); |
|
|
|
var oauthToken = ScriptApp.getOAuthToken(); |
|
var currentDate = new Date(startDate); |
|
var today = new Date(); |
|
|
|
// Obtener o crear la hoja kw_by_url |
|
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); |
|
var activeSheet = spreadsheet.getSheetByName('kw_by_url') || spreadsheet.insertSheet('kw_by_url'); |
|
activeSheet.clear(); // Clear any existing content |
|
activeSheet.getRange(1, 1).setValue("URL"); |
|
activeSheet.getRange(1, 2).setValue("Periodo"); |
|
activeSheet.getRange(1, 3).setValue("Queries"); |
|
activeSheet.getRange(1, 4).setValue("Posiciones"); |
|
|
|
var row = 2; // Start from the second row |
|
|
|
while (currentDate <= today) { |
|
var monthStart = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1); |
|
var monthEnd = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0); |
|
|
|
var formattedStartDate = Utilities.formatDate(monthStart, Session.getScriptTimeZone(), 'yyyy-MM-dd'); |
|
var formattedEndDate = Utilities.formatDate(monthEnd, Session.getScriptTimeZone(), 'yyyy-MM-dd'); |
|
|
|
Logger.log('Fetching data for: ' + formattedStartDate + ' to ' + formattedEndDate); |
|
|
|
var periodLabel = Utilities.formatDate(monthStart, Session.getScriptTimeZone(), 'yyyy/MM'); |
|
|
|
urls.forEach(function(url) { |
|
if (url) { |
|
var apiUrl = 'https://www.googleapis.com/webmasters/v3/sites/' + encodeURIComponent('https://' + siteUrl) + '/searchAnalytics/query'; |
|
|
|
var payload = { |
|
startDate: formattedStartDate, |
|
endDate: formattedEndDate, |
|
dimensions: ["query", "page"], |
|
dimensionFilterGroups: [{ |
|
filters: [{ |
|
dimension: "page", |
|
expression: url |
|
}] |
|
}], |
|
rowLimit: 1000 |
|
}; |
|
var headers = { |
|
'Authorization': 'Bearer ' + oauthToken, |
|
'Content-Type': 'application/json' |
|
}; |
|
var options = { |
|
headers: headers, |
|
method: 'POST', |
|
payload: JSON.stringify(payload), |
|
muteHttpExceptions: true |
|
}; |
|
|
|
var response = UrlFetchApp.fetch(apiUrl, options).getContentText(); |
|
Logger.log('API Response: ' + response); |
|
|
|
var json = JSON.parse(response); |
|
var queries = []; |
|
var positions = []; |
|
|
|
if (json.rows) { |
|
json.rows.forEach(row => { |
|
queries.push(row.keys[0]); |
|
positions.push(row.position); |
|
}); |
|
} |
|
|
|
var queriesString = queries.join(', '); |
|
var positionsString = positions.join(', '); |
|
|
|
// Escribir los datos en la hoja kw_by_url |
|
activeSheet.getRange(row, 1).setValue(url); // URL |
|
activeSheet.getRange(row, 2).setValue(periodLabel); // Periodo |
|
activeSheet.getRange(row, 3).setValue(queriesString); // Queries |
|
activeSheet.getRange(row, 4).setValue(positionsString); // Posiciones |
|
|
|
row++; // Avanzar a la siguiente fila |
|
} |
|
}); |
|
|
|
currentDate.setMonth(currentDate.getMonth() + 1); |
|
} |
|
|
|
ui.alert('Data by month and URL has been added to the kw_by_url sheet.'); |
|
} |