Entity Tracking
- gtag.js: A JavaScript tagging framework and API used to send event data to Google products like Google Analytics and Google Ads.
- Google Tag ID: A unique identifier, such as G-XXXXXX or AW-XXXXXX, used to connect a website to a specific Google property.
- Destination: The target platform, such as a GA4 web data stream or a Google Ads account, where tag data is delivered.
- Tag Assistant: A web-based debugging tool used to verify that Google tags are installed and firing correctly.
- Config Command: A specific instruction in the gtag.js code required to send data to advertising products and enable automated event tracking.
- Google Tag Manager (GTM): A container-based system that allows users to manage and deploy tags without directly editing website code.
gtag.js (the Google tag) is a unified JavaScript tagging framework used to send data to various Google products, including Google Analytics 4 (GA4), Google Ads, and the Google Marketing Platform. It replaces the older method of installing separate code snippets for every product with a single, streamlined library.
By using this focused API, marketers and SEO practitioners can manage multiple measurement destinations from one central script, reducing site weight and implementation errors.
What is gtag.js?
Formerly known as the global site tag, gtag.js is the current standard for manual Google tag implementation. It acts as a wrapper that calls other libraries internally, such as conversions.js or analytics.js, to maintain backward compatibility while remaining lightweight.
The primary purpose of gtag.js is to behave as "one tag to rule them all." It allows a single installation on a website to pipe data to multiple connected destinations. [The analytics.js JavaScript library is no longer supported by Google Analytics] (Google Analytics Help).
Why gtag.js matters
Implementing gtag.js provides several technical and performance advantages for site measurement:
- Future-proof measurement. Access the latest Google features and integrations automatically as they are released without changing site code.
- Improved data quality. Use advanced techniques to recover conversion data lost to ad blockers or cookie restrictions.
- Unified management. Manage multiple Google product IDs in one place instead of scattered code snippets.
- Accuracy. Achieve more precise tracking when third-party cookies are unavailable due to industry or regulation changes.
- Access to features. Enable "Enhanced measurement" in GA4 to automatically track actions like scrolls, clicks, and file downloads.
How gtag.js works
The framework uses a dataLayer to store and send information. When a user interacts with a page, the script executes commands to push that data to Google.
- Library Loading: The script loads the gtag.js library using your primary Tag ID.
- Initialisation: The code establishes a
gtag()function to communicate with the data layer. - Configuration: The
configcommand identifies where the data should go and sets up default tracking parameters. - Destination Routing: The library sends the data to the target destinations, such as a GA4 property or a Google Ads account.
Set up and Installation
To collect data, you must place the Google tag snippet immediately after the opening <head> HTML tag on every page you wish to measure.
Implementation Steps
- Obtain your Tag ID from your Google product (e.g., GA4 or Google Ads).
- Copy the provided gtag.js snippet.
- Replace the
TAG_IDplaceholder in the snippet with your specific ID. - Paste the code into your website's header.
ID Formats
Different Google products use specific ID prefixes: * [G-XXXXXXXXXX is used for GA4 properties] (Optimize Smart). * [AW-XXXXXXXXX is typically used for Google Ads conversion tracking] (Optimize Smart). * [GT-XXXXXXXXX is often used for tags managed through Google Tag Manager] (Optimize Smart).
Best practices
Place on every page. Ensure the tag is in the header of every page to maintain session continuity and conversion accuracy.
Use the config command. [Many features like conversion linking and automatically collected events require the config command to be set on your website] (Google Developers).
Verify with Tag Assistant. [Always test your installation at tagassistant.google.com] (Google Tag Assistant) to confirm the tag is sending data correctly.
Manage consent. Modify the tag to respect user privacy and legal requirements by deactivating remarketing data collection for users who do not provide consent.
Common mistakes
Mistake: Using multiple Tag IDs from different platforms in the same "tag ID" field. Fix: Create separate Google tag configurations for GA4 and Google Ads to ensure accurate data routing.
Mistake: Placing the tag snippet at the bottom of the page or inside the <body> tag.
Fix: Move the snippet immediately after the opening <head> tag to capture data before the user exits.
Mistake: Managing the same tag in both gtag.js (manual code) and Google Tag Manager. Fix: Choose one implementation method to avoid conflicting configurations and duplicate data.
gtag.js vs Google Tag Manager
| Feature | gtag.js | Google Tag Manager |
|---|---|---|
| Primary User | Technical users/Developers | Marketers/Non-technical users |
| Implementation | Manual code on every page | One-time container installation |
| Flexibility | Direct API control | UI-based tag management |
| Updates | Requires code changes | Updates managed in a web UI |
| Complexity | Simple for single products | Better for high-volume, complex tags |
Rule of Thumb: Use gtag.js if you lack a web development team or only need default metrics. Use Google Tag Manager for complex configurations or if you need to manage third-party (non-Google) tags.
FAQ
What is the difference between gtag.js and the Global Site Tag?
They are the same thing. Google rebranded the "Global Site Tag" to "the Google tag" to simplify measurement across its advertising and analytics products. If you already have the global site tag on your site, you do not need to update your code.
Can I send event data manually?
Yes. You can use the event command to track specific actions. For example, to track a purchase, you would call: gtag('event', 'purchase', { value: 1.23, transaction_id: 'test' });.
How do I check if my tag is working?
Use the Tag Assistant tool. Enter your URL and click "Connect." The tool will show you which Tag IDs were found and if they are firing successfully. You can also check the "Tag coverage summary" in your Google product for a site-wide overview.
Can one tag send data to both Google Ads and GA4?
Yes. You can connect a single Google tag to multiple destinations. This allows you to track one event on your site and send it to both platforms without adding extra code.