Sudha
Sudha

Reputation: 21

Datalayer not getting push on click event?

I m having trouble pushing datalayer to gtm on click event. But datalayer gets properly pushed on a load of the page but not on click. below is my code

    $('.load-test-div').on("click", function () {
      //console.log('Called properly');
      dataLayer.push({
        'event': 'display more',
      });
    });

I have manipulated code in a different way still it's not pushed. For testing, I m using the "Tag Manager" addon of chrome. Is there are any setting to capture click event in google tag manager. Please help!

Upvotes: 0

Views: 4971

Answers (1)

ASomN
ASomN

Reputation: 198

Without seeing more I can only guess. For example, this works: https://jsfiddle.net/emo38rbv/

dataLayer = window.dataLayer || [];
document.querySelector('.load-test-div').addEventListener('click', function(){
    dataLayer.push({
    'event': 'display more',
  });
  alert('this has pushed to the dataLayer: '+JSON.stringify(dataLayer[dataLayer.length -1]));
});

The issue could be:

  • Your function is being called before jquery is loaded.
  • The dataLayer hasn't been declared
  • The DOM is not ready (the query selector can't find the element)
  • The element does not exist at the time you run your function

Upvotes: 1

Related Questions