0

I have Google Tag Manager set up on a website and want to manually trigger an event when a button is clicked (by manually I mean from Javascript).

All tutorials say that pushing into the dataLayer variable will result in the event being recorded, however I don't see any requests made in the Network tab, so I don't think that's working.

My code is simple:

jQuery('.sign-up-action').on('click', function (e) {
    dataLayer.push({'event': 'signup-btn-click'});
});

After clicking the button, dataLayer does have a new item, but since I don't see a request made to the GTM servers, I don't believe the event is saved.

Please note that I don't have access to the GTM admin panel, so I can't really check.

1

[I'll only speak to the event handling aspect of GTM, but you can find many more resources about GTM in general]

How come you are not doing this with the built in click listener (and possibly setting your trigger to fire when the Click Classes variable equals sign-up-action)? That may be easier than having to push to the dataLayer.

But anyway, if you absolutely must use a dataLayer push, then read on. Part of how GTM works is by listening for certain things to happen via the dataLayer such as clicks, link clicks, page loads, form submissions, and custom events. When any of these happen, an event gets pushed into the dataLayer, eg. gtm.dom for when the DOM has loaded, or gtm.click when the mouse is clicked on the page, or in your case when the signup-btn-click event gets pushed. When any event happens via the ever-magical dataLayer, GTM can react and fire off tags. In your case, you have defined the custom event, but you haven't defined the tag to fire yet. So what you'll need to do is to create a new event tag, populate it with appropriate event parameters, and, here's the key part, enable it to fire based on a trigger of signup-btn-click. This last part is important. And that's all there is to it. Once you've done that, you can add other fancy parameters to the tag, like custom dimensions or metrics and such.

  • I see, so basically whichever of the 2 ways I'll go, I'll have to do some magic in the GTM dashboard. I was trying to avoid that, since I don't really have access to it and the client probably doesn't know how to do that. Ah, I miss the good old days where there was only GA and not this fancy GTM stuff. Thanks! – Eduard Luca Feb 8 '17 at 15:45
  • Ah, GTM is wonderful once you get used to it! Saves so much time and trouble from manually coding things in. :D – nyuen Feb 8 '17 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.