Create Custom Data Connector With App Script For Data Studio

By | 30 June 2018

In this article, we will create a custom data connector with App Script for Data Studio. In changing scenarios, we can visualize our data and statements. My scenario is population of intended the country. I will get population of country from Population IO API. Firstly, we will create an App Script. For this, click “New Script” on top left. You can define a name for your script. I defined “Population”.

   

From now on, we can write our the connector. We have to need 4 methods.

  • getAuthType()
  • getConfig()
  • getSchema()
  • getData()

In order, we will create all of them. Firstly, we can write the getAuthType(). Remove default function “myFunction”. And write getAuthType() method as below. Here, we haven’t got any authentication, we defined “NONE”.

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

We can now, write to next method. It is getConfig(). This method is like skeleton our connector. Before describe about it, we will create it.

function getConfig() {
    var config = {
        configParams: [
            {
                 type: 'INFO',
                 name: 'instructions',
                 text: 'Enter country name to fetch their population count.'
            },
            {
                type: 'TEXTINPUT',
                name: 'country',
                displayName: 'Enter a single country name',
                helpText: 'e.g. Turkey or Russia',
                placeholder: 'Turkey'
            }
        ]
    };
    return config;
}

In this method, we define the requirements and descriptions for our connector. We can get this output with this method as below.

At the moment, we need a schema. So, we can set metrics or dimensions. For this, create a schema as below.

var populationSchema = [
    {
        name: 'countryName',
        dataType: 'STRING',
        semantics: {
            conceptType: 'DIMENSION'
        }
    },
    {
         name: 'population',
         dataType: 'NUMBER',
         semantics: {
             conceptType: 'METRIC',
             semanticType: 'NUMBER',
             isReaggregatable: true
        },
        defaultAggregationType: 'SUM'
    },
    {
        name: 'date',
        dataType: 'STRING',
        semantics: {
            conceptType: 'DIMENSION',
            semanticType: 'YEAR_MONTH_DAY'
        }
     }
];

So, we can get this output and set configurations of metrics or dimensions.

From now on, we can create third method. We write this method as below.

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

Next method is our last method. It is getData(). We will fetch data of population and configure suitable format.

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

    // Fetch and parse data from API
    var url = [
       'http://api.population.io/1.0/population/',
       request.configParams.country,
       '/today-and-tomorrow/'
    ];
    var response = UrlFetchApp.fetch(url.join(''));
    var parsedResponse = JSON.parse(response).total_population;

    // Transform parsed data and filter for requested fields
    var requestedData = parsedResponse.map(function(dailyPopulation) {
       var values = [];
       requestedSchema.forEach(function (field) {
          switch (field.name) {
             case 'date':
                values.push(dailyPopulation.date.replace(/-/g, ''));
                break;
             case 'population':
                values.push(dailyPopulation.population);
                break;
             case 'countryName':
                values.push(request.configParams.country);
                break;
            default:
                values.push('');
          }
       });
       return { values: values };
    });

    return {
       schema: requestedSchema,
       rows: requestedData
    };
}

Save these codes and we need to create manifest file. For this, go “View > Show manifest file.” Remove all text and write as below.

{
    "dataStudio": {
          "name": "Populations of the World",
          "logoUrl": "http://icons.iconarchive.com/icons/dtafalonso/modern-xp/256/ModernXP-73-Globe-                icon.png",
          "company": "Mert Simsek",
          "companyUrl": "https://mertblog.net",
          "addonUrl": "https://api.population.io",
          "supportUrl": "https://github.com/mertingen",
          "description": "Get populations of the world!",
          "sources": ["population-io"]
    }
}

It needs to be a file like this.

Now, we can test our connector. For this, go “Publish > Deploy from manifest” and you will see this screen.

  

Click Latest Version text and you will see a url. Click the url. If you have not used Data Studio before, you will be asked to authorize Data Studio to your account and agree to the terms and conditions. Then, you will see this screen. I wrote “Turkey” into input as you see.

  

Click the Connect button on the right side.  You will see our dimensions and metrics again. Click the Explore button on the right side.

   

From now on, we can see today and tomorrow population. Of course, tomorrow is just estimate.

   

In this way, we have successfully connected our connector with App Script. In this article, so much for now. We have learned connect any connector for Data Studio. We can change Api, metrics/dimensions and we can connect the connector to Data Studio. See you next article 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

*