Amp-Node Client
Amp-Node Client Overview
The Amp-Node Client library has an Amp class. It can be used to construct an Amp instance used to represent a single Amp project and needs to be initialized with a project key and the domain, which is the URL of the Amp Agent.
Note: Contact support@scaledinference.com for more information on integrating with our Amp-Agent. Amp-Agent is required to run the Amp-Node client.
Installing Amp-Node Client
npm i --save amp-node
The Amp instance can then be used to create session objects which have two main methods: observe and decide.
Amp()
After importing amp-node, the Amp constructor can be used to create an Amp instance. It requires two parameters: a project key and the Amp-agent URL (with port 8100).
Importing and Intializing Amp
const Amp = require("amp-node");
...
const amp = new Amp({key: "YOUR_PROJECT_KEY", domain: "AMP_AGENT_URL"});
amp.Session()
The session constructor is used to create a session (object):
Initializing the Session
const session = new amp.Session();
Session objects created by an Amp instance support two methods: observe
and decide
.
session.observe()
The observe method is used to send observations.
void observe(name, properties, options, callback(err))
session.decide()
The decide method is used to make decisions.
void decide(name, candidates, options, callback(err, decision))
session.decideCond()
The decideCond method is used to make conditional decisions.
decideCond(name, candidates = [], event, contexts = {}, options = {}, callback(err, decision))
Example Usage
node ./examples/example.js <project_key> http://localhost:8100
console.log(`
This will demonstrate how to use the node thin client to communicate with amp agent. Make sure you pass in a valid project key and your domain to your amp agent, if you use a different apiPath, make sure you pass that too.
`);
const Amp = require("../Amp");
const projectKey = process.argv[2];
const domain = process.argv[3];
const apiPath = process.argv[4];
const amp = new Amp({key: projectKey, domain: domain, apiPath: apiPath});
console.log(`
amp instance initialized
`);
const session = new amp.Session();
console.log(`
session instance initliazed
`);
session.observe("userInfo", {lang: "en", country: "USA"}, function(err) {
if (err) {
console.log('UserInfo Observe not sent!', err.message);
} else {
console.log('UserInfo Observe request sent!');
}
});
session.decide("Template", [
{color: "red", font: "bold"},
{color: "green", font: "italic"},
{color: "red", font: "italic"},
{color: "green", font: "bold"}
], function(err, decision) {
if (err) {
console.log('Template Decide not sent!', err.message);
} else {
console.log('Template Decide request sent!', JSON.stringify(decision));
}
});
session.decide("TemplateCombo", {
color: ["red", "green"],
font: ["bold", "italic"]
}, function(err, decision) {
if (err) {
console.log('TemplateCombo Decide not sent!', err.message);
} else {
console.log('TemplateCombo Decide request sent!', JSON.stringify(decision));
}
});
session.observe("ClickBtn", {btnName: "SignUp"}, function(err) {
if (err) {
console.log('ClickBtn Observe not sent!', err.message);
} else {
console.log('ClickBtn Observe request sent!');
}
});
session.decideCond('TemplateCombo', {color: ['red', 'green'], font: ['bold', 'italic']}, 'Locale', {en: {showModal: true}, es: {showModal: false}}, function(err, decision) {
if (err) {
console.log('TemplateCombo conditional decision not sent!', err.message);
} else {
console.log('TemplateCombo conditional decide sent! Response was: ', decision);
}
});