I am trying to send an Event when clicked on a specific menu item. My header scripts are as follows:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-124755880-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-124755880-1');
</script>
<!-- Custom Google Analytics click event -->
<script>
jQuery(document).ready(function($){
function handleOutboundLinkClicks(event) {
console.log(event.target.href);
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: 'Book Now'
});
}
$('.book-link a').click(function(event){
console.log('click');
handleOutboundLinkClicks(event)
});
});
</script>
All console logs work as supposed, no errors on the site. I just can't see the event on my Google Analytics dashboard. Not in reports and not in the Real-time events.
The link has target="_blank"
attribute so I tried with and without transport: 'beacon'
. Nothing so far.
Do you have any idea what I am doing wrong?
Thank you
You are initializing GA using the gtag() method so you should be using the gtag() function call for sending events:
gtag('event', 'click', { 'event_category': 'Outbound Link','event_label': 'Book Now'
});
1) Check if call works on its own, without a click event. You can test the script by copy/pasting the gtag call into the console and see if it makes the connection or if it gives an error. If the connection works you will see a request in the "network" tab of the browsers devtools when manually calling the function.
2) Verify your browser can connect to GA. You can check the "network" tab in devtools to see if all the calls to analytics are being made or if they are blocked. If your running an ad-blocker or running the browser in a privacy/content blocking mode these tracker requests could be getting stopped. Depending on the browser you are using GA may already be blocked by default.
3) Verify analytics can see your requests. Check if analytics is seeing your actions in Realtime tracking. Look for the initial pageview first, if that isn't working make sure your testing with an unfiltered view.
来源:https://stackoverflow.com/questions/55420060/google-analytics-event-tracking-on-worpdress