Skip to content

Commit

Permalink
Merge pull request #2207 from metriport/1669-inbound-stats
Browse files Browse the repository at this point in the history
1669 inbound stats
  • Loading branch information
Orta21 committed Jun 5, 2024
2 parents cdd300c + a2f0a78 commit ca9903f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
16 changes: 14 additions & 2 deletions packages/core/src/external/analytics/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface EventMessageV1 extends IdentifyMessageV1 {

const defaultPostHogApiKey = Config.getPostHogApiKey();

export const analytics = (params: EventMessageV1, postApiKey?: string) => {
export function analytics(params: EventMessageV1, postApiKey?: string): PostHog | void {
const apiKey = postApiKey ?? defaultPostHogApiKey;
if (!apiKey) return;

Expand All @@ -34,7 +34,19 @@ export const analytics = (params: EventMessageV1, postApiKey?: string) => {
};

posthog.capture(params);
};

return posthog;
}

export async function analyticsAsync(params: EventMessageV1, postApiKey?: string) {
const posthog = analytics(params, postApiKey);

if (!posthog) return;

// Needed to send requests to PostHog in lambda
// https://posthog.com/docs/libraries/node#using-in-a-short-lived-process-like-aws-lambda
await posthog.shutdown();
}

export enum EventTypes {
query = "query",
Expand Down
5 changes: 2 additions & 3 deletions packages/lambdas/src/ihe-inbound-document-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { inboundDocumentQueryReqSchema } from "@metriport/ihe-gateway-sdk";
import * as Sentry from "@sentry/serverless";
import { getSecretValue } from "@metriport/core/external/aws/secret-manager";
import { processInboundDocumentQuery } from "@metriport/core/external/carequality/dq/process-inbound-dq";
import { analytics, EventTypes } from "@metriport/core/external/analytics/posthog";
import { analyticsAsync, EventTypes } from "@metriport/core/external/analytics/posthog";
import { getEnvVarOrFail, getEnvVar } from "@metriport/core/util/env-var";

const apiUrl = getEnvVarOrFail("API_URL");
Expand All @@ -23,8 +23,7 @@ export const handler = Sentry.AWSLambda.wrapHandler(async (event: string) => {
const postHogApiKey = await getSecretValue(postHogSecretName, region);

if (postHogApiKey && engineeringCxId) {
result.externalGatewayPatient?.system;
analytics(
await analyticsAsync(
{
distinctId: engineeringCxId,
event: EventTypes.inboundDocumentQuery,
Expand Down
4 changes: 2 additions & 2 deletions packages/lambdas/src/ihe-inbound-document-retrieval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Sentry from "@sentry/serverless";
import { getSecretValue } from "@metriport/core/external/aws/secret-manager";
import { getEnvVar, getEnvVarOrFail } from "@metriport/core/util/env-var";
import { processInboundDocumentRetrieval } from "@metriport/core/external/carequality/dr/process-inbound-dr";
import { analytics, EventTypes } from "@metriport/core/external/analytics/posthog";
import { analyticsAsync, EventTypes } from "@metriport/core/external/analytics/posthog";

const postHogSecretName = getEnvVar("POST_HOG_API_KEY_SECRET");
const engineeringCxId = getEnvVar("ENGINEERING_CX_ID");
Expand All @@ -21,7 +21,7 @@ export const handler = Sentry.AWSLambda.wrapHandler(async (event: string) => {
const postHogApiKey = await getSecretValue(postHogSecretName, region);

if (postHogApiKey && engineeringCxId) {
analytics(
await analyticsAsync(
{
distinctId: engineeringCxId,
event: EventTypes.inboundDocumentRetrieval,
Expand Down
4 changes: 2 additions & 2 deletions packages/lambdas/src/ihe-inbound-patient-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MPIMetriportAPI } from "@metriport/core/mpi/patient-mpi-metriport-api";
import { getEnvVarOrFail, getEnvVar } from "@metriport/core/util/env-var";
import { getSecretValue } from "@metriport/core/external/aws/secret-manager";
import { inboundPatientDiscoveryReqSchema } from "@metriport/ihe-gateway-sdk";
import { analytics, EventTypes } from "@metriport/core/external/analytics/posthog";
import { analyticsAsync, EventTypes } from "@metriport/core/external/analytics/posthog";
import * as Sentry from "@sentry/serverless";

const apiUrl = getEnvVarOrFail("API_URL");
Expand All @@ -27,7 +27,7 @@ export const handler = Sentry.AWSLambda.wrapHandler(async (event: string) => {
const postHogApiKey = await getSecretValue(postHogSecretName, region);

if (postHogApiKey && engineeringCxId) {
analytics(
await analyticsAsync(
{
distinctId: engineeringCxId,
event: EventTypes.inboundPatientDiscovery,
Expand Down

0 comments on commit ca9903f

Please sign in to comment.