CH
Chiogros/Twitter-connectors-for-GDataStudio
A set of connectors for Google Data Studio to fetch data from Twitter API.
Twitter connectors for GDataStudio
Google Data Studio connectors to fetch data from Twitter API.
Connectors organization
There is a main connector called Core: it retrieves and handles data to bring it properly for GDS and it sets the authentication method.
Children connectors (like Users-followers) use Core functions and also have specific functions for their API endpoint.
How to use them on GDS
Setup Core connector
- Go to Google Apps Script
- Create a new project
- Name it
- Go to project settings
- Check
Display appsscript.json manifest file - Take note about Script ID (useful for children connectors)
- Go back to code window
- Create files and set code for Core connector
Setup child connector
- Go to Google Apps Script
- Create a new project
- Name it
- Go to project settings
- Check
Display appsscript.json manifest file - Go back to code window
- Create files and set code for the child connector
- In
appsscript.json, changeDependencies>Libraries>LibraryIDto the Core script ID you took note - Deploy it (easiest by going through
Use old editorbutton >Publish>Publish from manifest file)
Use connectors in GDS
- Go to Google Data Studio
- Create > Data source
- Search for your deployed child connector
- Fill credentials
- Now you can import it in your GDS reports
Get access token
- Create a Twitter developer account
- On your developer portal, create an app
- Check app permissions that must match with your needs
- Take note of the Bearer token for your app (since you cannot view it again, or past token will be overwritten)
- Use it to fill your connector credentials
How to create a new Twitter connector
First, copy Users-followers connector as template.
Then you have 3 things to change :
- Change
endpointandsubEndpointglobal vars to the GET method you want
https://developer.twitter.com/en/docs/twitter-api/api-reference-index
// core.gs
var endpoint = 'users';
var subEndpoint = 'followers';- Change
optionalFieldswith the ones listed on the GET method page
(warning : the first element must be concatened to?parameter.name=)
// core.gs
var optionalFields = ['?user.fields=created_at', 'description', ...];- Put fetchable fields from API
They can be the same as the optionalFields you put before.
// fields.gs
function getFields(request) {
var fields = cc.getFields();
var types = cc.FieldType;
var aggregations = cc.AggregationType;
fields.newDimension()
.setId('Users-followers_created_at')
.setType(types.TEXT); // BOOLEAN, TEXT, ...
fields.newDimension()
.setId('Users-followers_X')
.setType(types.TEXT); // BOOLEAN, NUMBER, ...
// put all fetchable fields
return fields;
}- Handle each data row
// dataHandler.gs
function responseToRows(requestedFields, response) {
// Filter for requested fields
var fields = requestedFields.asArray();
return response.map(function(dataElement) {
var rows = [];
fields.forEach(function (field) {
switch (field.getId()) {
case 'Users-followers_created_at':
return rows.push(dataElement.created_at);
case 'Users-followers_X':
return rows.push(dataElement.X);
// put all other cases
default:
break;
}
});
return { values: rows };
});
}On this page
Languages
JavaScript100.0%
GNU Affero General Public License v3.0
Created July 2, 2021
Updated January 28, 2023