0

I have edit permissions on google tag manager. Is it possible from this tool to collect in the datalayer the coordinates (x, y) of an event when it is triggered?

It's possible that the web element from which the event jumps changes position in time.

How to do it?

Mario M.
  • 802
  • 11
  • 26

2 Answers2

1

This function can help you to find the position of the clicked element listening the event and detecting the position via the jQuery function of position(), all of this adding a listener, this code needs to be implemented on the page or inside a custom HTML tag

    $("a").click(function(event) {
        var loc = $(this).position();
        dataLayer = window.dataLayer || [];
        dataLayer.push({ 'event' : 'click_element' , 'posy' : loc.top , 'posx': loc.left });
    });

Also must be a clean way to do this inside a var, but it's a little more complicated to create that function.

Greetings

------------- Old Answer -----------------

What type of event are you talking about? this is a click? or is a event lauched by a Javascript? For example for a function on your head what you determine as X and Y?

Maybe determine what type of events can guide better the answer, this case, for example, you can see an example of clickEvent Position.

This function can be helpful, this track your mouse location and pushes the information to the dataLayer.

var posX;
var posY;
document.onmousemove = function(e){
      posX= e.pageX;
      posY= e.pageY;
};
function mouseLocation(){
     if(typeof window.dataLayer === 'undefined'){ window.dataLayer =  window.dataLayer || [] };
     dataLayer.push({ 'event' : 'mouseLocation', 'PosX' : posX, 'PosY' : posY});
};
  // paste this on the head or inside an HTML tag. 

   mouseLocation(); //Use this function on the onClick on the object to be track

You can try to add this as a variable adding a return on the function if you want to be used with a click listener. Remember that the location depends on the aspect ratio and resolution, so the button can be on the same place, but the X and Y location might not be the same.

Kemen Paulos Plaza
  • 1,560
  • 9
  • 18
0

In the case of click event you can get the coordinates of the event activation.

How to do?

  • Write a simple JS function to get the x,y co-ordinates on a mouse click.
  • Push the values into the datalayer with some event key.
  • Fire the tag through GTM on that data Layer event
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47