Categories
GTM Javascript

Use Custom Javascript Variable in GTM to get multiple elements

The problem: I need to put all the elements that contain the class “has-error” inside a variable

In order to do it I need to return a string with all the elements ids concatinated.

function() {
  var els = document.querySelectorAll('li.has-error');
  var ids = []
  for (var i = 0; i < els.length; i++) {
    ids.push(els[i].getAttribute('id'));
  }
  return ids.join();
}

Categories
Analytics GTM

Using GTM to fire Tags in Legacy Pages but not in WordPress

Issue: Make the GTM recognize if you are on a wordpress page and if you are not then fire a tag

 

Step #1: Create a DOM Variable in GTM to store the information of whether a page is built with WordPress or not.



I have identified that the pages in my wordpress site all have a class in Elementor named “wp-page”. So I use CSS Selector and get it with [data-elementor-type=”wp-page”].

 

Step #2 Build a DOM Ready Trigger and use as trigger the above DOM variable.

This triggers works as the lever of the whole process. If the DOM – Element WordPress is not “null” (meaning that the page I currently am is WordPress), then don’t fire.

If the page is legacy then the DOM – Element WordPress is “null” meaning that the trigger should fire.



Step #3 Attach the trigger to a tag