I am attempting to send a gtag event with a variable as an argument. I know that the function fires, and I can see it in GA, but the problem is the $anchorText
variable being passed. I am trying to capture the anchor text of the link that was clicked, then pass that variable to the gtag function.
I am quite new to javascript and jquery... learning as I go. The environment I am working with is a WordPress site theme built with bootstrap. Referring to the documentation, I am attempting to send a GA event when a site visitor clicks one of the accordion widgets. Bootstrap automatically sends a show.bs.collapse
event when one of the options is clicked, I am just trying to capture the text of that link and pass it to the gtag function.
For reference, the theme's demo site has the HTML structure, if needed.
<script>
jQuery(function($){
var $anchorText = $('.panel-title a').click(function(){$(this).text()});
$('.panel-group').on('show.bs.collapse',function(){
gtag('event', 'Clicked', {'event_category':'Interactions', 'event_label':'Accordion: ' + $anchorText})
});
});
</script>
Right now, the 'Event Label' in GA displays: Accordion: [object Object]
. I am expecting to see Accordion: FAQ Example Question
and so on, depending on the link that was clicked.
I'm going by what I can find online, but I believe the way I am assigning the anchor text/value to the variable is where my goof is. Any help or direction on what I can do to fix this is appreciated.