简体   繁体   English

JS 代码在 Google 标签管理器中产生错误

[英]JS code generates error in Google Tag Manager

I am trying to add following code in GTM to measure Web Core Vitals我正在尝试在 GTM 中添加以下代码来衡量 Web Core Vitals

<script type="text/javascript">
new PerformanceObserver((entryList) => {    
    for (const entry of entrytList.getEntries()) {
        const elm = entry.element;
        console.log(elm);
    }   
}).observe({type: 'largest-contentful-paint', buffered:true});
</script>

This code works in Console but when i try to publish it in GTM it generates error message as below此代码在控制台中有效,但是当我尝试在 GTM 中发布它时,它会生成如下错误消息

Not sure what is wrong with the code as it should support ECMA16 for GTM不确定代码有什么问题,因为它应该支持 GTM 的 ECMA16

在此处输入图片说明

Yes, GTM is pretty slow when it comes to ES6 adoption.是的,就 ES6 的采用而言,GTM 非常缓慢。 It partially adopted it for things like templates, but not fully even there.它在模板之类的东西上部分采用了它,但甚至没有完全采用它。 And obviously it didn't adopt it for custom html tags.显然它没有将它用于自定义 html 标签。

What you'll have to do is rewrite your code to be ES5 compliant.您需要做的是重写您的代码以符合 ES5。 Or you can use babel to do an automated transition.或者您可以使用 babel 进行自动转换。 it will look like this:它看起来像这样:

 new PerformanceObserver(function (entryList) { var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = entrytList.getEntries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var entry = _step.value; var elm = entry.element; console.log(elm); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } }).observe({ type: 'largest-contentful-paint', buffered: true });

You probably don't need all the try catch and finally, but babel is being honest with what it does.你可能不需要所有的 try catch 和 finally,但是 babel 对它的作用是诚实的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM