未分類

GTMのタグ一覧取得 Tag Manager APIで


function GTMListAccounts() {

  var tag_list = [];

  accounts = TagManager.Accounts.list();

  // account
  for (let i = 0; i < accounts.account.length; i++) {
    const account_name = accounts.account[i].name;
    const account_id = accounts.account[i].accountId;
    const account_path = accounts.account[i].path;

    // container
    containers = TagManager.Accounts.Containers.list(account_path,{fields: 'container(*)'}).container;  // pythonの書き方でかける(fileldsで何を取ってくるか指定)    
    for(let j = 0; j < containers.length ; j++){
      const gtm_id = containers[j].publicId;
      const container_name = containers[j].name;
      const container_id = containers[j].containerId;
      const container_path = containers[j].path;

      workspaces = TagManager.Accounts.Containers.Workspaces.list(container_path);
      
      // tag一覧取得
      for(let k = 0; k < workspaces.workspace.length; k++){
        var workspace_id = workspaces.workspace[k].workspaceId;
        var workspace_name = workspaces.workspace[k].name
        var workspace_path = workspaces.workspace[k].path;
        tags = TagManager.Accounts.Containers.Workspaces.Tags.list(workspace_path).tag;

        // tag一覧取得
        if(tags !== undefined && workspace_name == "Default Workspace"){
          for(let l = 0; l < tags.length; l++){
            var tag_name = tags[l].name;
            
            tag_list.push(new GTM_11(
              account_id,
              account_name,
              container_id,
              container_name,
              workspace_name,
              tag_name
              ))
         
          }
        }
      }

      Utilities.sleep(10000);
    }

  }

  return tag_list;
}


/*****************************************
 * GA4のオブジェクトインスタンスを作成
 *****************************************/
class GTM_11{
  constructor(account_id, account_name, container_id, container_name, workspace_name, tag_name){
    this.account_id = account_id;
    this.account_name = account_name;
    this.container_id = container_id;
    this.container_name = container_name;
    this.workspace_name = workspace_name;
    this.tag_name = tag_name;
  }
}

/***********************
 * スプレッドシート書き込み
 ***********************/
function writeReport3() {
  
  const sheet_name = "GTM_タグ一覧"
  var user_list = GTMListAccounts();
  
  try{
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name);

    // 書き込み処理
    const header = [["アカウントID", "アカウント名", "コンテナID", "コンテナ名", "ワークスペース名","タグ名"]];
    sheet.getRange(1, 1, 1, 6).setValues(header);
    for(i = 0; i < user_list.length ; i++){
      sheet.getRange(i+2, 1, 1, 6).setValues(
       [[user_list[i].account_id,
        user_list[i].account_name,
        user_list[i].container_id,
        user_list[i].container_name,
        user_list[i].workspace_name,
        user_list[i].tag_name]]
      );
    }
  
    Logger.log(`Report spreadsheet created: ${sheet.getSheetName()}`);
  } catch (e) {
    // TODO (Developer) - Handle exception
    Logger.log(`Failed to write report with error: ${e}`);
  }  
}



-未分類

© 2024 Yosshi Labo. Powered by AFFINGER5