Skip to content
weballin

weballin DIGIWIKI

gtag.js

gtag.js is an official JavaScript library that is inserted directly into HTML to transmit data to Google tools such as GA4 and Google Ads. This is a method where the developer manages tags directly with code without GTM, initializing them with gtag('config', 'G-XXXXXXXX') and sending events with gtag('event', ...).

weballin

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.

References

Frequently asked questions

Will there be a problem if gtag.js and GTM are installed on the site at the same time?

Double installation problems may occur. If the GA4 measurement ID is in both the gtag.js and GTM GA4 configuration tags, the pageview is counted twice. When diagnosed with Tag Assistant, a duplicate installation warning is displayed. To centralize tag management in GTM, you must remove the direct gtag.js snippet from HTML and operate GTM alone.

Can I implement GA4 e-commerce events directly with gtag.js?

It is possible. Implemented in gtag('event', 'purchase', {transaction_id:'T001', value:50000, currency:'KRW', items:[...]}) format. However, if the event structure is complex and code modifications are frequent, the data layer + GTM combination is advantageous for maintenance.