1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

this language feature is only supported for ECMASCRIPT6 mode or better 【Google Tag Manager】

Last updated at Posted at 2022-05-28

Google Tag Manager でカスタムHTMLを作成していたとき、次のようなエラーに遭遇

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

同様に、line4, 5, 6, 7, 11, 22 行目もエラーがあるらしい

書いていたコードは以下

<script>
 function formatDate(dt) {
    const y = ('00'+ dt.getFullYear()).slice(-2);
    const m = ('00' + (dt.getMonth()+1)).slice(-2);
    const d = ('00' + dt.getDate()).slice(-2);
    const h = ('00' + dt.getHours()).slice(-2);
    const mm = ('00' + dt.getMinutes()).slice(-2);
    return (y  + m + d +  h  + mm);
 }
 nowDate = formatDate(new Date());
 const getAfbItem = `${nowDate}`;

if (!window.dtlpcvCvConf) {
  window.dtlpcvCvConf = [];
}
window.dtlpcvCvConf.push({
  siteId: "",
  commitData: {
    pid: "",
    amount: "",
    mid: "",
    u: `${getAfbItem}`, 
    t:"",
    cvinfo:"1.1.1"
    }
});
</script>

どうやらGTMはES6に対応していないとのこと :(

解決方法

以下の記事を参考に書き直しました

JavaScript ES5以前とES6(ES2015)以降の違いについて

書き直したコード↓

<script>
 function formatDate(dt) {
    var y = ('00'+ dt.getFullYear()).slice(-2);
    var m = ('00' + (dt.getMonth()+1)).slice(-2);
    var d = ('00' + dt.getDate()).slice(-2);
    var h = ('00' + dt.getHours()).slice(-2);
    var mm = ('00' + dt.getMinutes()).slice(-2);
    return (y  + m + d +  h  + mm);
 }
 nowDate = formatDate(new Date());
 var getAfbItem = nowDate;

if (!window.dtlpcvCvConf) {
  window.dtlpcvCvConf = [];
}
window.dtlpcvCvConf.push({
  siteId: "",
  commitData: {
    pid: "",
    amount: "",
    mid: "",
    u: "getAfbItem", 
    t:"",
    cvinfo:"1.1.1"
    }
});
</script>

これでエラーは無くなりました

参考記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?