Search code examples
javascriptgoogle-tag-managerarrow-functions

Converting arrow function to regular function - For use in Google Tag Manager


I want to use that script in GTM:

<script>
var isOverIFrame = false
var iframes = window.document.querySelectorAll('iframe');
function trackIframeClicks(frames){
  window.addEventListener('blur',function(e) {
    frames.forEach(function (frame, index) {
      if (frame.mouseOver) {
        window.dataLayer.push({
          'event': 'ifameClick',
          'frameSource': frame.src
        });
        // console.log(frame.src);
      }
    })
  });
}
function setListeners (frames) {
  frames.forEach(function(frame) {
    frame.mouseOver = false
    frame.addEventListener('mouseenter', () =>{
      frame.mouseOver = true
      // console.log('mouse in iframe')
    });
    frame.addEventListener('mouseleave', () =>{
      frame.mouseOver = false
      // console.log('mouse out of iframe')
    });
  })
}
setListeners(iframes);
trackIframeClicks(iframes);
</script>

When trying to publish it I get the following error:

Error in line 20 & 24, character 42: This language feature is only supported for ECMASCRIPT_2015 mode or better: arrow function.

Can someone please help rewriting the function so it will work without arrow functions?

Many thanks in advance.


Solution

  •     function setListeners (frames) {
      frames.forEach(function(frame) {
        frame.mouseOver = false
        frame.addEventListener('mouseenter',function enter (){
          frame.mouseOver = true
          // console.log('mouse in iframe')
        });
        frame.addEventListener('mouseleave',function leave () {
          frame.mouseOver = false
          // console.log('mouse out of iframe')
        });
      })
    }