Reputation: 4539
First time working with Google Tag Manager
. I have a click event trigger based on a button class, yet it executes on every click.
My variable {{bookingUnit}}
is gets its value from text taken off the HTML on the page. It is a Custom Javascript variable (and returns the correct value):
function() {
return $({{Click Element}}).closest('table')).find('td.rnc').text();
}
I have to detect a button click which on the page (of a 3rd party system) was defined as:
<input type="submit" name="ctl00$plcBody$BookingFormGrid$ctl16" value="BOOK NOW" onclick="$('form').attr('action', 'DetailsEntry.aspx?bfid=c5efdee5-7280-4593-8256-564e9b7ac01a&bfpid=8072faaa-c89f-4a63-be19-e54a91f0f1a0&bfrtid=3d6e96c3-e557-4f85-97d1-c750787dc6d8&arr=2019-03-07&nts=3&a=1&c=0');WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$plcBody$BookingFormGrid$ctl16", "", true, "", "", false, false))" class="btn btn-primary bnow">
I have a trigger as follows:
Type: Click - All Elements
Fires On - Some Clicks
Click Classes - equals - btn btn-primary bnow
Click Element - equals - input
Wherever I click on the page I see the event firing:
gtm.click
Yet I can see the value of my variable being updated every time (so it seems to ignore my Click Classes
trigger condition and only update when the button is pressed).
What did I miss? Thanks.
UPDATE: Using info from this forum, I changed my trigger like so:
Click Element - matches CSS selector - .btn .btn-primary .bnow
But still the variable gets updated by clicking anywhere on the page.
Upvotes: 0
Views: 1954
Reputation: 11
The gtm.click event you are seeing is not your event firing - it is simply one of GTM's default events. If your tag had fired you would instead see your tag name in the preview.
It sounds like you want to send an event to GA every time your trigger fires along with the value of {{bookingUnit}}, if this is the case:
From here you could:
Once this is done you can attach your CSS selector trigger to this tag (Click Element - matches CSS selector - .btn .btn-primary .bnow) this will then fire an event every time the matched selector is clicked to your GA account. You can check for the tag firing in preview mode, just look out for the name of your GA event tag.
Upvotes: 0
Reputation: 1684
It sounds like you have a Click - All Elements
trigger active in your container. This will fire with every mouse click. Every time a trigger is fired, all variables will be re-evaluated at that time. So the best thing to do is to deactivate the Click - All Elements
trigger if you are not using it.
Edit:
Click Element, Click Classes, Click ID, Click Target, Click URL, Click Text
Click - All Elements
Some Clicks
Click Classes
matches CSS selector
.btn.btn-primary.bnow
Upvotes: 2
Reputation: 1
Do it like this:
Type: Click - All Elements Fires On - Some Clicks
Click Classes - equals - btn
Just use one class and one condition. Also make sure that it's the only button with that class or that only the buttons you want to track have that class.
In the debug console you will always see the event gtm.click firing but the variable should only be sent when clicking the button with that particular condition.
Upvotes: 0
Reputation: 32780
As bit more technical detail - GTM does not bind events to the element specified by the classes in your filter. Instead it binds the event to the document, waits for the event to bubble up to the root element of the page, and then inspects the event target if it matches your filters.
As vinoaj said the net effect is that the trigger is fired every time you click anywhere on the page. That also means that the Click Element changes everytime, and with it the return value for the selector in your custom variable.
However the actions connected to the trigger will only fire if the event target matches the conditions in your trigger. So this should actually not create a problem for you.
Upvotes: 0