static getLayoutShiftEvents()

in src/sdk/trace-metrics.ts [160:191]


  static getLayoutShiftEvents(traceEvents: Array<TraceEvent>) {
    const layoutShiftEvents = [];
    // Chromium will set `had_recent_input` if there was recent user input, which
    // skips shift events from contributing to CLS. This results in the first few shift
    // events having `had_recent_input` set to true, so ignore it for those events.
    // See https://crbug.com/1094974
    let ignoreHadRecentInput = true;

    for (const event of traceEvents) {
      // weighted_score_delta was added in chrome 90 - https://crbug.com/1173139
      if (
        event.name !== 'LayoutShift' ||
        !event.args?.data.is_main_frame ||
        !event.args?.data.weighted_score_delta
      ) {
        continue;
      }

      if (event.args.data.had_recent_input) {
        if (!ignoreHadRecentInput) continue;
      } else {
        ignoreHadRecentInput = false;
      }

      layoutShiftEvents.push({
        ts: event.ts,
        weightedScore: event.args.data.weighted_score_delta,
      });
    }

    return layoutShiftEvents;
  }