Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python
3.4 KB
Raw Blame
import { AnalyticsBrowser } from "@segment/analytics-next";

let analytics: AnalyticsBrowser | null = null;
let _environment = "";

export function initAnalytics(writeKey: string, environment = "") {
  _environment = environment;
  if (!writeKey || analytics) return;
  analytics = AnalyticsBrowser.load({ writeKey });
}

interface RequiredSegmentStaticProperties {
  UT30: string;
  environment: string;
  productCode: string;
  productCodeType: string;
  productPlanName: string;
  productTitle: string;
  category: string;
  accountPlan: string;
}

// These properties are required by IBM Segment event schema for all events or they will be blocked
// See: https://w3.ibm.com/w3publisher/instrumentation-at-ibm/required-properties
export const getRequiredStaticProperties =
  (): RequiredSegmentStaticProperties => ({
    UT30: "30AW0",
    environment: _environment,
    productCode: "WW1544",
    productCodeType: "WWPC",
    productPlanName: "enterprise-mcsp",
    productTitle: "IBM watsonx.data as a Service",
    accountPlan: "PAYG",
    category: "OpenRAG wxd",
  });

export const page = (
  pageTitle?: string,
  properties: Record<string, unknown> = {},
) => {
  if (!analytics) return;
  analytics.page(undefined, pageTitle, {
    ...getRequiredStaticProperties(),
    ...properties,
  });
};

export const identify = (
  userId: string,
  traits: Record<string, unknown> = {},
) => {
  if (!analytics || !userId) return;
  analytics
    .identify(userId, traits)
    .catch((e) => console.error("Analytics identify error:", e));
};

const track = (eventName: string, properties: Record<string, unknown> = {}) => {
  if (!analytics) return;
  try {
    analytics.track(eventName, {
      ...getRequiredStaticProperties(),
      ...properties,
    });
  } catch (e) {
    console.error("Analytics tracking error:", e);
  }
};

interface ButtonEventParams {
  action?: string;
  channel?: string;
  CTA: string;
  elementId?: string;
  namespace?: string;
  payload?: string | Record<string, unknown>;
  platformTitle?: string;
}

export const trackButton = <T = Record<string, unknown>>({
  action = "clicked",
  ...rest
}: T & ButtonEventParams): void =>
  track("CTA Clicked", { action, ...rest } as Record<string, unknown>);

interface StartProcessParams {
  processType: string;
  process?: string;
  category?: string;
}

export const trackStartProcess = <T = Record<string, unknown>>(
  props: T & StartProcessParams,
): void => track("Started Process", props as Record<string, unknown>);

interface EndProcessParams {
  processType: string;
  process?: string;
  successFlag?: boolean;
  resultValue?: string;
  category?: string;
}

const trackEndProcess = <T = Record<string, unknown>>(
  props: T & EndProcessParams,
): void => track("Ended Process", props as Record<string, unknown>);

export const trackProcessSuccess = <T = Record<string, unknown>>(
  props: T & Omit<EndProcessParams, "successFlag">,
): void =>
  trackEndProcess({ ...props, successFlag: true } as T & EndProcessParams);

export const trackProcessFailure = <T = Record<string, unknown>>(
  props: T & Omit<EndProcessParams, "successFlag">,
): void =>
  trackEndProcess({ ...props, successFlag: false } as T & EndProcessParams);

interface LLMCallParams {
  model?: string;
  objectType?: string;
  taskId?: string;
  mode?: string;
  inputTokens?: number;
  outputTokens?: number;
}

export const trackLLMCall = <T = Record<string, unknown>>(
  props: T & LLMCallParams,
): void => track("LLM Call", props as Record<string, unknown>);