I'm trying to add a simple solution to add event tracking for PDF files to google analytics.
I can't figure out, why the events are not tracked. If I manual add:
<a href="/my-file.pdf" target="_blank" onclick="gtag('event', 'click', {'event_category' : 'PDF download','event_label' : 'label123'});">Download the PDF</a>
to a link, it works fine.
It seems
var path = $(this).attr('href');
causes the issue.
What am I doing wrong?
<script>
(function($){
$('a[href$=\'.pdf\']').click(function() {
var path = $(this).attr('href');
gtag('event', 'click', {'event_category' : 'PDF download','event_label' : path});
});
});
</script>
thanks!