Data Studio社区连接器

时间:2018-08-11 19:19:04

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

我试图通过创建社区连接器将关键字排名数据拉入Data Studio。我正在使用的API方法可在以下位置找到:

https://www.rankranger.com/documentation/api-method-rank

基本上,我正在尝试使用其他API复制Google Code Labs教程以及此similar script-但是,当我尝试通过清单进行发布时,我什至无法连接到Data Studio

我想知道是否有人有经验-或者可以告诉我我要去哪里错了?

// Get Config Parameters

function getConfig(request) {
  var config = {
    "configParams": [
      {
        "type": "INFO",
        "name": "welcomeMessage",
        "text": "Full instructions tbc"
      },
      {
        "type": "TEXTINPUT",
        "name": "apiKey",
        "displayName": "API Key 1",
        "helpText": "You'll be provided with this :)",
        "placeholder": "API Key"
      },
      {
        "type": "TEXTINPUT",
        "name": "campaignId",
        "displayName": "Campaign ID",
        "helpText": "We need to retrieve this through the API",
        "placeholder": "Campaign ID"
      },
      {
        "type": "TEXTINPUT",
        "name": "domain",
        "displayName": "Root Domain",
        "helpText": "The root domain of the site you want rankings for.",
        "placeholder": "root domain"
      }
    ],
    dateRangeRequired: true
  };
  return config;
}

// Set the schema for the fields we'll need the data for

var dataSchema = [
 {
    name: 'date',
    label: 'Date',
    description: 'Date of parsedResponse to select in YYYY-MM-DD',
    group: 'Date',
    dataType: 'STRING',
    semantics: {
      conceptType: 'DIMENSION',
      semanticType: 'YEAR_MONTH_DAY',
      semanticGroup: 'DATE'
    }
  },
  {
    name: 'url',
    label: 'Domain',
    dataType: 'STRING',
    semantics: {
    conceptType: 'DIMENSION'
    }
  },
  {
    name: 'lp',
    label: 'Landing Page',
    dataType: 'URL',
    semantics: {
      conceptType: 'DIMENSION'
    }
  },
  {
    name: 'keyword',
    label: 'Keyword',
    dataType: 'STRING',
    semantics: {
      conceptType: 'DIMENSION'
    }
  },
  {
    name: 'se',
    label: 'Search Engine',
    dataType: 'STRING',
    semantics: {
      conceptType: 'DIMENSION'
    }
  },
  {
    name: 'se_name',
    label: 'Sarch Engine Name',
    dataType: 'STRING',
    semantics: {
      conceptType: 'DIMENSION'
    }
  },
  {
    name: 'rank',
    label: 'Rank',
    dataType: 'NUMBER',
    semantics: {
      conceptType: 'METRIC',
      isReaggregatable: false
    }
  }
];

// Function to return the data schema

function getSchema(request) {
  return {schema: dataSchema};
}

// OAuth function that we don't need

function getAuthType() {
  var response = {
    "type": "NONE"
  };
  return response;
}

// Most important - the get data request

function getData(request) {
  // Create schema for requested fields
  var requestedSchema = request.fields.map(function (field) {
    for (var i = 0; i < dataSchema.length; i++) {
      if (dataSchema[i].name == field.name) {
        return dataSchema[i];
      }
    }
  });

    var url = [
    'https://www.rankranger.com/api/v2/?rank&key=',
    request.configParams.apiKey,
    '&start_date=',
    request.dateRange.startDate,
    '&end_date=',
    request.dateRange.endDate,
    '&campaign_id=',
    request.configParams.campaignId,
    '&domain=',
    request.configParams.domain,
    '&output=json'
  ];

  var response = UrlFetchApp.fetch(url.join(''));
  var parsedResponse = JSON.parse(response.getContentText());


  // Prepare the tabular data.
  var requestedData = parsedResponse.map(function(rankings) {
    var values = [];
    // Provide values in the order defined by the schema.
    dataSchema.forEach(function(field) {
      switch(field.name) {
          case 'date':
            values.push(rankings.date.replace(/-/g, ''));
            break;
          case 'url':
            values.push(rankings.url);
            break;
          case 'lp':
            values.push(rankings.lp);
            break;
          case 'keyword':
            values.push(rankings.keyword);
            break;
          case 'se':
            values.push(rankings.se);
            break;
          case 'se_name':
            values.push(rankings.se_name);
            break;
          case 'rank':
            values.push(rankings.rank || 101);
            break;
      }
    });
    return {values: values};
  });
    // return the data for the request
    return {
    schema: requestedSchema,
    rows: requestedData
  };
}

1 个答案:

答案 0 :(得分:0)

您不应在“ configParams”中使用引号。看起来应该是这样;

var config = { configParams: [ { "type": "INFO", "name": "welcomeMessage", "text": "Full instructions tbc" }, { "type": "TEXTINPUT", "name": "apiKey", "displayName": "STAT API Key", "helpText": "You'll be provided with this :)", "placeholder": "STAT API Key" }, { "type": "TEXTINPUT", "name": "siteId", "displayName": "STAT Site ID", "helpText": "We need to retrieve this through the API", "placeholder": "STAT Site ID" }, { "type": "TEXTINPUT", "name": "siteName", "displayName": "STAT Site subdomain", "helpText": "The subdomain of your stat login url.", "placeholder": "STAT Site subdomain" } ] };