我正在尝试使用 GTM 将数据从 webVitals 发送到谷歌分析。
使用这个包:https ://github.com/GoogleChrome/web-vitals#load-web-vitals-from-a-cdn
<script defer src="https://unpkg.com/web-vitals@1.1.0/dist/web-vitals.umd.js"></script>
<script>
function sendToGoogleAnalytics ({name, delta, id}) {
// Assumes the global `gtag()` function exists, see:
// https://developers.google.com/analytics/devguides/collection/gtagjs
gtag('event', name, {
event_category: 'Web Vitals',
// The `id` value will be unique to the current page load. When sending
// multiple values from the same page (e.g. for CLS), Google Analytics can
// compute a total by grouping on this ID (note: requires `eventLabel` to
// be a dimension in your report).
event_label: id,
// Google Analytics metrics must be integers, so the value is rounded.
// For CLS the value is first multiplied by 1000 for greater precision
// (note: increase the multiplier for greater precision if needed).
value: Math.round(name === 'CLS' ? delta * 1000 : delta),
// Use a non-interaction event to avoid affecting bounce rate.
non_interaction: true,
});
}
addEventListener('DOMContentLoaded', function() {
webVitals.getCLS(sendToGoogleAnalytics);
webVitals.getFID(sendToGoogleAnalytics);
webVitals.getLCP(sendToGoogleAnalytics);
});
</script>
GTM 抱怨: 第 3 行错误,字符 38:此语言功能仅支持 ECMASCRIPT6 或更好的模式:对象解构。
我认为问题出在函数调用上。我试图将函数修改为:
var sendToGoogleAnalytics = function({name, delta, id}){...
似乎这不是问题。有人能指出 linter 不喜欢什么吗?