首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >社区连接器getData()请求仅使用前两个架构字段,而不是所有四个

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

Stack Overflow用户
提问于 2019-08-22 06:53:18
回答 2查看 650关注 0票数 0

我正在建立一个在谷歌数据工作室和SpyFu.com之间的社区连接器,以便将搜索引擎优化的特定网址到GDS仪表板的信息。

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

我已经阅读了教程,官方文档,YouTube视频,在谷歌上查找了这个问题,并查看了GitHub上的社区资源。

代码语言:javascript
复制
//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发布四列数据。它们是“关键词”、“排名”、“本地月度搜索”和“全球月度搜索”。

我不知道如何创建一个“固定模式”,以便系统在每次请求时始终打印这四列数据。教程和各种文档都说这是可能的,但不是怎么做的。请帮帮我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-05 05:39:48

最初由Google Community Connector调用的指标数量是通过Google Data Studio从前端处理的。

后端系统(连接器)最初只发布默认维度和默认度量。当您在Google Data Studio上构建报告时,应该处理要发布的其余模式。只需单击数据集,选择右侧菜单上的" data“,向下滚动到Metrics或Dimensions,然后选择要添加到当前集的数据。

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

票数 1
EN

Stack Overflow用户

发布于 2021-07-08 01:01:47

在这里,您正在过滤定义的模式中的字段,这些字段出现在getData()接收的request对象上。

代码语言:javascript
复制
var requestedFieldIds = request.fields.map(function(field) {
    return field.name;
  });

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57600079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档