How to make GTM tags wait for jQuery to load?

I would like to track whether a toast (or any "pop-up") will be displayed using Google Analytics event tracking through GTM. Whether the toast is displayed or not is determined by the jQuery code and based on such cookie information

function ShowToast(Msg){
    $('#toast-msg').html(Msg);
    $('#toast').animate({ left: '-10px' });
}

called up

<script type="text/javascript">
    $(function() {
        ShowToast("SOME HTML");
    });
</script>

This is what I got in GTM so far using a user variable

function(){
    if(document.querySelector("#toast #toast-msg").length > 0){
        return true;
    }
}

when a trigger listens on this variable as true and as a regular universal analytics event tag. The idea is to simply check if toast-msg is shown, which works great in preview mode.

: gtm.js( ), jQuery , gtm.js . , , , .

GTM Javascript/JQuery, , JQuery GTM-//? ?

+4
3
  • , , <head> , : <script>window.dataLayer = window.dataLayer || [];</script>
  • jQuery ( , ) dataLayer.push({'event': 'event_name'});
  • Custom Event GTM event_name, .
  • GA
+3

dataLayer, , , :

  • errenous code: document.querySelector("#toast #toast-msg").innerHTML.length > 0 innerHTML
  • jQuery, gtm.dom, .
0

Source: https://habr.com/ru/post/1651386/


All Articles