๋ฐ์ํ
๐ก Google Analytics ์์ ์ ๊ณตํ๋ ์ต์ ์ง๋ฆฌ ์ ๋ณด ๋ฐ์ดํฐ๋ฅผ Google Sheets๋ก ๊ฐ์ ธ์ค๊ณ ์ถ๋ค.
์ฃผ, ํน์ ์๋จ์๋ก ๋๊ธฐํ๋ฅผ ์ ์งํ๊ณ ์ถ๋ค.
Google Developer ์์ ์ ๊ณตํ๋ ์ต์ ์ง๋ฆฌ ์ ๋ณด ๋ฐ์ดํฐ๋ฅผ Google Sheets๋ก ๊ฐ์ ธ์ค๊ณ ์ถ๋ค.
์ ๊ท์์ ์ด์ฉํด latest csv ํ์ผ์ url์ ์ถ์ถํ๋ค.
function getGeoData() {
// ์ถ์ถ ์ ๋ณด
const url = 'https://developers.google.com/analytics/devguides/collection/protocol/v1/geoid';
const content = UrlFetchApp.fetch(url).getContentText();
// URL ์ถ์ถ์ถ ์ ๊ท์
const step1 = content.split("download_csv_of_geo_targets")[1];
const step2 = step1.match(/()/)[0];
const step3 = step2.match(/\\"(.*?)\\"/)[1];
const getUrl = `https://developers.google.com${step3}`
console.log(`latest csv : ${getUrl}`);
}
CSV ํ์ผ์ ์ฝ์ด ํ์ฑ ํ์ฌ Google Sheets์ ๋ฟ๋ ค์ค๋ค.
function getGeoData() {
// ์ถ์ถ ์ ๋ณด
const url = 'https://developers.google.com/analytics/devguides/collection/protocol/v1/geoid';
const content = UrlFetchApp.fetch(url).getContentText();
// URL ์ถ์ถ์ถ ์ ๊ท์
const step1 = content.split("download_csv_of_geo_targets")[1];
const step2 = step1.match(/()/)[0];
const step3 = step2.match(/\\"(.*?)\\"/)[1];
const getUrl = `https://developers.google.com${step3}`
console.log(`latest csv : ${getUrl}`);
const res = UrlFetchApp.fetch(getUrl);
const csvraw = res.getContentText();
const values = Utilities.parseCsv(csvraw);
const valuesSize = Object.keys(values).length;
// google sheets ์ ๋ณด
const sheetId = "google_sheet_id";
const sheetName = "geoData";
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
// ๋ฐ์ดํฐ ๋ฟ๋ฆฌ๊ธฐ
console.log("๋ฐ์ดํฐ ๋ฟ๋ ค์ฃผ๊ธฐ ์์");
sheet.getRange("A1:G"+valuesSize).setValues(values);
console.log("๋ฐ์ดํฐ ๋ฟ๋ ค์ฃผ๊ธฐ ๋");
}
Target Type์ด Country ์ Region์ธ ๋ฐ์ดํฐ๋ง ๋ณด์ฌ์ฃผ๊ณ ์ถ๊ธฐ ๋๋ฌธ์, ํํฐ๋ฅผ ์ ์ฉํด์ค๋ค.
function getGeoData() {
// ์ถ์ถ ์ ๋ณด
const url = 'https://developers.google.com/analytics/devguides/collection/protocol/v1/geoid';
const content = UrlFetchApp.fetch(url).getContentText();
// URL ์ถ์ถ์ถ ์ ๊ท์
const step1 = content.split("download_csv_of_geo_targets")[1];
const step2 = step1.match(/()/)[0];
const step3 = step2.match(/\\"(.*?)\\"/)[1];
const getUrl = `https://developers.google.com${step3}`
console.log(`latest csv : ${getUrl}`);
const res = UrlFetchApp.fetch(getUrl);
const csvraw = res.getContentText();
const values = Utilities.parseCsv(csvraw);
const valuesSize = Object.keys(values).length;
// google sheets ์ ๋ณด
const sheetId = "google_sheet_id";
const sheetName = "geoData";
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
const range = sheet.getRange("A:G");
// ๊ธฐ์กด ๋ฐ์ดํฐ ์ง์ฐ๊ธฐ
range.clearContent();
const getFilter = range.getFilter();
if ( getFilter != null ) {
// ๊ธฐ์กด ํํฐ ์ง์ฐ๊ธฐ
console.log("๊ธฐ์กด ํํฐ ์ง์ฐ๊ธฐ");
getFilter.remove();
}
// ๋ฐ์ดํฐ ๋ฟ๋ฆฌ๊ธฐ
console.log("๋ฐ์ดํฐ ๋ฟ๋ ค์ฃผ๊ธฐ ์์");
sheet.getRange("A1:G"+valuesSize).setValues(values);
console.log("๋ฐ์ดํฐ ๋ฟ๋ ค์ฃผ๊ธฐ ๋");
// ํํฐ ์ค์ ํ๊ธฐ
newFilter = range.createFilter();
const colInfo = 6;
const Filter_Criteria1 = SpreadsheetApp.newFilterCriteria().setHiddenValues(["City", "Airport", "Autonomous Community", "Borough", "Municipality", "Neighborhood", "Canton", "District", "City Region", "Province", "State", "Department", "Territory", "Union Territory", "Governorate", "Postal Code", "University", "TV Region", "Prefecture", "National Park", "Okrug", "Congressional District", "County"]);
newFilter.setColumnFilterCriteria(colInfo, Filter_Criteria1);
}
์ด์ ์์ฑํ ํจ์๋ฅผ ์ฃผ ๋จ์ ํน์ ์ ๋จ์ ํธ๋ฆฌ๊ฑฐ๋ฅผ ๊ฑธ์ด ๋์ผ๋ฉด,
์์์ ๋๊ธฐํ๋ฅผ ์งํํ ๊ฒ์ด๋ค.
๋ฐ์ํ