automizy-js-api
Version:
JavaScript API library for Automizy Marketing Automation software
1,461 lines (1,369 loc) • 87.9 kB
JavaScript
(function($){
var $AA = {};
(function(){
$AA = new function () {
var t = this;
t.d = {
version: '0.1.0',
login:false,
hasOpenLoginForm:true,
lastAjaxData:{},
ad:{
error:function (jqXHR, textStatus, errorThrown) {
if(arguments.length === 3) {
if ($.inArray(jqXHR.status, [404, 405]) >= 0) {
alert(jqXHR.status + ' (' + errorThrown + ')');
} else if ($.inArray(jqXHR.status, [403]) >= 0) {
$AA.d.lastAjaxData = this;
$AA.openLoginDialog();
} else if ($.inArray(jqXHR.status, [400, 401]) >= 0) {
alert(jqXHR.status + ' (' + errorThrown + ')');
$AA.openLoginDialog();
} else {
console.error(jqXHR.status, textStatus, errorThrown);
}
}else{
console.log('<----------- Automizy Api Error');
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
console.log('Automizy Api Error ----------->');
$AA.openLoginDialog();
}
}
}
};
var baseUrl = "https://api.automizy.com";
var apiLoginPhp = "https://app.automizy.com/php/login.php";
var apiRefreshPhp = "https://app.automizy.com/php/refresh.php";
t.u = {
base:baseUrl,
loginPhp: apiLoginPhp,
refreshPhp: apiRefreshPhp,
oauth: baseUrl + '/oauth',
segments: baseUrl + '/segments',
campaigns: baseUrl + '/campaigns',
splitTests: baseUrl + '/split-tests',
newsletters: baseUrl + '/newsletters',
contacts: baseUrl + '/contacts',
customFields: baseUrl + '/custom-fields',
users: baseUrl + '/users',
jobs: baseUrl + '/jobs',
webhooks: baseUrl + '/webhooks',
images: baseUrl + '/images',
templates: baseUrl + '/templates',
forms: baseUrl + '/forms',
automations: baseUrl + '/automations',
account: baseUrl + '/account',
contactImports: baseUrl + '/contact-imports',
tags: baseUrl + '/tags',
clients: baseUrl + '/clients',
emailPreview: baseUrl + '/email-preview'
};
t.m = [];
}();
})();
(function(){
$AA.cookie = function () {
function extend () {
var i = 0;
var result = {};
for (; i < arguments.length; i++) {
var attributes = arguments[ i ];
for (var key in attributes) {
result[key] = attributes[key];
}
}
return result;
}
function init (converter) {
function api (key, value, attributes) {
var result;
// Write
if (arguments.length > 1) {
attributes = extend({
path: '/'
}, api.defaults, attributes);
if (typeof attributes.expires === 'number') {
var expires = new Date();
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
attributes.expires = expires;
}
try {
result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {}
value = encodeURIComponent(String(value));
value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
key = encodeURIComponent(String(key));
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
key = key.replace(/[\(\)]/g, escape);
return (document.cookie = [
key, '=', value,
attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
attributes.path && '; path=' + attributes.path,
attributes.domain && '; domain=' + attributes.domain,
attributes.secure && '; secure'
].join(''));
}
// Read
if (!key) {
result = {};
}
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all. Also prevents odd result when
// calling "get()"
var cookies = document.cookie ? document.cookie.split('; ') : [];
var rdecode = /(%[0-9A-Z]{2})+/g;
var i = 0;
for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var name = parts[0].replace(rdecode, decodeURIComponent);
var cookie = parts.slice(1).join('=');
if (cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
}
cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent);
if (this.json) {
try {
cookie = JSON.parse(cookie);
} catch (e) {}
}
if (key === name) {
result = cookie;
break;
}
if (!key) {
result[name] = cookie;
}
}
return result;
}
api.get = api.set = api;
api.getJSON = function () {
return api.apply({
json: true
}, [].slice.call(arguments));
};
api.defaults = {};
api.remove = function (key, attributes) {
api(key, '', extend(attributes, {
expires: -1
}));
};
api.withConverter = init;
return api;
}
return init();
}();
})();
(function(){
$AA.date = {};
$AA.date.now = function () {
return new Date().getTime();
};
$AA.date.timestamp = function () {
return Math.floor($AA.date.now() / 1000);
};
})();
(function(){
var Token = function () {
var t = this;
t.d = {
access_token: $AA.cookie.get('AutomizyApiAccessToken') || false,
refresh_token: $AA.cookie.get('AutomizyApiRefreshToken') || false,
client_id: $AA.cookie.get('AutomizyApiClientId') || false,
username: $AA.cookie.get('AutomizyApiUsername') || false,
expires: parseInt($AA.cookie.get('AutomizyApiExpires')) || 0,
scope: $AA.cookie.get('AutomizyApiScope') || null,
token_type: $AA.cookie.get('AutomizyApiTokenType') || "Bearer",
error:function(){},
success:function(){},
loginError:function(){},
loginAjaxError:function(){},
loginAjaxSuccess:function(){},
refreshAjaxError:function(){},
refreshAjaxSuccess:function(){},
refreshLoopTimeout:false
};
};
var p = Token.prototype;
p.refreshLoop = function(){
var t = this;
if ($AA.date.timestamp() > parseInt($AA.cookie.get('AutomizyApiExpires')) - 120) {
t.refresh();
}
clearTimeout(t.d.refreshLoopTimeout);
t.d.refreshLoopTimeout = setTimeout(function(){
t.refreshLoop();
}, 30000);
};
p.refreshLoopStop = function(){
var t = this;
clearTimeout(t.d.refreshLoopTimeout);
};
p.refreshLoopStart = function(){
var t = this;
t.refreshLoop();
};
p.refreshLoopRestart = function(){
var t = this;
t.refreshLoopStop();
t.refreshLoopStart();
};
p.loggedIn = function(loggedIn){
var t = this;
if (typeof loggedIn !== 'undefined') {
t.d.loggedIn = $AA.parseBoolean(loggedIn);
return t;
}
return t.d.loggedIn;
};
p.get = function () {
var t = this;
if($AA.cookie.get('AutomizyApiAccessToken') !== false){
t.loggedIn(true);
t.refreshLoopRestart();
}else{
t.refreshLoopStop();
t.loggedIn(false);
}
return $AA.cookie.get('AutomizyApiAccessToken');
};
p.set = function (token) {
var t = this;
if(typeof token.access_token !== 'undefined'){
t.d.access_token = token.access_token;
$AA.cookie.set('AutomizyApiAccessToken', token.access_token, t.cookieAttributes())
}
if(typeof token.expires_in !== 'undefined'){
t.d.expires = $AA.date.timestamp() + parseInt(token.expires_in) - 10;
$AA.cookie.set('AutomizyApiExpires', t.d.expires, t.cookieAttributes())
}
if(typeof token.refresh_token !== 'undefined'){
t.d.refresh_token = token.refresh_token;
$AA.cookie.set('AutomizyApiRefreshToken', token.refresh_token, t.cookieAttributes())
}
if(typeof token.scope !== 'undefined'){
t.d.scope = token.scope;
if(t.d.scope !== false)$AA.cookie.set('AutomizyApiScope', token.scope, t.cookieAttributes());
}
if(typeof token.token_type !== 'undefined'){
t.d.token_type = token.token_type;
$AA.cookie.set('AutomizyApiTokenType', token.token_type, t.cookieAttributes())
}
if(typeof token.client_id !== 'undefined'){
t.d.client_id = token.client_id;
$AA.cookie.set('AutomizyApiClientId', token.client_id, t.cookieAttributes())
}
if(typeof token.username !== 'undefined'){
t.d.username = token.username;
$AA.cookie.set('AutomizyApiUsername', token.username, t.cookieAttributes())
}
return true;
};
p.refresh = function () {
var t = this;
return $.ajax({
type: "POST",
url: $AA.u.refreshPhp,
data: {
refresh_token: $AA.cookie.get('AutomizyApiRefreshToken'),
username: $AA.cookie.get('AutomizyApiUsername')
},
success: function (data, textStatus, jqXHR) {
t.set(data);
t.loggedIn(true);
t.refreshAjaxSuccess.apply(t, [data, textStatus, jqXHR]);
},
error: function(jqXHR, textStatus, errorThrown){
t.refreshAjaxError.apply(t, [jqXHR, textStatus, errorThrown]);
}
});
};
p.error = function(error){
var t = this;
if (typeof error === 'function') {
t.d.error = error;
return t;
}
return t.d.error;
};
p.success = function(success){
var t = this;
if (typeof success === 'function') {
t.d.success = success;
return t;
}
return t.d.success;
};
p.loginError = function(loginError){
var t = this;
if (typeof loginError === 'function') {
t.d.loginError = loginError;
return t;
}
return t.d.loginError;
};
p.loginAjaxError = function(loginAjaxError){
var t = this;
if (typeof loginAjaxError === 'function') {
t.d.loginAjaxError = loginAjaxError;
return t;
}
return t.d.loginAjaxError;
};
p.loginAjaxSuccess = function(loginAjaxSuccess){
var t = this;
if (typeof loginAjaxSuccess === 'function') {
t.d.loginAjaxSuccess = loginAjaxSuccess;
return t;
}
return t.d.loginAjaxSuccess;
};
p.refreshAjaxError = function(refreshAjaxError){
var t = this;
if (typeof refreshAjaxError === 'function') {
t.d.refreshAjaxError = refreshAjaxError;
return t;
}
return t.d.refreshAjaxError;
};
p.refreshAjaxSuccess = function(refreshAjaxSuccess){
var t = this;
if (typeof refreshAjaxSuccess === 'function') {
t.d.refreshAjaxSuccess = refreshAjaxSuccess;
return t;
}
return t.d.refreshAjaxSuccess;
};
p.passwordLogin = function (obj) {
var t = this;
var obj = obj || {};
obj.username = obj.username || false;
obj.password = obj.password || false;
if (obj.username === false || obj.password === false) {
t.loginError.apply(t, ['The username and password must be provide!']);
return false;
}
$AA.cookie.set('AutomizyApiUsername', obj.username, t.cookieAttributes());
return $.ajax({
type: "POST",
url: $AA.u.loginPhp,
data: {
username: obj.username,
password: obj.password
},
success: function (data, textStatus, jqXHR) {
t.set(data);
t.loggedIn(true);
t.refreshLoopRestart();
t.d.loginAjaxSuccess.apply(t, [data, textStatus, jqXHR]);
},
error: function(jqXHR, textStatus, errorThrown){
t.d.loginAjaxError.apply(t, [jqXHR, textStatus, errorThrown]);
}
});
};
p.credentialsLogin = function (obj) {
var t = this;
var obj = obj || {};
obj.clientId = obj.clientId || false;
obj.clientSecret = obj.clientSecret || false;
if (obj.clientId === false || obj.clientSecret === false) {
t.loginError.apply(t, ['The clientId and clientSecret must be provide!']);
return false;
}
return $.ajax({
type: "POST",
url: $AA.u.oauth,
data: {
grant_type: "client_credentials",
client_id: obj.clientId,
client_secret: obj.clientSecret
},
success: function (data, textStatus, jqXHR) {
t.set(data);
t.loggedIn(true);
t.refreshLoopRestart();
t.d.loginAjaxSuccess.apply(t, [data, textStatus, jqXHR]);
},
error: function(jqXHR, textStatus, errorThrown){
t.d.loginAjaxError.apply(t, [jqXHR, textStatus, errorThrown]);
}
});
};
p.login = function (obj) {
var t = this;
var obj = obj || {};
if (typeof obj.username !== 'undefined' && typeof obj.password !== 'undefined') {
return t.passwordLogin(obj);
}else if (typeof obj.clientId !== 'undefined' && typeof obj.clientSecret !== 'undefined') {
return t.credentialsLogin(obj);
} else {
t.loginError.apply(t, ['Missing parameters!']);
return false;
}
};
p.cookieAttributes = function(){
var obj = {};
if(location.href.indexOf('.protopmail.com') >= 0){
obj.domain = '.protopmail.com';
}else if(location.href.indexOf('.automizy.com') >= 0){
obj.domain = '.automizy.com';
}
return obj;
};
p.logout = function(){
var t = this;
t.loggedIn(false);
t.refreshLoopStop();
t.d.access_token = false;
t.d.refresh_token = false;
t.d.username = false;
t.d.client_id = false;
t.d.expires = 0;
$AA.cookie.remove('AutomizyApiAccessToken', t.cookieAttributes());
$AA.cookie.remove('AutomizyApiRefreshToken', t.cookieAttributes());
$AA.cookie.remove('AutomizyApiClientId', t.cookieAttributes());
$AA.cookie.remove('AutomizyApiExpires', t.cookieAttributes());
$AA.cookie.remove('AutomizyApiUsername', t.cookieAttributes());
};
$AA.m.Token = Token;
$AA.d.token = new Token();
$AA.token = function () {
return $AA.d.token;
}
})();
(function(){
$AA.initBasicFunctions = function (module, moduleName) {
var module = module || false;
if (module === false)
return false;
var moduleName = moduleName || false;
if (moduleName === false)
return false;
var moduleNameLower = moduleName.toLowerCase();
var moduleNameLowerFirst = moduleName.charAt(0).toLowerCase() + moduleName.slice(1);
var p = module.prototype;
p.init = p.init || function(){
var t = this;
if(typeof t.d.urlSuffix === 'undefined'){
t.d.urlSuffix = '';
}
if(typeof t.d.itemsDir === 'undefined'){
t.d.itemsDir = false;
}
if(typeof t.d.fields === 'undefined'){
t.d.fields = false;
}
if(typeof t.d.format === 'undefined'){
t.d.format = false;
}
if(typeof t.d.limit === 'undefined'){
t.d.limit = false;
}
if(typeof t.d.page === 'undefined'){
t.d.page = false;
}
if(typeof t.d.order_by === 'undefined'){
t.d.order_by = false;
}
if(typeof t.d.order_dir === 'undefined'){
t.d.order_dir = false;
}
if(typeof t.d.links === 'undefined'){
t.d.links = false;
}
if(typeof t.d.set === 'undefined'){
t.d.set = {};
}
};
p.initParameter = p.initParameter || function(obj){
var t = this;
if (typeof obj.urlSuffix !== 'undefined')
t.urlSuffix(obj.urlSuffix);
if (typeof obj.itemsDir !== 'undefined')
t.itemsDir(obj.itemsDir);
if (typeof obj.format !== 'undefined')
t.format(obj.format);
if (typeof obj.fields !== 'undefined')
t.fields(obj.fields);
if (typeof obj.limit !== 'undefined')
t.limit(obj.limit);
if (typeof obj.page !== 'undefined')
t.page(obj.page);
if (typeof obj.orderBy !== 'undefined' || typeof obj.order_by !== 'undefined')
t.orderBy(obj.orderBy || obj.order_by);
if (typeof obj.orderDir !== 'undefined' || typeof obj.order_dir !== 'undefined')
t.orderDir(obj.orderDir || obj.order_dir);
if (typeof obj.links !== 'undefined')
t.links(obj.links);
if (typeof obj.url !== 'undefined')
t.url(obj.url);
};
p.setOptions = p.setOptions || function(obj){
if (typeof obj.fields !== 'undefined')
t.d.option.fields = obj.fields; //mezők vesszővel
if (typeof obj.format !== 'undefined')
t.d.option.format = obj.format; //format data
if (typeof obj.limit !== 'undefined')
t.d.option.limit = obj.limit; //hány darab
if (typeof obj.page !== 'undefined')
t.d.option.page = obj.page; //hanyadik elemtől
if (typeof obj.where !== 'undefined')
t.d.option.where = obj.where; //feltétel
if (typeof obj.order_by !== 'undefined')
t.d.option.order_by = obj.order_by; //mi szerint rendezzen
if (typeof obj.order_dir !== 'undefined')
t.d.option.order_dir = obj.order_dir; //desc vagy asc
if (typeof obj.order !== 'undefined')
t.d.option.order = obj.order; //name:desc
if (typeof obj.links !== 'undefined')
t.d.option.links = obj.links; //milyen linkek kellenek vesszővel
}
p.getDataFromParameter = p.getDataFromParameter || function(obj){
var data = {};
if (obj.fields !== false)
data.fields = obj.fields;
if (obj.format !== false)
data.format = obj.format;
if (obj.limit !== false)
data.limit = obj.limit;
if (obj.page !== false)
data.page = obj.page;
if (obj.where !== false)
data.where = obj.where;
if (obj.order_dir !== false)
data.order_dir = obj.order_dir;
if (obj.order_by !== false)
data.order_by = obj.order_by;
if (obj.order !== false)
data.order = obj.order;
if (obj.links !== false)
data.links = obj.links;
return data;
};
p.get = p.get || function (obj, isMod, async) {
var t = this;
var isMod = true; //modify the options from the 'obj' object
if(typeof async !== 'undefined'){
async = $AA.parseBoolean(async);
}else{
var async = true;
}
if (typeof isMod !== 'undefined') {
isMod = $AA.parseBoolean(isMod);
}
if (isMod && (typeof obj === 'object' || typeof obj === 'array')) {
t.setOptions(obj)
}
var data = {};
if (typeof obj === 'object' || typeof obj === 'array') {
data = t.getDataFromParameter(obj);
}else{
for(var i in t.d.option){
if(t.d.option[i] !== false){
data[i] = t.d.option[i];
}
}
}
if(typeof data.order === 'undefined' && typeof data.order_by !== 'undefined'){
data.order = data.order_by+':'+data.order_dir || 'asc'
}
var $ajax = $.ajax({
url: t.d.url + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
async:async,
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
if(!async){
return $ajax.responseJSON;
}
return $ajax;
};
p.getSync = p.getSync || function(obj, isMod){
var obj = obj || false;
var isMod = isMod || false;
return this.get.apply(this, [obj, isMod, false]);
};
p.export = p.export || function (obj, isMod, async) {
var t = this;
var isMod = true;
if(typeof async !== 'undefined'){
async = $AA.parseBoolean(async);
}else{
var async = true;
}
if (typeof isMod !== 'undefined') {
isMod = $AA.parseBoolean(isMod);
}
if (isMod && (typeof obj === 'object' || typeof obj === 'array')) {
t.setOptions(obj)
}
var data = {};
if (typeof obj === 'object' || typeof obj === 'array') {
data = t.getDataFromParameter(obj);
}else{
for(var i in t.d.option){
if(t.d.option[i] !== false){
data[i] = t.d.option[i];
}
}
}
if(typeof data.order === 'undefined' && typeof data.order_by !== 'undefined'){
data.order = data.order_by+':'+data.order_dir || 'asc'
}
data.showContent = false;
var $ajax = $.ajax({
xhr: function()
{
var xhr = new window.XMLHttpRequest();
xhr.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
console.log(parseInt(percentComplete*100));
}
}, false);
return xhr;
},
url: t.d.url + t.d.urlSuffix,
type: 'GET',
//dataType: 'json',
async:async,
data: data,
headers: {
Authorization: 'Bearer ' + $AA.token().get(),
Accept: 'text/csv'
},
error: $AA.token().error()
});
if(!async){
return $ajax.responseJSON;
}
return $ajax;
};
p.insert = p.insert || function(obj, async){
var t = this;
if(typeof async !== 'undefined'){
async = $AA.parseBoolean(async);
}else{
var async = true;
}
var data = obj;
var $ajax = $.ajax({
url: t.d.url + t.d.urlSuffix,
type: 'POST',
dataType: 'json',
async:async,
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
if(!async){
return $ajax.responseJSON;
}
return $ajax;
};
p.insertSync = p.insertSync || function(obj){
var obj = obj || false;
return this.insert.apply(this, [obj, false]);
};
p.update = p.update || function(obj, id, async){
var t = this;
if(typeof async !== 'undefined'){
async = $AA.parseBoolean(async);
}else{
var async = true;
}
var data = obj;
var id = id || obj.id || 0;
delete data.id;
var $ajax = $.ajax({
url: t.d.url + '/' + id + t.d.urlSuffix,
type: 'PATCH',
dataType: 'json',
async:async,
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
if(!async){
return $ajax.responseJSON;
}
return $ajax;
};
p.updateSync = p.updateSync || function(obj, id){
var obj = obj || false;
var id = id || false;
return this.update.apply(this, [obj, id, false]);
};
p.delete = p.delete || function(id, async){
var t = this;
if(typeof async !== 'undefined'){
async = $AA.parseBoolean(async);
}else{
var async = true;
}
var $ajax = $.ajax({
url: t.d.url+'/'+id + t.d.urlSuffix,
type: 'DELETE',
dataType: 'json',
async:async,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
if(!async){
return $ajax.responseJSON;
}
return $ajax;
};
p.deleteSync = p.deleteSync || function(id){
var id = id || false;
return this.delete.apply(this, [id, false]);
};
/*
[
[
['name', '=', 'Tilda'],
['age', '<=', 18]
],
[
['name', 'like', 'Thom%'],
['age', 'in', [18, 20, 21]]
],
[
['name', 'like', 'Thom%'],
['age', 'between', [18, 22]]
]
]
*/
p.getAll = p.getAll || function () {
var t = this;
return $.ajax({
url: t.d.url + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getRecordById = p.getRecordById || function (id) {
var t = this;
var data = {};
for(var i in t.d.option){
if(t.d.option[i] !== false){
data[i] = t.d.option[i];
}
}
if(typeof data.order === 'undefined' && typeof data.order_by !== 'undefined'){
data.order = data.order_by+':'+data.order_dir || 'asc'
}
return $.ajax({
url: t.d.url + '/' + id + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getFieldById = p.getFieldById || function (id, fieldName) {
var t = this;
var fieldTree = fieldName.split('.');
return $.ajax({
url: t.d.url + '/' + id + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: {fields: fieldTree[0], links: ''},
headers: {Authorization: 'Bearer ' + $AA.token().get()},
converters: {
'text json': function (result) {
var field = $.parseJSON(result)[fieldTree[0]];
for (var i = 1; i < fieldTree.length; i++) {
field = field[fieldTree[i]];
}
return field;
}
},
error: $AA.token().error()
});
};
p.getAllIdNamePair = p.getAllIdNamePair || function (nameFieldName) {
var t = this;
var fieldTree = nameFieldName.split('.');
return $.ajax({
url: t.d.url + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: {fields: 'id,' + fieldTree[0], links: ''},
headers: {Authorization: 'Bearer ' + $AA.token().get()},
converters: {
'text json': function (result) {
var res = $.parseJSON(result)._embedded;
res = res[Object.keys(res)[0]];
var arr = [];
for (var i in res) {
var field = res[i][fieldTree[0]];
for (var j = 1; j < fieldTree.length; j++) {
field = field[fieldTree[j]];
}
arr.push([res[i]['id'], field]);
}
return arr;
}
},
error: $AA.token().error()
});
};
p.urlSuffix = p.urlSuffix || function (urlSuffix) {
var t = this;
if (typeof urlSuffix !== 'undefined') {
t.d.urlSuffix = urlSuffix;
return t;
}
return t.d.urlSuffix;
};
p.itemsDir = p.itemsDir || function (itemsDir) {
var t = this;
if (typeof itemsDir !== 'undefined') {
t.d.itemsDir = itemsDir;
return t;
}
return t.d.itemsDir;
};
p.fields = p.fields || function (fields) {
var t = this;
if (typeof fields !== 'undefined') {
if(fields !== false && fields.length > 0){
t.d.option.fields = fields;
}
return t;
}
return t.d.option.fields;
};
p.limit = p.limit || function (limit) {
var t = this;
if (typeof limit !== 'undefined') {
t.d.option.limit = limit;
return t;
}
return t.d.option.limit;
};
p.format = p.format || function (format) {
var t = this;
if (typeof format !== 'undefined') {
t.d.option.format = format;
return t;
}
return t.d.option.format;
};
p.page = p.page || function (page) {
var t = this;
if (typeof page !== 'undefined') {
t.d.option.page = page;
return t;
}
return t.d.option.page;
};
p.where = p.where || function (where) {
var t = this;
if (typeof where !== 'undefined') {
t.d.option.where = where;
return t;
}
return t.d.option.where;
};
p.orderBy = p.order_by = p.orderBy || p.order_by || function (order_by) {
var t = this;
if (typeof order_by !== 'undefined') {
if(order_by === false)return t;
t.d.option.order_by = order_by;
return t;
}
return t.d.option.order_by;
};
p.orderDir = p.order_dir = p.orderDir || p.order_dir || function (order_dir) {
var t = this;
if (typeof order_dir !== 'undefined') {
if(order_dir === false)return t;
t.d.option.order_dir = order_dir;
return t;
}
return t.d.option.order_dir;
};
p.order = p.order || function (order) {
var t = this;
if (typeof order !== 'undefined') {
if(order === false)return t;
t.d.option.order = order;
return t;
}
return t.d.option.order;
};
p.links = p.links || function (links) {
var t = this;
if (typeof links !== 'undefined') {
t.d.option.links = links;
return t;
}
return t.d.option.links;
};
p.set = p.values = p.set || p.values || function(set, value){
var t = this;
if (typeof set === 'string' && typeof value !== 'undefined') {
t.d.option.set[set] = value;
return t;
}
if (typeof set === 'object' || typeof set === 'array') {
for(var i in set){
t.d.option.set[i] = set[i];
}
return t;
}
return t.d.option.set;
};
$AA.m[moduleName] = module;
$AA[moduleNameLowerFirst] = function (obj) {
var t = new module(obj);
return t;
};
};
})();
(function(){
var Segments = function (obj) {
var t = this;
t.d = {
a: 3,
option: {},
url: $AA.u.segments
};
t.init();
if (typeof obj !== 'undefined') {
t.initParameter(obj);
}
};
var p = Segments.prototype;
p.calculateByArray = function(arr){
var t = this;
return $.ajax({
url: t.d.url+'/calculate' + t.d.urlSuffix,
type: 'GET',
data: arr,
dataType: 'json',
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
/**/
p.calculateById = function(id){
var t = this;
return $.ajax({
url: t.d.url+'/'+id+'/calculate' + t.d.urlSuffix,
type: 'GET',
data: {
waitForResponse:true,
saveAfterFinish:false
},
dataType: 'json',
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.calculateAndSave = function(id){
var t = this;
return $.ajax({
url: t.d.url+'/'+id+'/calculate' + t.d.urlSuffix,
type: 'GET',
data: {
waitForResponse:true,
saveAfterFinish:true
},
dataType: 'json',
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
$AA.initBasicFunctions(Segments, "Segments");
})();
(function(){
var Campaigns = function (obj) {
var t = this;
t.d = {
a: 3,
option: {},
url: $AA.u.campaigns
};
t.init();
if (typeof obj !== 'undefined') {
t.initParameter(obj);
}
};
var p = Campaigns.prototype;
p.getOpenStatById = function (id, from, to) {
var t = this;
var data = {
format:'total'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getClickStatById = function (id, from, to) {
var t = this;
var data = {
format:'total'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getShareStatById = function (id, from, to) {
var t = this;
var data = {
format:'total'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/shares' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getUnsubscribeStatById = function (id, from, to) {
var t = this;
var data = {
format:'total'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/unsubscribes' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getBounceStatById = function (id, from, to) {
var t = this;
var data = {
format:'total'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/bounces' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getGeoStatById = function (id, from, to) {
var t = this;
var data = {
format:'raw'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/geo-locations' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getOpenTimeLineById = function (id, from, to, step) {
var t = this;
var data = {
format:'timeline'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
if(typeof step !== 'undefined' && step !== false){
data.step = step;
}
return $.ajax({
url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getClickTimeLineById = function (id, from, to, step) {
var t = this;
var data = {
format:'timeline'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
if(typeof step !== 'undefined' && step !== false){
data.step = step;
}
return $.ajax({
url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getOpenDevicePieById = function (id, from, to) {
var t = this;
var data = {
format:'aggregate',
groupBy:'device'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getClickDevicePieById = function (id, from, to) {
var t = this;
var data = {
format:'aggregate',
groupBy:'device'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getOpenDeviceTimeLineById = function (id, from, to, step) {
var t = this;
var data = {
format:'timeline',
groupBy:'device'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getClickDeviceTimeLineById = function (id, from, to, step) {
var t = this;
var data = {
format:'timeline',
groupBy:'device'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getOpenOsPieById = function (id, from, to) {
var t = this;
var data = {
format:'aggregate',
groupBy:'os'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getClickOsPieById = function (id, from, to) {
var t = this;
var data = {
format:'aggregate',
groupBy:'os'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getOpenOsTimeLineById = function (id, from, to, step) {
var t = this;
var data = {
format:'timeline',
groupBy:'os'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getClickOsTimeLineById = function (id, from, to, step) {
var t = this;
var data = {
format:'timeline',
groupBy:'os'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getOpenBrowserPieById = function (id, from, to) {
var t = this;
var data = {
format:'aggregate',
groupBy:'browser'
};
if(typeof from !== 'undefined' && from !== false){
data.from = from;
}
if(typeof to !== 'undefined' && to !== false){
data.to = to;
}
return $.ajax({
url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
type: 'GET',
dataType: 'json',
data: data,
headers: {Authorization: 'Bearer ' + $AA.token().get()},
error: $AA.token().error()
});
};
p.getClickBrowserPieById = function (id, from, to) {
var t = this;
var data = {
format:'aggregate',
groupBy:'browser'
};
if(typeof from !== 'undefined' &