Analysis

It’s possible to run analysis scripts on your computer, or inside Tago server. In the follow pages, you will be instructed on how to setup an analysis on your computer, use our services, and manage any data from Tago.

If you want to get instructions about how to upload your script or how to use third-party packages inside our server, take a look at admin analysis documentation

Setting Up Analysis

Through analysis, it is possible to insert any calculation and manage your data from Tago in any way you want. We provide some services, such as SMS and email, but you are free to use any third party packages that you need.

To setup an analysis, you first need a analysis token. That can be retrieved from the admin analysis section..

Syntax
new Analysis(/function/, /analysis_token/)

Arguments
function(function) a function to be executed when the analysis runs.
analysis_token(string) analysis token. Only needed if the script will run remotelly (Optional).

from tago import Tago
import os
analysis_token = os.environ.get('TAGO_TOKEN_DEVICE') or 'f0ba1f34-2bec-4cba-80ba-088624e37fb2'

def func_callback(context, scope):
   print "context"
   print context
   print "scope"
   print scope

def test_socket():
   s =  Tago(analysis_token).analysis.run_analysis(func_callback, 0)

test_socket()

context

As you can setup some predefined parameters in your analysis, it’s possible to get these value from the context variable defined in the admin. It is a object, and it comes with follow properties:

PROPERTY VALUE
environment All environment variables
token Token of the analysis
.log(/msg/) Print a message to the admin console

scope

Every time an action triggers a script, the variable scope will be generated. This scope will bring all others variables generated at the same time by the same event. For example, if you submit a form, together with the variable that the script is reading, the scope will return a list of all values/variable input in that form. This allows you to manipulate data in real time, and more easily the new values inserted in your bucket.

Runtime Timeout

Tago Analysis has a mechanism that prevents scripts from being locked in their executions by applying a timeout of 30 seconds. It means that if a script takes more than 30 seconds to be completed, Tago will abort it, and the script will not be completed.

This limitation doesn’t apply when running the analyze from your own machine. Check the information below to learn how to run scripts from an external server (e.g. from your own computer).

Running in your machine

You always have the option to run your script from your own machine or from Tago server without any technical difference. When running the script from your machine, you will need to install all the packages used by your analysis by using the command npm install mypackage.

Be sure to set your analysis configuration with the option to run the script from “external”. And finally, get the analysis token from the same configuration screen, and put it on the second parameter when calling new Analysis. Check out this example:

module.exports = new Analysis(myanalysis, ‘c89f0d50-38e2-11e6-966e-b94d760acc7d’);

Tago-Builder and Using Another Packages

When you are programming, it can be useful to use another packages inside your code; Or you may want to organize your project using require and subfoulders.

Tago is friendly with some packages:
* moment and moment-timezone
* lodash
* co
* async
* axios
* crypto
* Tago itself

So you don’t need to generate a build if you are using only them.

Also, Tago only accepts one single .js file when uploading your script to our servers. ago provides a builder CLI that can build your entire project and generate a single .js file with the whole code. You can access the repository clicking here

To use our Tago-Builder, follow the following steps:

  1. Type in your terminal `npm install -g tago-builder`
  2. Wait it for the installation to be completed
  3. Type in your terminal `tago-builder ‘my script’.js ‘new name’.tago.js (the last parameter is optional).
  4. Upload the generated ‘my script’.tago.js file to Tago.

If everything is okay, a new file called ‘my script’.tago.js will be generated. Now you can upload this file to Tago!

Services

We provide some functions that can greatly help your application. When creating a analysis, you are can use Tago services on your own, just make sure you understand the policies and cost associate with the usage.

When setting up a service, you need to pass an analysis-token. For convenience, the context returns a property token that you can use to setup a service object.

from tago import Tago
from tago.services.sms import SMS as sms
import os

TOKEN = os.environ.get('TAGO_TOKEN_ANALYSIS') or 'f0ba1f34-2bec-4cba-80ba-088624e37fb2'

# Main function to be executed when the analysis are called
def myanalysis(context, scope) {
    # Setting up a SMS service
    sms = Services(context.token).sms
}

def test_analysis():
    s = Tago(analysis_token).analysis.run_analysis(myanalysis, 0)

test_analysis()

sms

You can configure the system to send SMS directly from your analysis to yourself or your customers. Another option is to use the Actions to send SMS.

Some costs may occur when using the SMS service, which varies based on the country of operation. Check pricing, terms of use, and your plan before using the SMS service.

.send

Whenever you need to send a sms, use .send function.

Syntax
.send(/to/, /message/)

Arguments
to(string) A string with a phone number. If not sending to the USA, you have to add the country code, (+55) for Brazil, for example.
message(string) message to be sent. Use “n” to breakline. (optional)

Returns
(Promise)

from tago import Tago
from tago.services.sms import SMS as sms
import os

TOKEN = os.environ.get('TAGO_TOKEN_ANALYSIS') or 'f0ba1f34-2bec-4cba-80ba-088624e37fb2'

# Main function to be executed when the analysis are called
def myanalysis(context, scope) {
    # Setting up a SMS service
    sms = Services(context.token).sms
    sms.send('+11234567890', 'test tago services')
}

def test_analysis():
    s = Tago(analysis_token).analysis.run_analysis(myanalysis, 0)

test_analysis()

email

Email service allows you to send e-mail through your analysis. Cost may occur when using the e-mail service.

.send

Whenever you need to send an email, use .send function.

Syntax
.send(/to/, /subject/, /message/, /from/)

Arguments
to(string) E-mail address which will receive the email.
subject(string) Subject of the email;
message(string) message to be sent. Use “<br>” to breakline.
from(string) E-mail address for the receiver to reply. Default is tago@tago.io (optional);

Returns
(Promise)

from tago import Tago
from tago.services.email import Email as email
import os

TOKEN = os.environ.get('TAGO_TOKEN_ANALYSIS') or 'f0ba1f34-2bec-4cba-80ba-088624e37fb2'

# Main function to be executed when the analysis are called
def myanalysis(context, scope) {
    # Setting up a SMS service
    email = Services(context.token).email
    email.send('xyz@ncsu.edu', 'tago test', 'test tago services', 'xyz@ncsu.edu', '')
}

def test_analysis():
    s = Tago(analysis_token).analysis.run_analysis(myanalysis, 0)

test_analysis()