最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google tag manager - GTM JavaScript Compiler Error ECMASCRIPT6 - Stack Overflow

programmeradmin8浏览0评论

My question is about I use JavaScript to send some form data from Webflow to Typeform. When I create a custom-html tag in Google Tagmanager I get this error:

JavaScript Compiler Error Typeform Tag
Error at line 3, character 1: This language feature is only supported for ECMASCRIPT6 mode or better: const declaration.

The same error occurs for line 4,5,6, 13, 14, 15.

This is my code:

<script>
  $( "#formbutton" ).click(function() {
    const naam = $('#Naam-2').val();
    const email = $('#Email-2').val();
    const postcode = $('#Postcode-2').val();
    Cookies.set('naam', naam, { expires: 30 } );
    Cookies.set('email', email, { expires: 30 } );
    Cookies.set('postcode', postcode, { expires: 30 } );
  });

  var Webflow = Webflow || [];
  Webflow.push(function() {
    const naam = Cookies.get("naam");
    const email = Cookies.get("email");
    const postcode = Cookies.get("postcode");
    $('#naam').val(naam);
    $('#email').val(email);
    $('#postcode').val(postcode);
  });
</script>

Please, share your thoughts or any advices, would highly appreciate it! - thanks you in advance.

My question is about I use JavaScript to send some form data from Webflow to Typeform. When I create a custom-html tag in Google Tagmanager I get this error:

JavaScript Compiler Error Typeform Tag
Error at line 3, character 1: This language feature is only supported for ECMASCRIPT6 mode or better: const declaration.

The same error occurs for line 4,5,6, 13, 14, 15.

This is my code:

<script>
  $( "#formbutton" ).click(function() {
    const naam = $('#Naam-2').val();
    const email = $('#Email-2').val();
    const postcode = $('#Postcode-2').val();
    Cookies.set('naam', naam, { expires: 30 } );
    Cookies.set('email', email, { expires: 30 } );
    Cookies.set('postcode', postcode, { expires: 30 } );
  });

  var Webflow = Webflow || [];
  Webflow.push(function() {
    const naam = Cookies.get("naam");
    const email = Cookies.get("email");
    const postcode = Cookies.get("postcode");
    $('#naam').val(naam);
    $('#email').val(email);
    $('#postcode').val(postcode);
  });
</script>

Please, share your thoughts or any advices, would highly appreciate it! - thanks you in advance.

Share Improve this question edited Nov 15, 2018 at 0:52 picsoung 6,5381 gold badge20 silver badges36 bronze badges asked Nov 14, 2018 at 14:48 ElienElien 411 silver badge2 bronze badges 4
  • Hi Elien, the problem is right in the error message. Declaring a variable as const is an ES6 feature and GTM does not support that. It should work if you just remove the const keyword. – Eike Pierstorff Commented Nov 14, 2018 at 16:12
  • Hi Elien, Why don't you embed directly a Typeform in Webflow? Otherwise Eike's ments seems to be the way to go. – picsoung Commented Nov 15, 2018 at 0:53
  • Thanks @EikePierstorff, if I remove the 'const' the error indeed disappeared. However, the cookie is nog placed on the form fields of "Naam", "E-mail" and "Postcode". How can I make the cookie work so I'll be able to see it in the URL on the next page? – Elien Commented Nov 15, 2018 at 12:22
  • It turned out to be blocked by our cookie consent, the code is indeed working without 'const'. Thanks for your response @EikePierstorff it worked out and @NicolasGrenie! – Elien Commented Nov 20, 2018 at 16:31
Add a ment  | 

2 Answers 2

Reset to default 5

const and let are different ways to declare variables only available in ES6 (a version of JavaScript) or later. Google Tag Manager does not support ES6 as I found out today when I used another ES6 feature of arrow functions.

In your case, changing the const keyword, or any instance of let to the keyword var would likely fix your issue.

I see your actual problem was cookie consent, but if anyone runs into ES6 or Ecmascript 6 errors in GTM. Search the ES6 feature online for a replacement using ES5. Other acronyms are ES2016 vs ES2015. Don't ask me about the naming convention because it is super confusing.

Another trick would be to paste your code into an online version of BabelJS and see what it spits out when you use the ES2015 checkbox. BabelJS is a transpiler/piler that takes future syntax JS and converts it into older version syntax. For example, see the before and after code that was spit out:

Original ES6 Code (Uses arrow function, const, and let):

window.addEventListener("load", (event) => {
  const myVariable = "text";
  let anotherVariable = 8;
});

Output from BabelJS into ES2015/ES5:

window.addEventListener("load", function (event) {
  var myVariable = "text";
  var anotherVariable = 8;
});

I convert es6 code to the old version of js vanilla try this tool:

https://es6console./

发布评论

评论列表(0)

  1. 暂无评论