社区连接器getData()请求仅使用前两个架构字段,而不是全部四个

时间:2019-08-21 22:53:17

标签: api google-apps-script google-data-studio

我正在Google Data Studio和SpyFu.com之间建立社区连接器,以便将特定网址的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发布四列数据。它们是“关键字”,“排名”,“本地每月搜索”和“全球每月搜索”。

我无法弄清楚如何创建“固定模式”,因此系统总是在每次请求时都打印这四列数据。教程和各种文档都说有可能,但没有做到这一点。请帮忙!

2 个答案:

答案 0 :(得分:1)

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

后端系统(连接器)最初仅发布默认尺寸和默认指标。在Google Data Studio上生成报表时,应处理其余架构的发布。只需单击数据集,在右侧菜单上选择“数据”,向下滚动到“指标”或“维度”,然后选择要添加到当前集中的数据。

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

答案 1 :(得分:0)

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

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

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

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