# Core web vitals module for Unified Digital
> Track Core Web Vitals metrics including LCP, FID, CLS, INP, and TTFB with the Core Web Vitals module.
> Source: https://docs.snowplow.io/docs/modeling-your-data/modeling-your-data-with-dbt/dbt-models/dbt-unified-data-model/core-web-vitals-module/
> Documentation index: https://docs.snowplow.io/llms.txt

[v1.0.0](https://github.com/snowplow/dbt-snowplow-unified "Latest Unified Digital package version")

This custom module is built as an extension of the dbt-snowplow-unified package, it transforms raw `web vitals` event data into derived tables for easier querying. These events are generated by the **Snowplow Web Vitals plugin** (@snowplow/browser-plugin-web-vitals) of the [JavaScript tracker](/docs/sources/web-trackers/).

To enable this optional module, the unified package must be correctly configured. Please refer to the [quickstart guides](/docs/modeling-your-data/modeling-your-data-with-dbt/dbt-quickstart/) for a full breakdown of how to set it up.

## Prerequisites

In order to use this module you would need to:

1. Track core web vitals using the web `Snowplow Web Vitals plugin`, which populates the column/table `unstruct_event_com_snowplowanalytics_snowplow_web_vitals_1`
2. Have the [yauaa enrichment](/docs/pipeline/enrichments/available-enrichments/yauaa-enrichment/) enabled (for device type and device class), which populates `contexts_nl_basjes_yauaa_context_1`
3. (Ideally, but not necessarily) Have the [spiders and bots](/docs/pipeline/enrichments/available-enrichments/iab-enrichment/) enrichment enabled, which populates `contexts_com_iab_snowplow_spiders_and_robots_1`

## Overview

This custom module consists of a series of dbt models which produce the following aggregated models from the raw web vitals events:

- `snowplow_unified_web_vitals`: Incremental table used as a base for storing core web vital events (first event per page view).
- `snowplow_unified_web_vital_measurements`: Drop and recompute table to use for visualizations that takes core web vital measurements at the user specified percentile point (defaulted to 75).

## Operation

It is assumed that the Snowplow unified package is already installed and configured as per the [Quick Start](/docs/modeling-your-data/modeling-your-data-with-dbt/dbt-quickstart/) instructions.

### Enable the module

You can enable the custom module through the `snowplow__enable_cwv` variable in your `dbt_project.yml` file:

```yaml

vars:
snowplow_unified:
  snowplow__enable_cwv: true
  
```

### Override the module specific macros

> **Tip:** For information about overriding our macros, see [here](/docs/modeling-your-data/modeling-your-data-with-dbt/package-mechanics/overridable-macros/)

- The `core_web_vital_page_groups()` [(source)](https://github.com/snowplow/dbt-snowplow-unified/blob/main/macros/core_web_vital_page_groups.sql) macro is used to let the user classify their urls to specific page groups. It returns the sql to provide the classification (expected in the form of case when statements).

- The `core_web_vital_results_query()` [(source)](https://github.com/snowplow/dbt-snowplow-unified/blob/main/macros/core_web_vital_results_query.sql) macro is used to let the user classify the thresholds to be applied for the measurements. It returns the sql to provide the logic for the evaluation based on user defined thresholds (expected in the form of case when statements).

Please make sure you set the results you would like the measurements to pass to **`good`** or align it with the `macro_core_web_vital_pass_query()` macro.

- The `core_web_vital_pass_query()` [(source)](https://github.com/snowplow/dbt-snowplow-unified/blob/main/macros/core_web_vital_pass_query.sql)

```sql
case when lcp_result = 'good' and fid_result = 'good' and cls_result = 'good' then 1 else 0 end passed
```

### Run the module

If you have previously run the model without this optional module enabled, you can simply enable the module and run `dbt run --selector snowplow_unified` as many times as needed for this module to catch up with your other data. If you only wish to process this from a specific date, be sure to change your `snowplow__start_date`, or refer to the [Custom module](/docs/modeling-your-data/modeling-your-data-with-dbt/dbt-custom-models) section for a detailed guide on how to achieve this the most efficient way.

If you haven't run the web package before, then you can run it using `dbt run --selector snowplow_unified` either through your CLI, within dbt Cloud, or for Snowplow CDI customers you can use Snowplow Console. In this situation, all models will start in-sync as no events have been processed.
