Sub topic 2
Recording actions as events
This chapter assumes you know basic HTML, how a page loads scripts, what a URL query string is, and basic JavaScript. You have already installed GA4 with the standard gtag.js snippet.
Events in GA4
An event is a named record of something a visitor did, such as viewing a page or submitting a form. You send one with gtag('event', name, params):
document.querySelector('#signup-form').addEventListener('submit', function () {
gtag('event', 'sign_up', { method: 'email' });
});The method value is an event parameter. Parameters add context so you can later split the event by value.
Marking key events and reading conversions by source
A key event is an event you have marked as a conversion in the GA4 admin under Events. Marking an event does not change how it is recorded. It only tells reports to count it as a goal.
Once an event is a key event, open the Traffic acquisition report and group by Session source / medium. Social visits tagged with utm_medium=paid_social or organic_social then show their key event counts side by side.
Respecting consent and privacy
Set consent defaults before any other gtag call. Denied defaults stop the storage of identifying data until the visitor accepts:
gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied'
});
function onConsentAccepted() {
gtag('consent', 'update', { analytics_storage: 'granted' });
}Managing tags with a tag manager and data layer
A tag manager is a service that loads tracking tags from one container, so you change tracking without editing page code. The data layer is a JavaScript array, window.dataLayer, where your page pushes structured values for the tag manager to read:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ event: 'generate_lead', form_id: 'newsletter_footer' });A trigger in the tag manager fires the GA4 tag when it sees generate_lead.
Exercise
Exercise
A GA4 event named purchase fires for every visitor, but the conversion report shows zero. What is the most likely cause, and how do you fix it?
Show answer
The event is recorded but not marked as a key event. Open Admin, then Events, and toggle purchase as a key event.