0

I need some way to access the coordinates of the mouse when a click happens. I've looked at the built-in variables, but none seem to contain the coordinate information. Also I've seen codes which solve the problem, but need to be implemented on the page. I need something, if it's possible, entirely on google tag manager.

2 Answers2

0

There isn't a built-in variable for mouse position in GTM. In this case you'll need to create a Custom JS variable to retrieve this info.

This answer shows how to get this info using pure JS.

0

I ended up creating a custom HTML tag and firing it with the gtm.click events.

Here is the code of the tag:

<script>
  (function() {
    var el = document.body;
    var ell = document.documentElement

    document.onclick = function(e) {
      window.dataLayer.push({
        event: 'custom.event.' + e.type,
        coord_X: e.clientX + el.scrollLeft + ell.scrollLeft,
        coord_Y: e.clientY + el.scrollTop + ell.scrollTop
      });
    };    
  })();
</script>

Huge inspiration from this link