Google Data Studio connectors to fetch data from Twitter API.
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.
- 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
- 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
>LibraryID
to the Core script ID you took note - Deploy it (easiest by going through
Use old editor
button >Publish
>Publish from manifest file
)
- 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
- 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
First, copy Users-followers connector as template.
Then you have 3 things to change :
- Change
endpoint
andsubEndpoint
global 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
optionalFields
with 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 };
});
}