The article provides a comprehensive guide on how to track events with Google Tag (gtag.js) and send them to GA4.
Assumptions made are that you have gtag.js installed on your site and you have a familiarity with JavaScript.
To track events using gtag, a solid familiarity with JavaScript is required. You may need to leave the coding to your developers, but it’s crucial for you to grasp the process behind everything.
Before starting, review the Google Tag developer documentation to understand the different commands you can use within the gtag() function. The event command is mainly focused on to send event data to GA4. There are three parts of the event command: 'event', '
There are also some event limits to be aware of. There is a maximum of 40 characters for event names and 25 parameters per event.
To test this, you must download and enable the Google Analytics Debugger Chrome extension to activate Debugview.
The article then provides a step-by-step guide on how to create an event with Google Tag, starting with exploring your site with the console, then enhancing the code by adding a condition, an event name, and parameters.
The final code for tracking link clicks is:
<script>
document.addEventListener("click", function(event) {
if (event.target.tagName === "A") {
gtag('event', 'link_click', {
'link_text': event.target.innerText,
'link_url': event.target.href,
'link_id': event.target.id,
'link_classes': event.target.className
});
}
});
</script>
Finally, the article concludes by emphasizing the importance of tailoring this process to your specific needs and working closely with your developer or someone who knows JavaScript.