Skip to content

Commit

Permalink
feat: remove logging for amplitude (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbrunson authored Jul 25, 2023
1 parent 528d080 commit 3988758
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 88 deletions.
2 changes: 0 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ export const FLUSH_INTERVAL = 60 * 1000;
export const DEFAULT_LOG_LEVEL: $Values<typeof LOG_LEVEL> = __DEBUG__
? LOG_LEVEL.DEBUG
: LOG_LEVEL.WARN;

export const AMPLITUDE_URL = "https://api2.amplitude.com/2/httpapi";
8 changes: 2 additions & 6 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type CrossDomainWindowType,
} from "@krakenjs/cross-domain-utils/src";

import { canUseSendBeacon, isAmplitude, sendBeacon } from "./util";
import { canUseSendBeacon, sendBeacon } from "./util";

export type TransportOptions = {|
url: string,
Expand Down Expand Up @@ -36,11 +36,7 @@ export function getHTTPTransport(httpWin?: CrossDomainWindowType): Transport {
let beaconResult = false;

if (canUseSendBeacon({ headers, enableSendBeacon })) {
if (isAmplitude(url)) {
beaconResult = sendBeacon({ win, url, data: json, useBlob: false });
} else {
beaconResult = sendBeacon({ win, url, data: json, useBlob: true });
}
beaconResult = sendBeacon({ win, url, data: json, useBlob: true });
}

return beaconResult
Expand Down
43 changes: 0 additions & 43 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,46 +158,3 @@ describe("beaver-logger default transport tests", () => {
expect(sendBeaconCalled).toBeFalsy();
});
});

describe("beaver-logger Amplitude tests", () => {
it("should log something and flush it to the buffer using sendBeacon if Amplitude API key is present", async () => {
const $logger = Logger({
url: "https://api2.amplitude.com/2/httpapi",
amplitudeApiKey: "test_key",
enableSendBeacon: true,
});

$logger.track({
foo: "bar",
bar: true,
user_id: "abc123",
});

const requestSpy = vi
.spyOn(belter, "request")
.mockImplementation((data) => {
const { url, method, json } = data;
if (
url === "https://api2.amplitude.com/2/httpapi" &&
method === "POST"
) {
return json.events.some(
(event) => event.foo === "bar" && event.user_id === "abc123"
);
}

return false;
});

let sendBeaconCalled = false;

window.navigator.sendBeacon = () => {
sendBeaconCalled = true;
};

await $logger.flush();
expect(sendBeaconCalled).toBeTruthy();
expect(requestSpy).toHaveBeenCalled();
expect(requestSpy).toReturnWith(true);
});
});
27 changes: 0 additions & 27 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
LOG_LEVEL_PRIORITY,
AUTO_FLUSH_LEVEL,
FLUSH_INTERVAL,
AMPLITUDE_URL,
} from "./config";
import { LOG_LEVEL, PROTOCOL } from "./constants";
import { extendIfDefined } from "./util";
Expand All @@ -28,7 +27,6 @@ type LoggerOptions = {|
transport?: Transport,
flushInterval?: number,
enableSendBeacon?: boolean,
amplitudeApiKey?: string,
|};

type ClientPayload = Payload;
Expand Down Expand Up @@ -76,7 +74,6 @@ export function Logger({
prefix,
logLevel = DEFAULT_LOG_LEVEL,
transport = getHTTPTransport(),
amplitudeApiKey,
flushInterval = FLUSH_INTERVAL,
enableSendBeacon = false,
}: LoggerOptions): LoggerType {
Expand Down Expand Up @@ -160,26 +157,6 @@ export function Logger({
}).catch(noop);
}

if (amplitudeApiKey) {
transport({
method: "POST",
url: AMPLITUDE_URL,
headers: {},
json: {
api_key: amplitudeApiKey,
events: tracking.map((payload: Payload) => {
// $FlowFixMe
return {
event_type: payload.transition_name || "event",
event_properties: payload,
...payload,
};
}),
},
enableSendBeacon,
}).catch(noop);
}

events = [];
tracking = [];
metrics = [];
Expand Down Expand Up @@ -326,10 +303,6 @@ export function Logger({
transport = opts.transport;
}

if (opts.amplitudeApiKey) {
amplitudeApiKey = opts.amplitudeApiKey;
}

if (opts.flushInterval) {
flushInterval = opts.flushInterval;
}
Expand Down
11 changes: 1 addition & 10 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { type SameDomainWindowType } from "@krakenjs/cross-domain-utils/src";

import { AMPLITUDE_URL } from "./config";
import type { Payload } from "./types";

type CanUseBeaconOptions = {|
Expand All @@ -28,14 +27,6 @@ const canUseSendBeacon = ({
return false;
};

const isAmplitude = (url: string): boolean => {
if (url === AMPLITUDE_URL) {
return true;
}

return false;
};

type SendBeaconOptions = {|
win: SameDomainWindowType,
url: string,
Expand Down Expand Up @@ -75,4 +66,4 @@ const extendIfDefined = (target: Payload, source: Payload) => {
}
};

export { canUseSendBeacon, extendIfDefined, isAmplitude, sendBeacon };
export { canUseSendBeacon, extendIfDefined, sendBeacon };

0 comments on commit 3988758

Please sign in to comment.