簡體   English   中英

如何通過第三方JavaScript訪問容器中的標簽列表?

[英]How do you access the list of tags in a container via 3rd party javascript?

試圖找到一種通過JavaScript列出GTM容器中所有標簽的方法。 我們希望將客戶的所有標簽導出到Google表格中,以便更好地處理清理他們的容器(目前有500多個標簽)。

我已經成功地將容器從GTM抓到表格中,但是根據一些Google搜索,該容器在其下方沒有標簽的屬性。

var gtmID;
var webPropertyID;
var name;
var triggerType;
var firingTrigger;
var lastEdited;
var clientID = 'CLIENT_ID';
var containerName = 'Customer - WEBSITES';
var workspaceName = 'Default Workspace';
var container;

var scopes=['https://www.googleapis.com/auth/tagmanager.manage.accounts',
      'https://www.googleapis.com/auth/tagmanager.edit.containers',
      'https://www.googleapis.com/auth/tagmanager.delete.containers',
      'https://www.googleapis.com/auth/tagmanager.edit.containerversions',
      'https://www.googleapis.com/auth/tagmanager.manage.users',
      'https://www.googleapis.com/auth/tagmanager.publish'];
var jsonObjectGTM=[];
var jsonObjectTags=[];

function controller() {
  gtmID = 'GTM_ID';
  listContainer(gtmID);
  listTags(jsonObjectGTM);
  recordTags(jsonObjectTags);
}

function checkAuth(immediate) {
  var authorizeCheckPromise = new Promise((resolve) => {
    gapi.auth.authorize(
                                          { client_id: clientID, scope: scopes.join(' '), immediate: immediate}, resolve);
  });
  authorizeCheckPromise
     .then(handleAuthResult)
     .then(loadTagManagerApi)
     .then(runTagManagerTask)
     .catch(() => {
         console.log('you must authorize any access to the api.');
}

//Check if the user has authoriation
function checkAuth() {
  checkAuth(true);
}

//Initiate auth flow in response to user clicking authorize button
function handleAuthclick(event) {
  checkAuth();
  return false;
}

//Handle response from authorization server
function handleAuthResult(authResult) {
  return new Promise((resolce, reject) => {
     var authorizeDiv = document.getElementById('authorize-div');
     if(authResult && !authResult.error) {
        //Hide auth UI, then load client library
        authorizeDiv.style.display = 'none';
        resolve();
     } else {
        //Show auth UI, allowing the user to initiate authorization by clicking authorize button
        authorizeDiv.style.display = 'inline';
        reject();
     }
  });
}

//load Tag Manager API client library
function loadTagManagerApi() {
  return new Promise((resolve, reject) => {
    console.log('Load Tag Manager api');
    gapi.client.load('tagmanager', 'v2', resolve);
  });
}

//Interacts with the tagmanager api v2 to grab the tags
function runTagManagerTask() {
  return new Promise((resolve, reject) => {
    console.log('Grabbing list of tags');
    container = findContainer(gtmID, containerName)
       .catch(handleError);
    resolve();
  }
}

//handles error
function handleError(error) {
  console.log('Error when interacting with GTM API');
  console.log(error);
}

//Wraps an API request in a promise
function reqestPromise(request) {
  return new Promise((resolve, reject) => {
     request.execute((response) => {
       if (response.code) {
          reject(response);
       }
       resolve(response);
     }
  }
}

//Returns the specified container
function findContainer(accountPath, containerName) {
  consle.log('Finding container in account:' + accountPath);
  var request = gapi.client.tagmanager.accounts.containers.list({'parent' : accountPath});

  return requestPromise(request)
    .then((response) => {
       var containers = response.container || [];
       var container = containers.find((container) => container.name === containerName);
       return container ||
          Promise.reject('Unable to find ' + containerName + ' container.');
    });
}

一旦我抓住它,只需要找到一種方法即可從容器中抓住標簽的數組/列表

感謝@Eike Pierstorff對Simo Ahava工具的推薦,它為該過程提供了相當多的幫助,它比任何腳本都要准確得多,而且速度可能更快。

但是就腳本而言,這是我的解決方案:

var name;
var tagType;
var firingTrigger;
var sheet;

var tagList = [];

function controller() {
  tagList = TagManager.Accounts.Containers.Workspaces.Tags.list('accounts/{accountID}/containers/{containerID}/workspaces/{workspaceID}');
  sheet = SpreadsheetApp.getActiveSpreadsheet();

  for (var i = 0;  i < tagList.tag.length; i++) {
    name = tagList.tag[i].name;
    firingTrigger = tagList.tag[i].firingTriggerId;
    tagType = tagList.tag[i].type;

    sheet.appendRow([name, tagType, firingTrigger]);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM