Search code examples
google-apps-scriptlooker-studio

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


I am building a Community Connector between Google Data Studio and SpyFu.com, in order to funnel SEO information for a specific URL into the GDS Dashboard.

However, my getData() request only contains the first two fields from my Schema. As you can see, I have four listed in the code. The result is only the first two fields in the schema are printed to GDS.

I've been through tutorials, official documentation, YouTube videos, looked this issue up on google and checked out the community resources on 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
  };
}

I need the GDS to post four columns of data. They are, "Keyword", "Rank", "Local Monthly Searches" and "Global Monthly searches".

I cannot figure out how to create a "fixed schema" so that the system always prints these four columns of data at every request. The tutorials and various documentation say it's possible, but not how to do it. Please help!


Solution

  • The number of metrics initially called up by the Google Community Connector is handled from the front-end, via Google Data Studio.

    The back-end system (the Connector) only initially posts the default dimension and default metric. Getting the rest of the schemas to post should be handled when you are building a report on Google Data Studio. Simply click on the data set, select "data" on the right-hand menu, scroll down to either Metrics or Dimensions, and pick the ones you wish to add to the current set.

    Note that these are the fields you established earlier in the coding process, when you were setting up your schemas.