Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for the ignore reporting functionality #23

Merged
merged 4 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "voxa-dashbot",
"version": "2.1.0-alpha2",
"version": "2.1.0-alpha3",
"description": "Integrate Dashbot analytics into your Alexa apps using the voxa framework",
"main": "lib/index.js",
"scripts": {
Expand Down
38 changes: 22 additions & 16 deletions src/Voxa-Dashbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export function register(voxaApp: VoxaApp, config: IVoxaDashbotConfig) {
}

async function initDashbot(voxaEvent: IVoxaEvent) {
if (!shouldTrack(voxaEvent)) {
return;
}

const { platform } = voxaEvent;
const apiKey = _.get(pluginConfig, platform.name) || pluginConfig.api_key;

Expand All @@ -118,10 +122,6 @@ export function register(voxaApp: VoxaApp, config: IVoxaDashbotConfig) {
| IDashbotPageLaunchEvent
| IDashbotCustomEvent
) {
if (pluginConfig.suppressSending) {
return;
}

const requestBody = {
...dashbotEvent,
...{
Expand All @@ -146,15 +146,10 @@ export function register(voxaApp: VoxaApp, config: IVoxaDashbotConfig) {
}

async function trackIncoming(voxaEvent: IVoxaEvent) {
for (const ignoreRule of pluginConfig.ignoreUsers) {
if (voxaEvent.user.userId.match(ignoreRule)) {
return;
}
}

if (pluginConfig.suppressSending) {
if (!shouldTrack(voxaEvent)) {
return;
}

const { rawEvent, platform } = voxaEvent;
const apiKey = _.get(pluginConfig, platform.name) || pluginConfig.api_key;

Expand All @@ -170,11 +165,8 @@ export function register(voxaApp: VoxaApp, config: IVoxaDashbotConfig) {
reply: IVoxaReply,
transition?: ITransition
) {
if (_.includes(pluginConfig.ignoreUsers, voxaEvent.user.userId)) {
return Promise.resolve(null);
}
if (pluginConfig.suppressSending) {
return Promise.resolve(null);
if (!shouldTrack(voxaEvent)) {
return;
}

const { rawEvent, platform } = voxaEvent;
Expand Down Expand Up @@ -203,4 +195,18 @@ export function register(voxaApp: VoxaApp, config: IVoxaDashbotConfig) {

await Dashbot.logOutgoing(rawEvent, reply);
}

function shouldTrack(voxaEvent: IVoxaEvent): boolean {
for (const ignoreRule of pluginConfig.ignoreUsers) {
if (voxaEvent.user.userId.match(ignoreRule)) {
return false;
}
}

if (pluginConfig.suppressSending) {
return false;
}

return true;
}
}
84 changes: 34 additions & 50 deletions test/voxa-dashbot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,20 @@ describe("Voxa-Dashbot plugin", () => {
});

it("should not record analytics if the user is ignored", async () => {
const spy = simple.spy((_voxaEvent, reply) => {
return reply;
});
voxaApp.onSessionEnded(spy);
const spy = simple.spy(() => ({
say: "LaunchIntent.OpenResponse",
flow: "yield",
to: "entry"
}));

voxaApp.onIntent("LaunchIntent", spy);

const event = {
request: {
type: "SessionEndedRequest"
type: "LaunchRequest"
},
session: {
new: false,
new: true,
application: {
applicationId: "appId"
},
Expand All @@ -244,23 +247,26 @@ describe("Voxa-Dashbot plugin", () => {
ignoreUsersConfig.ignoreUsers = ["user-id"];

register(voxaApp, ignoreUsersConfig);
return alexaSkill.execute(event as any).then(reply => {
expect(reply.version).to.equal("1.0");
});
const reply = await alexaSkill.execute(event as any);
expect(reply.version).to.equal("1.0");
expect(nockScope.isDone()).to.be.false;
});

it("should support regexex for ignored user ids", async () => {
const spy = simple.spy((_voxaEvent, reply) => {
return reply;
});
voxaApp.onSessionEnded(spy);
const spy = simple.spy(() => ({
say: "LaunchIntent.OpenResponse",
flow: "yield",
to: "entry"
}));

voxaApp.onIntent("LaunchIntent", spy);

const event = {
request: {
type: "SessionEndedRequest"
type: "LaunchRequest"
},
session: {
new: false,
new: true,
application: {
applicationId: "appId"
},
Expand All @@ -280,15 +286,20 @@ describe("Voxa-Dashbot plugin", () => {
});

it("should not record analytics if suppressSending === true", async () => {
const spy = simple.spy((_voxaEvent, reply) => reply);
voxaApp.onSessionEnded(spy);
const spy = simple.spy(() => ({
say: "LaunchIntent.OpenResponse",
flow: "yield",
to: "entry"
}));

voxaApp.onIntent("LaunchIntent", spy);

const event = {
request: {
type: "SessionEndedRequest"
type: "LaunchRequest"
},
session: {
new: false,
new: true,
application: {
applicationId: "appId"
},
Expand All @@ -302,34 +313,8 @@ describe("Voxa-Dashbot plugin", () => {
suppressSendingConfig.suppressSending = true;

register(voxaApp, suppressSendingConfig);
return alexaSkill.execute(event as any).then(reply => {
expect(reply.version).to.equal("1.0");
});
});

it("should not record analytics due to Dashbot Error", async () => {
const spy = simple.spy((_voxaEvent, reply) => reply);
voxaApp.onSessionEnded(spy);

const event = {
request: {
type: "SessionEndedRequest"
},
session: {
new: false,
application: {
applicationId: "appId"
},
user: {
userId: "user-id"
}
}
};

register(voxaApp, dashbotConfig);
return alexaSkill.execute(event as any).then(reply => {
expect(reply.version).to.equal("1.0");
});
const reply = await alexaSkill.execute(event as any);
expect(reply.version).to.equal("1.0");
});

it("should not record analytics due to Dashbot Error", async () => {
Expand All @@ -352,9 +337,8 @@ describe("Voxa-Dashbot plugin", () => {
};

register(voxaApp, dashbotConfig);
return alexaSkill.execute(event as any).then(reply => {
expect(reply.version).to.equal("1.0");
});
const reply = await alexaSkill.execute(event as any);
expect(reply.version).to.equal("1.0");
});

const alexaRequestTypes = [
Expand Down