Answer the question
In order to leave comments, you need to log in
How to generate an API request if some of the parameters are static and some are dynamic?
You need to make a script for googledoc that would send a GET request to the Ya.Metrika API, in which half of the parameters are static, the other half are dynamic and are specified as arguments to a custom function.
I tried just a string expression, into which I inserted dynamic parameters using template literals, but it didn’t even work. Although theoretically, the request should have been formed correctly. In JS, I don’t rummage from the word at all. Is there a good way?
Current non-working variant:
function GetMetrika(date1,date2,url3) {
var metrika_api_base_url = 'https://api-metrika.yandex.ru/stat/v1/data?oauth_token=<мой токен>&ids=<id моего счётчика>&date1=${date1}&date2=${date2}&dimensions=ym%3As%3AsearchEngine&metrics=ym%3As%3Ausers&filters=ym%3As%3AstartURLPath%3D%3D${url3}&limit=10000';
metrika_api_base_url = metrika_api_base_url;
Logger.log(metrika_api_base_url);
var responseJson=UrlFetchApp.fetch(metrika_api_base_url).getContentText();
var response = JSON.parse(responseJson);
Logger.log (responseJson);
return response;
}
var responseJson=UrlFetchApp.fetch(metrika_api_base_url).getContentText();
Answer the question
In order to leave comments, you need to log in
A good way to work with URL parameters is URLSearchParams
, which automatically encodes invalid URL characters and glues all parameters into one string:
var params = new URLSearchParams();
params.append('token', 'a b c');
params.append('limit', 100);
var baseApiUrl = 'https://api-metrika.yandex.ru/stat/v1/data';
console.log(baseApiUrl + '?' + params.toString());
// → https://api-metrika.yandex.ru/stat/v1/data?token=a+b+c&limit=100
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question