Skip to content
This repository was archived by the owner on Mar 10, 2023. It is now read-only.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,19 @@ import { ItlyNode as Itly } from '@itly/sdk';

load(options: PluginLoadOptions): void {...}

alias(userId: string, previousId?: string): void {...}
async alias(userId: string, previousId?: string) {...}

group(userId: string | undefined, groupId: string, properties?: Properties): void {...}
async group(userId: string | undefined, groupId: string, properties?: Properties) {...}

identify(userId?: string, properties?: Properties): void {...}
async identify(userId?: string, properties?: Properties) {...}

page(userId?: string, category?: string, name?: string, properties?: Properties): void {...}
async page(userId?: string, category?: string, name?: string, properties?: Properties) {...}

reset(): void {...}

flush(): void {...}
async flush() {...}

track(userId: string | undefined, event: ItlyEvent): void {...}
async track(userId: string | undefined, event: ItlyEvent) {...}

validate(event: Event): ValidationResponse {...}
}
Expand Down
10 changes: 5 additions & 5 deletions __tests__/src/CustomPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export default class CustomPlugin extends RequestLoggerPlugin {
};
}

alias(userId: string, previousId: string | undefined): void {
async alias(userId: string, previousId: string | undefined) {
this.log(`alias() userId='${userId}' previousId='${previousId}'`);
}

identify(userId: string | undefined, properties: Properties | undefined): void {
async identify(userId: string | undefined, properties: Properties | undefined) {
this.log(`identify() userId='${userId}' properties=${this.stringify(properties)}`);
}

Expand All @@ -49,7 +49,7 @@ export default class CustomPlugin extends RequestLoggerPlugin {
);
}

group(userId: string | undefined, groupId: string, properties: Properties | undefined): void {
async group(userId: string | undefined, groupId: string, properties: Properties | undefined) {
this.log(`group() userId='${userId}' groupId='${groupId}' properties=${this.stringify(properties)}`);
}

Expand All @@ -66,7 +66,7 @@ export default class CustomPlugin extends RequestLoggerPlugin {
);
}

page(userId?: string, category?: string, name?: string, properties?: Properties): void {
async page(userId?: string, category?: string, name?: string, properties?: Properties) {
this.log(
`page() userId='${userId}' category='${category}' name='${name}' properties=${this.stringify(properties)}`,
);
Expand All @@ -86,7 +86,7 @@ export default class CustomPlugin extends RequestLoggerPlugin {
);
}

track(userId: string | undefined, { name, properties }: Event): void {
async track(userId: string | undefined, { name, properties }: Event) {
this.log(`track() userId='${userId}' event='${name}' properties=${this.stringify(properties)}`);
}

Expand Down
10 changes: 5 additions & 5 deletions __tests__/src/ErrorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export default class ErrorPlugin extends Plugin {
};
}

alias(userId: string, previousId: string | undefined): void {
async alias(userId: string, previousId: string | undefined) {
throw new Error('Error in alias().');
}

identify(userId: string | undefined, properties: Properties | undefined): void {
async identify(userId: string | undefined, properties: Properties | undefined) {
throw new Error('Error in identify().');
}

Expand All @@ -35,7 +35,7 @@ export default class ErrorPlugin extends Plugin {
throw new Error('Error in postIdentify().');
}

group(userId: string | undefined, groupId: string, properties: Properties | undefined): void {
async group(userId: string | undefined, groupId: string, properties: Properties | undefined) {
throw new Error('Error in group().');
}

Expand All @@ -48,7 +48,7 @@ export default class ErrorPlugin extends Plugin {
throw new Error('Error in postGroup().');
}

page(userId?: string, category?: string, name?: string, properties?: Properties): void {
async page(userId?: string, category?: string, name?: string, properties?: Properties) {
throw new Error('Error in page().');
}

Expand All @@ -62,7 +62,7 @@ export default class ErrorPlugin extends Plugin {
throw new Error('Error in postPage().');
}

track(userId: string | undefined, event: Event): void {
async track(userId: string | undefined, event: Event) {
throw new Error('Error in track().');
}

Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-amplitude-node/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class AmplitudePlugin extends RequestLoggerPlugin {
callback?.(response);
} catch (e) {
responseLogger.error(e.toString());
throw e;
}
}

Expand All @@ -81,6 +82,7 @@ export class AmplitudePlugin extends RequestLoggerPlugin {
callback?.(response);
} catch (e) {
responseLogger.error(e.toString());
throw e;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-amplitude/lib/__tests__/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ afterEach(() => {
const methodArgLoggerMock = (methodName: string, ...args: any[]) => {
// eslint-disable-next-line no-console
console.log(`${methodName}()`, JSON.stringify(args.map((arg) => (typeof arg === 'function' ? '<FUNC>' : arg))));
// Invoke callback.
args.filter((arg) => typeof arg === 'function').forEach((arg) => arg());
};

test.each(testParams.map((test) => [test.name, test]) as any[])('%s',
Expand Down
61 changes: 42 additions & 19 deletions packages/plugin-amplitude/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,69 @@ export class AmplitudePlugin extends RequestLoggerPlugin {
}
}

identify(userId: string | undefined, properties?: Properties, options?: IdentifyOptions) {
async identify(userId: string | undefined, properties?: Properties, options?: IdentifyOptions) {
if (userId) {
this.amplitude.getInstance().setUserId(userId);
}

if (properties) {
const identifyObject = new this.amplitude.Identify();
for (const p in properties) {
if (!properties.hasOwnProperty(p)) {
continue;
}
if (!properties) {
return undefined;
}

identifyObject.set(p, (properties as any)[p]);
const identifyObject = new this.amplitude.Identify();
for (const p in properties) {
if (!properties.hasOwnProperty(p)) {
continue;
}

const { callback } = this.getPluginCallOptions<AmplitudeIdentifyOptions>(options);
const responseLogger = this.logger!.logRequest('identify', `${userId} ${JSON.stringify(properties)}`);
this.amplitude.getInstance().identify(identifyObject, this.wrapCallback(responseLogger, callback));
identifyObject.set(p, (properties as any)[p]);
}

const { callback } = this.getPluginCallOptions<AmplitudeIdentifyOptions>(options);
const responseLogger = this.logger!.logRequest('identify', `${userId} ${JSON.stringify(properties)}`);
return new Promise((resolve, reject) => {
this.amplitude.getInstance().identify(
identifyObject,
this.wrapCallback(responseLogger, callback, resolve, reject),
);
});
}

track(userId: string | undefined, { name, properties }: Event, options?: TrackOptions) {
async track(userId: string | undefined, { name, properties }: Event, options?: TrackOptions) {
const { callback } = this.getPluginCallOptions<AmplitudeIdentifyOptions>(options);
const responseLogger = this.logger!.logRequest('track', `${userId} ${name} ${JSON.stringify(properties)}`);
this.amplitude.getInstance().logEvent(name, properties, this.wrapCallback(responseLogger, callback));
return new Promise((resolve, reject) => {
this.amplitude.getInstance().logEvent(
name,
properties,
this.wrapCallback(responseLogger, callback, resolve, reject),
);
});
}

reset() {
this.amplitude.getInstance().setUserId(null);
this.amplitude.getInstance().regenerateDeviceId();
}

private wrapCallback(responseLogger: ResponseLogger, callback: AmplitudeCallback | undefined) {
private wrapCallback(
responseLogger: ResponseLogger,
callback: AmplitudeCallback | undefined,
resolve: () => void,
reject: (reason?: any) => void,
) {
return (statusCode: number, responseBody: string, details: unknown) => {
if (statusCode >= 200 && statusCode < 300) {
responseLogger.success(`${statusCode}`);
} else {
responseLogger.error(`unexpected status: ${statusCode}. ${responseBody}\n${JSON.stringify(details)}`);
try {
if (statusCode >= 200 && statusCode < 300) {
responseLogger.success(`${statusCode}`);
} else {
responseLogger.error(`unexpected status: ${statusCode}. ${responseBody}\n${JSON.stringify(details)}`);
}
callback?.(statusCode, responseBody, details);
resolve();
} catch (e) {
reject(e);
}
callback?.(statusCode, responseBody, details);
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-braze/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class BrazePlugin extends RequestLoggerPlugin {
}
}

identify(userId: string | undefined, properties?: Properties) {
async identify(userId: string | undefined, properties?: Properties) {
if (userId) {
this.appboy!.changeUser(userId);
}
Expand All @@ -56,7 +56,7 @@ export class BrazePlugin extends RequestLoggerPlugin {
}
}

track(userId: string | undefined, { name, properties }: Event) {
async track(userId: string | undefined, { name, properties }: Event) {
if (userId) {
this.appboy!.changeUser(userId);
}
Expand Down
45 changes: 33 additions & 12 deletions packages/plugin-mixpanel-node/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,61 @@ export class MixpanelPlugin extends RequestLoggerPlugin {
this.mixpanel = Mixpanel.init(this.apiKey, this.options);
}

alias(userId: string, previousId: string, options?: AliasOptions) {
async alias(userId: string, previousId: string, options?: AliasOptions) {
const { callback } = this.getPluginCallOptions<MixpanelAliasOptions>(options);
const responseLogger = this.logger!.logRequest('alias', `${userId}, ${previousId}`);
this.mixpanel!.alias(previousId, userId, this.wrapCallback(responseLogger, callback));
return new Promise((resolve, reject) => {
this.mixpanel!.alias(previousId, userId, this.wrapCallback(responseLogger, callback, resolve, reject));
});
}

identify(userId: string, properties: Properties | undefined, options?: IdentifyOptions) {
async identify(userId: string, properties: Properties | undefined, options?: IdentifyOptions) {
const { callback } = this.getPluginCallOptions<MixpanelIdentifyOptions>(options);
const payload = {
distinct_id: userId,
...properties,
};
const responseLogger = this.logger!.logRequest('identify', JSON.stringify(payload));
this.mixpanel!.people.set(userId, payload, this.wrapCallback(responseLogger, callback));
return new Promise((resolve, reject) => {
this.mixpanel!.people.set(userId, payload, this.wrapCallback(responseLogger, callback, resolve, reject));
});
}

track(userId: string, { name, properties }: Event, options?: TrackOptions) {
async track(userId: string, { name, properties }: Event, options?: TrackOptions) {
const { callback } = this.getPluginCallOptions<MixpanelTrackOptions>(options);
const payload = {
distinct_id: userId,
...properties,
};
const responseLogger = this.logger!.logRequest('track', JSON.stringify(payload));
this.mixpanel!.track(name, payload, this.wrapCallback(responseLogger, callback));
return new Promise((resolve, reject) => {
this.mixpanel!.track(name, payload, this.wrapCallback(responseLogger, callback, resolve, reject));
});
}

private wrapCallback(responseLogger: ResponseLogger, callback: MixpanelCallback | undefined) {
private wrapCallback(
responseLogger: ResponseLogger,
callback: MixpanelCallback | undefined,
resolve: () => void,
reject: (reason?: any) => void,
) {
return (err: Error | undefined): any => {
if (err == null) {
responseLogger.success('success');
} else {
responseLogger.error(err.toString());
try {
let result;
if (err == null) {
responseLogger.success('success');
result = callback?.(undefined);
resolve();
} else {
responseLogger.error(err.toString());
result = callback?.(err);
reject(err);
}
return result;
} catch (e) {
reject(e);
return undefined;
}
return callback?.(err);
};
}
}
Expand Down
51 changes: 42 additions & 9 deletions packages/plugin-mixpanel/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,70 @@ export class MixpanelPlugin extends RequestLoggerPlugin {
}
}

alias(userId: string, previousId: string | undefined) {
async alias(
userId: string,
previousId: string | undefined,
) {
this.mixpanel.alias(userId, previousId);
}

identify(userId: string | undefined, properties: Properties | undefined, options?: IdentifyOptions) {
async identify(
userId: string | undefined,
properties: Properties | undefined,
options?: IdentifyOptions,
) {
const { callback } = this.getPluginCallOptions<MixpanelIdentifyOptions>(options);

if (userId) {
this.mixpanel.identify(userId);
}

if (properties) {
const responseLogger = this.logger!.logRequest('identify', `${userId}, ${JSON.stringify(properties)}`);
this.mixpanel.people.set(properties, this.wrapCallback(responseLogger, callback));
return undefined;
}

const responseLogger = this.logger!.logRequest('identify', `${userId}, ${JSON.stringify(properties)}`);
return new Promise((resolve, reject) => {
this.mixpanel.people.set(properties, this.wrapCallback(responseLogger, callback, resolve, reject));
});
}

track(userId: string | undefined, { name, properties }: Event, options?: TrackOptions) {
async track(
userId: string | undefined,
{ name, properties }: Event,
options?: TrackOptions,
) {
const { callback } = this.getPluginCallOptions<MixpanelTrackOptions>(options);
const responseLogger = this.logger!.logRequest('track', `${userId}, ${name}, ${JSON.stringify(properties)}`);
this.mixpanel.track(name, { ...properties }, this.wrapCallback(responseLogger, callback));
return new Promise((resolve, reject) => {
this.mixpanel.track(name, { ...properties }, this.wrapCallback(responseLogger, callback, resolve, reject));
});
}

reset() {
this.mixpanel.reset();
}

private wrapCallback(responseLogger: ResponseLogger, callback: MixpanelCallback | undefined) {
private wrapCallback(
responseLogger: ResponseLogger,
callback: MixpanelCallback | undefined,
resolve: () => void,
reject: (reason?: any) => void,
) {
return (...args: any[]) => {
responseLogger.success(`done: ${JSON.stringify(args)}`);
return callback?.(...args);
try {
if (args.length === 1 && args[0] instanceof Error) {
responseLogger.error(args[0].toString());
callback?.(...args);
reject(args[0]);
} else {
responseLogger.success(JSON.stringify(args));
callback?.(...args);
resolve();
}
} catch (e) {
reject(e);
}
};
}
}
Expand Down
Loading