export async function getDesktopCTRPercentData()

in lib/looker.ts [223:291]


export async function getDesktopCTRPercentData(
  id: string,
  template: string,
  channel?: string,
  experiment?: string,
  branch?: string,
  startDate?: string | null,
  endDate?: string | null,
): Promise<CTRData | undefined> {
  // XXX the filters are currently defined to match the filters in getDashboard.
  // It would be more ideal to consider a different approach when definining
  // those filters to sync up the data in both places. Non-trivial changes to
  // this code are worth applying some manual performance checking.
  let queryResult;
  if (template === "infobar") {
    queryResult = await runQueryForSurface(
      template,
      {
        "messaging_system.metrics__text2__messaging_system_message_id": id,
        "messaging_system.normalized_channel": channel,
        "messaging_system.metrics__string__messaging_system_ping_type":
          template,
        "messaging_system__ping_info__experiments.key": experiment,
        "messaging_system__ping_info__experiments.value__branch": branch,
      },
      startDate,
      endDate,
    );
  } else {
    queryResult = await runQueryForSurface(
      template,
      {
        "event_counts.message_id": "%" + id + "%",
        "event_counts.normalized_channel": channel,
        "onboarding_v1__experiments.experiment": experiment,
        "onboarding_v1__experiments.branch": branch,
      },
      startDate,
      endDate,
    );
  }

  if (queryResult?.length > 0) {
    // CTR percents will have 2 decimal places since this is what is expected
    // from Experimenter analyses.
    let impressions;
    if (template === "infobar") {
      const pingCount = queryResult[0]["messaging_system.ping_count"];
      const events =
        pingCount["messaging_system.metrics__string__messaging_system_event"];
      impressions = events["IMPRESSION"];
    } else {
      const userCount = queryResult[0]["event_counts.user_count"];
      const action = userCount.action;
      impressions = action[" Impression"];
    }

    const primaryRate = queryResult[0].primary_rate;

    const ctrPercent = getSafeCtrPercent(primaryRate);

    return {
      ctrPercent: ctrPercent,
      impressions: impressions,
    };
  }

  return undefined;
}