Skip to content

Instantly share code, notes, and snippets.

@adstage-david
Last active February 28, 2018 19:17
Show Gist options
  • Save adstage-david/41fead9f79ae914a3f4f55838b1a98e0 to your computer and use it in GitHub Desktop.
Save adstage-david/41fead9f79ae914a3f4f55838b1a98e0 to your computer and use it in GitHub Desktop.
An Example Tableau + AdStage Connector (untested)
(function() {
// STEP 1 - CONFIGURE:
var adstageToken = "deadxxxxxbeef";
var adstageOrgId = 80;
// This makes it so AJAX calls are authenticated:
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + adstageToken);
xhr.setRequestHeader('Accept', 'application/json');
}
});
//
// Connector definition
//
var myConnector = tableau.makeConnector();
var connectionUrl = "https://platform.adstage.io/api/organizations/" + adstageOrgId + "/build_report";
// NOTES: The getSchema function defines the columns you want to show up in
// tableau and what type of data they are add more columns here, order will matter.
myConnector.getSchema = function(schemaCallback) {
// NOTES: If you needed to pull data from the form where they click the button
// you could fetch it here
var data = tableau.connectionData;
var cols = [
{ id: "entity_id", alias: "Entity ID", dataType: tableau.dataTypeEnum.string },
{ id: "date", alias: "Date", dataType: tableau.dataTypeEnum.date },
{ id: "spend", alias: "Spend", dataType: tableau.dataTypeEnum.float }
// STEP 3: add columns as needed
];
var tableInfo = {
id: "adstage_campaigns",
columns: cols
};
schemaCallback([tableInfo]); // tell tableau about the fields and their types (can have multiple tables)
};
myConnector.getData = function(table, doneCallback) {
// STEP 2: Configure to fit needs
var report = {
date_range: "this_month",
entity_level: "campaigns",
fields: ["spend"],
limit: 500,
aggregate_by: "day"
};
var APIPromise = makeAPIRequest(table, report, connectionUrl);
APIPromise.then(function(response) {
console.log("Success");
doneCallback();
}, function(error) {
console.error(error);
});
};
function makeAPIRequest(table, report, connectionUrl) {
return new Promise(function(resolve, reject) {
var xhr = $.ajax({
url: connectionUrl,
type: "POST",
data: JSON.stringify(report),
contentType: 'application/json',
dataType: 'json',
success: function(data) {
if (data._embedded["adstage:time_series"]) {
var series = data._embedded["adstage:time_series"];
var ii, jj;
var toRet = [];
// mash the data into an array of objects
for (ii = 0; ii < series.length; ++ii) {
var meta = series[ii].meta;
var list = series[ii].series;
for (jj = 0; jj < list.length; ++jj) {
// STEP 3: add columns as needed
var entry = [meta.entity_id, Date.parse(list[jj].timeframe.start), list[jj].data["spend"]];
toRet.push(entry);
}
}
table.appendRows(toRet);
resolve();
} else {
Promise.reject("No results found for ticker symbol: " + ticker);
}
},
error: function(xhr, ajaxOptions, thrownError) {
Promise.reject("error connecting to the yahoo stock data source: " + thrownError);
}
});
});
}
setupConnector = function() {
// NOTES: If you needed to set up data to pass from the main page to the connector, set it on connectionData
tableau.connectionData = null;
tableau.connectionName = 'AdStage Data - Campaigns This Month'; // name the data source. This will be the data source name in Tableau
tableau.submit();
};
tableau.registerConnector(myConnector);
//
// Setup connector UI
//
$(document).ready(function() {
$("#submitButton").click(function() { // This event fires when a button is clicked
setupConnector();
});
$('#tickerForm').submit(function(event) {
event.preventDefault();
setupConnector();
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment