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

Support for push report metrics endpoint. #98

Merged
merged 5 commits into from
Feb 7, 2022
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
6 changes: 6 additions & 0 deletions lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export type RequestHandlerOptions = {
headers: RequestOptions['headers'];
body?: string | null;
};
export interface PushRequestData {
delivery_id?: string;
device_id?: string;
event?: 'delivered' | 'opened' | 'converted';
timestamp?: number;
};

const TIMEOUT = 10_000;
const PACKAGE_JSON = findPackageJson(resolve(__dirname, '..'));
Expand Down
8 changes: 6 additions & 2 deletions lib/track.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { RequestOptions } from 'https';
import Request, { BasicAuth, RequestData } from './request';
import Request, { BasicAuth, RequestData, PushRequestData } from './request';
import { Region, RegionUS } from './regions';
import { isEmpty } from './utils';
import { IdentifierType } from './types';

type TrackDefaults = RequestOptions & { region: Region; url?: string; apiUrl?: string };
type TrackDefaults = RequestOptions & { region: Region; url?: string; apiUrl?: string; };

class MissingParamError extends Error {
constructor(param: string) {
Expand Down Expand Up @@ -98,6 +98,10 @@ export class TrackClient {
});
}

trackPush(data: PushRequestData = {}) {
return this.request.post(`${this.trackRoot}/push/events`, data);
}

addDevice(customerId: string | number, device_id: string, platform: string, data = {}) {
if (isEmpty(customerId)) {
throw new MissingParamError('customerId');
Expand Down
23 changes: 23 additions & 0 deletions test/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ test('#trackAnonymous works', (t) => {
);
});

test('#trackPush works', (t) => {
sinon.stub(t.context.client.request, 'post');
t.context.client.trackPush();
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackUrl}/push/events`, {}),
);

t.context.client.trackPush({
delivery_id: "RPILAgUBcRhIBqSfeiIwdIYJKxTY",
event: "opened",
device_id: "CIO-Delivery-Token from the notification",
timestamp: 1613063089
});
t.truthy(
(t.context.client.request.post as SinonStub).calledWith(`${RegionUS.trackUrl}/push/events`, {
delivery_id: "RPILAgUBcRhIBqSfeiIwdIYJKxTY",
event: "opened",
device_id: "CIO-Delivery-Token from the notification",
timestamp: 1613063089
}),
);
});

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