Community Connector getData() 请求仅使用前两个架构字段,而不是全部四个

Community Connector getData() Request only uses the first two schema fields, not all four

我正在 Google Data Studio 和 SpyFu.com 之间构建一个社区连接器,以便将特定 url 的 SEO 信息汇集到 GDS 仪表板中。

但是,我的 getData() 请求仅包含我的架构中的前两个字段。如您所见,我在代码中列出了四个。结果是只有模式中的前两个字段被打印到 GDS。

我浏览了教程、官方文档、YouTube 视频,在 google 上查看了这个问题,并在 GitHub 上查看了社区资源。

//Step Two: Define getConfig()
function getConfig(request) {
  var cc = DataStudioApp.createCommunityConnector();
  var config = cc.getConfig();

  config.newInfo()
    .setId('instructions')
    .setText('Give me SpyFu information on the following domain:');

  config.newTextInput()
    .setId('domain')
    .setName('Enter the domain to search')
    .setHelpText('e.g. ebay.com')
    .setPlaceholder('ebay.com');

  config.newTextInput()
    .setId('SECRET_KEY')
    .setName('Enter your API Secret Key')
    .setHelpText('e.g. A1B2C3D4')
    .setPlaceholder('A1B2C3D4'); 

  config.setDateRangeRequired(false);

  return config.build();

}

//Step Three: Define getSchema()
function getFields(request) {
  var cc = DataStudioApp.createCommunityConnector();
  var fields = cc.getFields();
  var types = cc.FieldType;
  var aggregations = cc.AggregationType;

  fields.newDimension()
    .setId('Keyword')
    .setName('Keywords')
    .setDescription('The keywords most often attributed to this domain.')
    .setType(types.TEXT);

  fields.newMetric()
    .setId('Rank')
    .setName('Rankings')
    .setDescription('The ranking of the target site keyword on the Google Search Page.')
    .setType(types.NUMBER);

  fields.newMetric()
    .setId('Local_Monthly_Searches')
    .setName('Local Searches per Month')
    .setDescription('Number of times, locally, that people have searched for this term within in the last month.')
    .setType(types.NUMBER);

  fields.newMetric()
    .setId('Global_Monthly_Searches')
    .setName('Global Searches per Month')
    .setDescription('Number of times, globally, that people have searched for this term within in the last month.')
    .setType(types.NUMBER);

  return fields;
}

function getSchema(request) {
  var fields = getFields(request).build();
  return { schema: fields };
}

//Step Four: Define getData()
function responseToRows(requestedFields, response, domain) {
  // Transform parsed data and filter for requested fields

  return response.map(function(Array) {
    var row = [];
    requestedFields.asArray().forEach(function (field) {
      switch (field.getId()) {
        case 'Keyword':
          return row.push(Array.term);
        case 'Rank':
          return row.push(Array.position);
        case 'Local_Monthly_Searches':
          return row.push(Array.exact_local_monthly_search_volume);
        case 'Global_Monthly_Searches':
          return row.push(Array.exact_global_monthly_search_volume);  
        case 'domain':
          return row.push(domain);
        default:
          return row.push('');
      }
    });
    return { values: row };
  });
}

function getData(request) {
  console.log("Request from Data Studio");
  console.log(request);

  var requestedFieldIds = request.fields.map(function(field) {
    return field.name;
  });

  var requestedFields = getFields().forIds(requestedFieldIds);

  // Fetch data from API  
  var url = [
    'https://www.spyfu.com/apis/url_api/organic_kws?q=' 
    + request.configParams.domain
    + '&r=20'
    + '&p=[1 TO 10]'
    + '&api_key='
    + request.configParams.SECRET_KEY,
  ];

try {   
  var response = UrlFetchApp.fetch(url.join(''));
} catch (e) {
  DataStudioApp.createCommunityConnector()  
    .newUserError()
    .setDebugText('Failed URL Fetch Attempt. Exception details: ' + e)
    .setText('There was an error accessing this domain. Try again later, or file an issue if this error persists.')
    .throwException();
  } 

console.log("Response from API");
console.log(response);

  //Parse data from the API

try { 
  var parsedResponse = JSON.parse(response);
} catch (e) {
  DataStudioApp.createCommunityConnector()  
    .newUserError()
    .setDebugText('Error parsing the JSON data. Exception details: ' + e)
    .setText('There was an error parsing the JSON data. Try again later, or file an issue if this error persists.')
    .throwException();
  }

  var rows = responseToRows(requestedFields, parsedResponse);
  return {
    schema: requestedFields.build(),
    rows: rows
  };
}

我需要 GDS post 四列数据。它们是 "Keyword"、"Rank"、"Local Monthly Searches" 和 "Global Monthly searches"。

我不知道如何创建一个 "fixed schema" 以便系统在每次请求时始终打印这四列数据。教程和各种文档说这是可能的,但没有说如何去做。请帮忙!

Google 社区连接器最初调用的指标数量是通过 Google Data Studio 从前端处理的。

后端系统(连接器)最初只是 post 默认维度和默认指标。当您在 Google Data Studio 上构建报告时,应该处理将其余架构获取到 post。只需单击数据集,在右侧菜单上单击 select "data",向下滚动到“指标”或“维度”,然后选择您希望添加到当前集的那些。

请注意,这些是您之前在编码过程中设置架构时建立的字段。

在这里,您正在为 getData() 收到的 request 对象上存在的字段过滤定义的架构。

var requestedFieldIds = request.fields.map(function(field) {
    return field.name;
  });

var requestedFields = getFields().forIds(requestedFieldIds);

Google Data Studio 中作为请求催化剂的可视化将决定请求哪些字段。