gtag.js is inserted as a script tag within the head tag of HTML, initializes GA4 properties with gtag('config', 'G-XXXXXXXX'), and sends events with gtag('event', 'purchase', {...}). This is a method in which developers directly write code and collect data without GTM.
Difference from GTM: gtag.js requires developer deployment for each code change. GTM uses gtag.js internally, but manages tags by manipulating the interface, allowing marketers to change them directly without requiring a developer to deploy page-code changes. If the number of events is small and the site is static, gtag.js is sufficient, but if there are many dynamic events, GTM is advantageous.
GA4 screen path: GA4 Management → Properties → Data Stream → Web Stream Details → Check the gtag.js snippet code in ‘View Tag Guide’. Copy the measurement ID (G-XXXXXXXX) from this screen.
Key takeaways
- It is an official JavaScript library that transmits data to Google tools by inserting it directly into the HTML head.
- It consists of three main commands: gtag('config'), gtag('event'), and gtag('set').
- You can initialize GA4 and Google Ads conversions simultaneously with a single snippet.
- It can be used without GTM, but requires developer deployment for each event change.
- In SPA, when changing routes, you must manually call gtag('config', ..., {page_path: ...}) for page views to be recorded correctly.
