You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
In the [attribution build](#attribution-build) each of the metric functions has two primary differences from their standard build counterparts:
792
792
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.
794
794
795
-
```ts
796
-
interfaceAttributionReportOptsextendsReportOpts {
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
+
interfaceAttributionReportOptsextendsReportOpts {
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
+
functioncustomGenerateTarget(el) {
807
+
if (el.dataset.name) {
808
+
returnel.dataset.name;
809
+
}
810
+
811
+
// Do some other fallback when data-name doesn't exist (not-shown).
812
+
}
800
813
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.
0 commit comments