Skip to content
Open
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
49 changes: 49 additions & 0 deletions lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,55 @@ export interface PushRequestData {
event?: 'delivered' | 'opened' | 'converted';
timestamp?: number;
}
export interface ReportEmailMetricsRequestData {
delivery_id: string;
metric: 'bounced' | 'clicked' | 'converted' | 'deferred' | 'delivered' | 'dropped' | 'opened' | 'spammed';
timestamp?: number;
recipient?: string;
reason?: string;
href?: string;
}
export interface ReportInappMetricsRequestData {
delivery_id: string;
metric: 'clicked' | 'converted' | 'opened';
timestamp?: number;
recipient?: string;
href?: string;
}
export interface ReportPushMetricsRequestData {
delivery_id: string;
metric: 'converted' | 'delivered' | 'opened';
timestamp?: number;
recipient?: string;
}
export interface ReportSlackMetricsRequestData {
delivery_id: string;
metric: 'clicked' | 'converted' | 'delivered' | 'opened';
timestamp?: number;
href?: string;
}
export interface ReportSMSMetricsRequestData {
delivery_id: string;
metric: 'bounced' | 'clicked' | 'delivered' | 'opened';
timestamp?: number;
recipient?: string;
reason?: string;
href?: string;
}
export interface ReportWebhookMetricsRequestData {
delivery_id: string;
metric: 'bounced' | 'clicked' | 'converted' | 'deferred' | 'delivered' | 'dropped' | 'opened' | 'spammed';
timestamp?: number;
reason?: string;
href?: string;
}
export type ReportMetricsRequestData =
| ReportEmailMetricsRequestData
| ReportInappMetricsRequestData
| ReportPushMetricsRequestData
| ReportSlackMetricsRequestData
| ReportSMSMetricsRequestData
| ReportWebhookMetricsRequestData;
Comment on lines +28 to +76
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's no way to narrow the type here to choose a variant (i.e. there's no way for TS to know if it should be choosing an SMS metrics object over email) I think it would be better to match the docs and have a single request type for now.

This will also prevent issues such as the Push and Slack metrics objects not having reason, etc.


const TIMEOUT = 10_000;

Expand Down
9 changes: 8 additions & 1 deletion lib/track.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RequestOptions } from 'https';
import Request, { BasicAuth, RequestData, PushRequestData } from './request';
import Request, { BasicAuth, RequestData, PushRequestData, ReportMetricsRequestData } from './request';
import { Region, RegionUS } from './regions';
import { isEmpty, isIdentifierType, MissingParamError } from './utils';
import { IdentifierType } from './types';
Expand Down Expand Up @@ -99,6 +99,13 @@ export class TrackClient {
});
}

reportMetrics(data: ReportMetricsRequestData) {
return this.request.post(`${this.trackRoot}/metrics`, data);
}

/**
* @deprecated Use reportMetrics instead
*/
trackPush(data: PushRequestData = {}) {
return this.request.post(`${this.trackRoot}/push/events`, data);
}
Expand Down
31 changes: 31 additions & 0 deletions test/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,37 @@ test('#trackPush works', (t) => {
);
});

test('#reportMetrics works', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.reportMetrics({
delivery_id: 'RPILAgUBcRhIBqSfeiIwdIYJKxTY',
metric: 'opened',
});
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackUrl}/metrics`, {
delivery_id: 'RPILAgUBcRhIBqSfeiIwdIYJKxTY',
metric: 'opened',
}),
);

t.context.client.reportMetrics({
delivery_id: 'RPILAgUBcRhIBqSfeiIwdIYJKxTY',
metric: 'clicked',
timestamp: 1613063089,
recipient: 'cool.person@company.com',
href: 'https://example.com',
});
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackUrl}/metrics`, {
delivery_id: 'RPILAgUBcRhIBqSfeiIwdIYJKxTY',
metric: 'clicked',
timestamp: 1613063089,
recipient: 'cool.person@company.com',
href: 'https://example.com',
}),
);
});

ID_INPUTS.forEach(([input, expected]) => {
test(`#trackPageView works for ${input}`, (t) => {
sinon.stub(t.context.client.request, 'post');
Expand Down