const yesterday = new Date(); yesterday.setDate(yesterday.getDate() – 1); var dateString = yesterday.toString() var year = dateString.split(‘ ‘).pop(); var month = (yesterday.getMonth() + 1).toString().padStart(2, ‘0’); var day = yesterday.getDate().toString().padStart(2, ‘0’); var formattedYesterdayDate = `${year}-${month}-${day}`; log.info(formattedYesterdayDate); // Define the Yandex Metrica API credentials var refresh_token = ‘1:DC1892xxxxxxxxxxxxxxxxxxxxxxxxCmKM6mKL1VZtxxxxxx-xekEUPfyTB5WHXe61MgPomf97zBt_kVlnhghTbw:yYcaLvdWFvvBtk8h9tqwhA’; // Replace with your Yandex refresh token var access_token = ‘y0_AgAAAABxxxxxxxxxxxxxxxxxxxxxxxxxxHkaxoZLaqft5jL9UU3pOH4ToA’; // Replace with your Yandex access token var oauth_url = ‘https://oauth.yandex.com/token’; var generate_report_url = “https://api-metrika.yandex.net/management/v1/counter/9xxxx472/logrequests?date1=2024-07-29&date2=”+formattedYesterdayDate+”&source=hits&fields=ym:pv:watchID,ym:pv:counterID,ym:pv:date,ym:pv:dateTime,ym:pv:title,ym:pv:URL,ym:pv:referer,ym:pv:UTMCampaign,ym:pv:UTMContent,ym:pv:UTMMedium,ym:pv:UTMSource,ym:pv:UTMTerm,ym:pv:browser,ym:pv:browserMajorVersion,ym:pv:browserMinorVersion,ym:pv:browserCountry,ym:pv:browserEngine,ym:pv:browserEngineVersion1,ym:pv:browserEngineVersion2,ym:pv:browserEngineVersion3,ym:pv:browserEngineVersion4,ym:pv:browserLanguage,ym:pv:clientTimeZone,ym:pv:cookieEnabled,ym:pv:deviceCategory,ym:pv:from,ym:pv:hasGCLID,ym:pv:GCLID,ym:pv:ipAddress,ym:pv:javascriptEnabled,ym:pv:mobilePhone,ym:pv:mobilePhoneModel,ym:pv:openstatAd,ym:pv:openstatCampaign,ym:pv:openstatService,ym:pv:openstatSource,ym:pv:operatingSystem,ym:pv:operatingSystemRoot,ym:pv:physicalScreenHeight,ym:pv:physicalScreenWidth,ym:pv:regionCity,ym:pv:regionCountry,ym:pv:regionCityID,ym:pv:regionCountryID,ym:pv:screenColors,ym:pv:screenFormat,ym:pv:screenHeight,ym:pv:screenOrientation,ym:pv:screenWidth,ym:pv:windowClientHeight,ym:pv:windowClientWidth,ym:pv:lastTrafficSource,ym:pv:lastSearchEngine,ym:pv:lastSearchEngineRoot,ym:pv:lastAdvEngine,ym:pv:artificial,ym:pv:pageCharset,ym:pv:isPageView,ym:pv:isTurboPage,ym:pv:isTurboApp,ym:pv:link,ym:pv:download,ym:pv:notBounce,ym:pv:lastSocialNetwork,ym:pv:httpError,ym:pv:clientID,ym:pv:counterUserIDHash,ym:pv:networkType,ym:pv:lastSocialNetworkProfile,ym:pv:goalsID,ym:pv:shareService,ym:pv:shareURL,ym:pv:shareTitle,ym:pv:iFrame,ym:pv:recommendationSystem,ym:pv:messenger,ym:pv:parsedParamsKey1,ym:pv:parsedParamsKey2,ym:pv:parsedParamsKey3,ym:pv:parsedParamsKey4,ym:pv:parsedParamsKey5,ym:pv:parsedParamsKey6,ym:pv:parsedParamsKey7,ym:pv:parsedParamsKey8,ym:pv:parsedParamsKey9,ym:pv:parsedParamsKey10”; var client_id = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’; // Replace with your Yandex client ID var client_secret = ‘xxxxxxxxxxxxxxxxxxxxxxxxx’; // Replace with your Yandex client secret var base_url = ‘https://api-metrika.yandex.net/management/v1/counter’ // Function to build URL with query parameters function makeUrl(input_url, params) { var url = input_url + ‘?’; for (var key in params) { url = url + key + ‘=’ + encodeURIComponent(params[key]) + ‘&’; } return url.slice(0, -1); // Remove trailing ‘&’ } // Function to refresh the access token function refreshToken() { var params = { “refresh_token”: refresh_token, “client_id”: client_id, “client_secret”: client_secret, “grant_type”: “refresh_token” }; var url = makeUrl(oauth_url, params); var result = rest.post(url, ‘object’); if (result.code === 200) { access_token = result.data.access_token; } else { var msg = ‘======, refreshToken fail, Result is: ‘ + JSONUtil.obj2JsonPretty(result); throw new Error(msg); } } function getHeaders() { return { “Authorization”: “OAuth ” + access_token, // “Content-Type”: “application/json;charset=utf-8”, “Accept-Encoding”: “gzip, deflate, br”, }; } var headers = getHeaders(); var request_id = “”; var result = rest.post(generate_report_url, {}, headers, ‘object’); log.info(“Report Result”+ result) if (result.code === 200) { request_id = result.data.log_request.request_id; log.info(“request_id”+ request_id) // return request_id; } else { var msg = ‘======, Report not generated, Result is: ‘ + JSONUtil.obj2JsonPretty(result); throw new Error(msg); } // Function to check the status of the report function checkReportStatus() { var status_url = “https://api-metrika.yandex.net/management/v1/counter/97xxxxx72/logrequest/” + request_id; var result = rest.get(status_url, getHeaders(), ‘object’); log.info(“Status Check Result: ” + JSON.stringify(result)); if (result.code === 200) { var report_status = result.data.log_request.status; return report_status } else { var msg = ‘======, Failed to check status, Result is: ‘ + JSONUtil.obj2JsonPretty(result); throw new Error(msg); } } function csvToObjectArray(csvString) { // 将 CSV 字符串按行拆分 const lines = csvString.trim().split(‘\n’); // 解析第一行,作为对象的键 const headers = lines[0].split(‘\t’); // 遍历剩余的行,解析每一行并生成对象 const objects = lines.slice(1).map(line => { const values = line.split(‘\t’); const obj = {}; headers.forEach((header, index) => { obj[(“”+header).trim().replace(“ym:pv:”, “”)] = (“”+values[index]).trim(); }); return obj; }); return objects; } let report_status_after_report_creation; do { report_status_after_report_creation = checkReportStatus(); // log.info(“report Status in while loop”+ report_status_after_report_creation) } while(report_status_after_report_creation!=’processed’); var r = rest.get(base_url+”/9xxxxx2/logrequest/” + request_id +”/part/0/download”, headers,’string’); result = “” + r; let index = result.indexOf(“data”); let res = result.substring(index + 5, result.length – index); if (res.indexOf(“}”) != -1) { res = res.substring(0, result.lengtn-1); } var jsonData = csvToObjectArray(res); for (let i = 0 ; i < jsonData.length; i++){ core.push(jsonData[i]); }