GCP/Apps Script

Apps Script๋กœ ์›น์‚ฌ์ดํŠธ์— ์—…๋กœ๋“œ ๋˜์–ด์žˆ๋Š”(a tag) CSV ํŒŒ์ผ์„ Google Sheets๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

whistory 2023. 3. 28. 10:55
๋ฐ˜์‘ํ˜•

๐Ÿ’ก Google Analytics ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ตœ์‹  ์ง€๋ฆฌ ์ •๋ณด ๋ฐ์ดํ„ฐ๋ฅผ Google Sheets๋กœ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค.
      ์ฃผ, ํ˜น์€ ์›”๋‹จ์œ„๋กœ ๋™๊ธฐํ™”๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค.

 

Google Developer ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ตœ์‹  ์ง€๋ฆฌ ์ •๋ณด ๋ฐ์ดํ„ฐ๋ฅผ Google Sheets๋กœ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค.

 

 

์ง€์—ญ ID  |  Analytics Measurement Protocol  |  Google Developers

์ด ๋ฌธ์„œ๋Š” ์œ ๋‹ˆ๋ฒ„์„ค ์• ๋„๋ฆฌํ‹ฑ์Šค๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Google ์• ๋„๋ฆฌํ‹ฑ์Šค 4๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ธก์ • ํ”„๋กœํ† ์ฝœ (Google ์• ๋„๋ฆฌํ‹ฑ์Šค 4)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. ์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜

developers.google.com

 

 

 

 

 

 

 

์ •๊ทœ์‹์„ ์ด์šฉํ•ด 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);
}

 

์ด์ œ ์ƒ์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ์ฃผ ๋‹จ์œ„ ํ˜น์€ ์›” ๋‹จ์œ„ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ๊ฑธ์–ด ๋†“์œผ๋ฉด,

์•Œ์•„์„œ ๋™๊ธฐํ™”๋ฅผ ์ง„ํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•