Skip to content

Commit

Permalink
#186510319 : Add Actions support in middleware (#105)
Browse files Browse the repository at this point in the history
* Add Actions support

* Update test and add more tests for errors

* Bump versions

* Update README
  • Loading branch information
selectiveduplicate authored Nov 4, 2024
1 parent 634b113 commit 0ba3288
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 27 deletions.
17 changes: 0 additions & 17 deletions .vscode/launch.json

This file was deleted.

101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,107 @@ The `metadata` field can contain any company demographic or other information yo
This method is a convenient helper that calls the Moesif API library. For more information, see the function documentation in [Moesif Node.js API reference](https://www.moesif.com/docs/api?javascript--nodejs#update-companies-in-batch).
### Add a single Action
To track and log single [Action](https://www.moesif.com/docs/getting-started/user-actions/) in Moesif, use the `sendAction()` function.
```javascript
var moesifMiddleware = moesif(options);
// Only `actionName` and `request` is required.
// `metadata` is an object containing custom metadata about the Action.
var action = {
transactionId: "a3765025-46ec-45dd-bc83-b136c8d1d257",
actionName: "Clicked Sign Up",
sessionToken: "23jdf0owekfmcn4u3qypxg08w4d8ayrcdx8nu2nz]s98y18cx98q3yhwmnhcfx43f",
userId: "12345",
companyId: "67890",
metadata: {
email: "[email protected]",
button_label: 'Get Started',
sign_up_method: 'Google SSO'
},
request: {
time: new Date(),
uri: "https://api.acmeinc.com/items/reviews/",
ipAddress: "61.48.220.123",
}
};
// Send the Action
moesifMiddleware.sendAction(action, callback);
```
The `metadata` field can contain any optional metadata about the Action you want to store. Moesif only requires the `actionName` and `request` fields.
This method is a convenient helper that calls the Moesif API library. For more information, see [Moesif API reference](https://www.moesif.com/docs/api?int_source=docs#track-a-custom-action).
### Add a batch of Actions
To track and log a batch of [Actions](https://www.moesif.com/docs/getting-started/user-actions/) in Moesif, use the `sendActionsBatch()` function.
```javascript
var moesifMiddleware = moesif(options);
// Define the request context objects for each action.
var req_contextA = {
time: new Date(),
uri: "https://api.acmeinc.com/items/reviews/",
ipAddress: "61.48.220.123",
userAgentString: "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
};
var req_contextB = {
time: new Date(),
uri: "https://api.acmeinc.com/pricing/",
ipAddress: "61.48.220.126",
userAgentString: "PostmanRuntime/7.26.5"
};
// Define the actions.
// Only `actionName` and `request` is required.
// `metadata` is an object containing custom metadata about the Action.
var actionA = {
transactionId: "a3765025-46ec-45dd-bc83-b136a8d1d357",
actionName: "Clicked Sign Up",
sessionToken: "23abf0owekfmcn4u3qypxg09w4d8ayrcdx8nu2ng]s98y18cx98q3yhwmnhcfx43f",
userId: "18340",
companyId: "25100",
metadata: {
email: "[email protected]",
button_label: 'Get Started',
sign_up_method: 'Google SSO'
},
request: req_contextA
};
var actionB = {
transactionId: "a3765024-46ee-45dd-bc83-b136c8d1d250",
actionName: "Viewed pricing",
sessionToken: "23jdf0owejfmbn4u3qypxg09w4d8ayrxdx8nu2ng]s98y18cx98q3yhwmnhcfx43f",
userId: "12390",
companyId: "97895",
metadata: {
email: "[email protected]",
button_label: 'See pricing',
sign_up_method: 'Google SSO'
},
request: req_contextB
};
var actions = [
actionA,
actionB
];
// Send the batch of Actions
moesifMiddleware.sendActionsBatch(actions, callback);
```
The `metadata` field can contain any optional metadata about the Action you want to store. Moesif only requires the `actionName` and `request` fields.
This method is a convenient helper that calls the Moesif API library. For more information, see [Moesif API reference](https://www.moesif.com/docs/api?int_source=docs#track-custom-actions-in-batch).
## How to Get Help
If you face any issues using this middleware, try the [troubheshooting guidelines](#troubleshoot). For further assistance, reach out to our [support team](mailto:[email protected]).
Expand Down
24 changes: 23 additions & 1 deletion lib/ensureValidUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,33 @@ function ensureValidCompaniesBatchModel(companiesBatchModel) {
}
}

function ensureValidActionModel(actionModel) {
if (!actionModel.actionName) {
throw new Error('To send an Action, the actionName field is required');
}
if (!(actionModel.request && actionModel.request.uri)) {
throw new Error('To send an Action, the request and request.uri fields are required');
}
}

function ensureValidActionsBatchModel(actionsBatchModel) {
for (let actionModel of actionsBatchModel) {
if (!actionModel.actionName) {
throw new Error('To send an Action, the actionName field is required');
}
if (!(actionModel.request && actionModel.request.uri)) {
throw new Error('To send an Action, the request and request.uri fields are required');
}
}
}

module.exports = {
ensureValidOptions: ensureValidOptions,
ensureValidLogData: ensureValidLogData,
ensureValidUserModel: ensureValidUserModel,
ensureValidUsersBatchModel: ensureValidUsersBatchModel,
ensureValidCompanyModel: ensureValidCompanyModel,
ensureValidCompaniesBatchModel: ensureValidCompaniesBatchModel
ensureValidCompaniesBatchModel: ensureValidCompaniesBatchModel,
ensureValidActionModel: ensureValidActionModel,
ensureValidActionsBatchModel: ensureValidActionsBatchModel
};
58 changes: 57 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var EventModel = moesifapi.EventModel;
var UserModel = moesifapi.UserModel;
var CompanyModel = moesifapi.CompanyModel;
var SubscriptionModel = moesifapi.SubscriptionModel;
var ActionModel = moesifapi.ActionModel;

var dataUtils = require('./dataUtils');
var patch = require('./outgoing');
Expand Down Expand Up @@ -36,6 +37,8 @@ var ensureValidUserModel = ensureValidUtils.ensureValidUserModel;
var ensureValidUsersBatchModel = ensureValidUtils.ensureValidUsersBatchModel;
var ensureValidCompanyModel = ensureValidUtils.ensureValidCompanyModel;
var ensureValidCompaniesBatchModel = ensureValidUtils.ensureValidCompaniesBatchModel;
var ensureValidActionModel = ensureValidUtils.ensureValidActionModel;
var ensureValidActionsBatchModel = ensureValidUtils.ensureValidActionsBatchModel;

// default option utility functions.

Expand Down Expand Up @@ -108,7 +111,7 @@ function makeMoesifMiddleware(options) {
* @type {string}
*/
config.ApplicationId = options.applicationId || options.ApplicationId;
config.UserAgent = 'moesif-nodejs/' + '3.8.1';
config.UserAgent = 'moesif-nodejs/' + '3.9.0';
config.BaseUri = options.baseUri || options.BaseUri || config.BaseUri;
// default retry to 1.
config.retry = isNil(options.retry) ? 1 : options.retry;
Expand Down Expand Up @@ -806,6 +809,59 @@ function makeMoesifMiddleware(options) {
}
};

/**
* @param {object} actionModel - https://www.moesif.com/docs/api?javascript--nodejs#track-a-custom-action
* @param {function} [cb]
*/
moesifMiddleware.sendAction = function (actionModel, cb) {
var action = new ActionModel(actionModel);
logMessage(options.debug, 'sendAction', 'convertedActionObject=', action);
ensureValidActionModel(action);
logMessage(options.debug, 'sendAction', 'actionModel valid');
if (cb) {
moesifController.sendAction(action, cb);
} else {
return new Promise(function (resolve, reject) {
moesifController.sendAction(action, function (err, response) {
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
}
};

/**
* @param {object[]} actionsBatchModel
* @param {function} [cb]
*/
moesifMiddleware.sendActionsBatch = function (actionsBatchModel, cb) {
var actionsBatch = [];
for (let action of actionsBatchModel) {
actionsBatch.push(new ActionModel(action));
}

logMessage(options.debug, 'sendActionsBatch', 'convertedActionArray=', actionsBatch);
ensureValidActionsBatchModel(actionsBatchModel);
logMessage(options.debug, 'sendActionsBatch', 'actionsBatchModel valid');

if (cb) {
moesifController.sendActionsBatch(actionsBatch, cb);
} else {
return new Promise(function (resolve, reject) {
moesifController.sendActionsBatch(actionsBatch, function (err, response) {
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
}
};

moesifMiddleware.startCaptureOutgoing = function () {
if (moesifMiddleware._mo_patch) {
logMessage(
Expand Down
Binary file removed moesif-nodejs-3.10.6.tgz
Binary file not shown.
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moesif-nodejs",
"version": "3.8.2",
"version": "3.9.0",
"description": "Monitoring agent to log API calls to Moesif for deep API analytics",
"main": "lib/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -38,7 +38,7 @@
"iconv-lite": "^0.6.3",
"koa-body": "^4.2.0",
"lodash": "^4.17.19",
"moesifapi": ">=3.0.1",
"moesifapi": "^3.1.0",
"raw-body": "^2.4.2",
"request-ip": "^3.3.0",
"uuid4": "^2.0.2"
Expand Down
Loading

0 comments on commit 0ba3288

Please sign in to comment.