Skip to content

Commit d98f066

Browse files
Update README with example for generateTarget (#619)
* Update README with example for generateTarget * Apply suggestions from code review Co-authored-by: Philip Walton <[email protected]> --------- Co-authored-by: Philip Walton <[email protected]>
1 parent ac3dbcb commit d98f066

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ A subclass of `ReportOpts` used for each metric function exported in the [attrib
648648

649649
```ts
650650
interface AttributionReportOpts extends ReportOpts {
651-
generateTarget?: (el: Node | null) => unknown;
651+
generateTarget?: (el: Node | null) => string;
652652
}
653653
```
654654

@@ -790,15 +790,29 @@ console.log(LCPThresholds); // [ 2500, 4000 ]
790790

791791
In the [attribution build](#attribution-build) each of the metric functions has two primary differences from their standard build counterparts:
792792

793-
1. They accept an `AttributionReportOpts` objects instead of a `ReportOpts` object. The `AttributionReportOpts` object supports an additional, optional, `generateTarget()` function that lets developers customize how DOM elements are stringified for reporting purposes. When passed, the return value `generateTarget()` function will be used for any "target" properties in the following attribution objects: [`CLSAttribution`](#CLSAttribution), [`INPAttribution`](#INPAttribution), and [`LCPAttribution`](#LCPAttribution).
793+
1. Their callback is invoked with a `MetricWithAttribution` objects instead of a `Metric` object. Each `MetricWithAttribution` extends the `Metric` object and adds an additional `attribution` object, which contains potentially-helpful debugging information that can be sent along with the metric values for the current page visit in order to help identify issues happening to real-users in the field.
794794

795-
```ts
796-
interface AttributionReportOpts extends ReportOpts {
797-
generateTarget?: (el: Node | null) => string;
798-
}
799-
```
795+
2. They accept an `AttributionReportOpts` objects instead of a `ReportOpts` object. The `AttributionReportOpts` object supports an additional, optional, `generateTarget()` function that lets developers customize how DOM elements are stringified for reporting purposes. When passed, the return value of the `generateTarget()` function will be used for any "target" properties in the following attribution objects: [`CLSAttribution`](#CLSAttribution), [`INPAttribution`](#INPAttribution), and [`LCPAttribution`](#LCPAttribution).
796+
797+
```ts
798+
interface AttributionReportOpts extends ReportOpts {
799+
generateTarget?: (el: Node | null) => string;
800+
}
801+
```
802+
803+
For example, if a web page has unique `data-name` attibute on many elements, you may prefer to use those over the built-in selector-style strings that are generated by default.
804+
805+
```js
806+
function customGenerateTarget(el) {
807+
if (el.dataset.name) {
808+
return el.dataset.name;
809+
}
810+
811+
// Do some other fallback when data-name doesn't exist (not-shown).
812+
}
800813

801-
2. Their callback is invoked with a `MetricWithAttribution` objects instead of a `Metric` object. Each `MetricWithAttribution` extends the `Metric` object and adds an additional `attribution` object, which contains potentially-helpful debugging information that can be sent along with the metric values for the current page visit in order to help identify issues happening to real-users in the field.
814+
onLCP(sendToAnalytics, {generateTarget: customGenerateTarget});
815+
```
802816

803817
The next sections document the shape of the `attribution` object for each of the metrics:
804818

0 commit comments

Comments
 (0)