FreeToolsHub

GA4 Gtag Code Generator & Event Builder

Paste your GA4 Measurement ID and instantly generate the gtag.js install snippet plus custom event, pageview, and conversion tracking code — ready to paste into your site's head.

Get Your GA4 Tracking Code Right the First Time

Google Analytics 4 tracks nothing until the gtag.js snippet is installed correctly in your site's <head> with the right Measurement ID. Copy it wrong — miss the config line, put in a UA-style ID, or forget the async attribute — and you'll stare at an empty Realtime report wondering why. This GA4 gtag code generator takes your G-XXXXXXX Measurement ID and outputs the exact, current gtag.js install block, correctly ordered and ready to paste.

Beyond the base tag, it builds the custom event code most site owners actually need: button-click and form-submit events, outbound-link and file-download tracking, and conversion events you can mark as key events in GA4. Each snippet uses the modern gtag('event', ...) syntax with proper parameters, so you're not hand-editing examples from three-year-old blog posts that still reference Universal Analytics.

Base Tag, Events, and Conversions in One Place

Whether you're wiring GA4 into a static HTML site, a WordPress theme header, or a React app, the generated code drops in without a dependency on Google Tag Manager. Generate the base install tag once, then produce individual event snippets for each interaction you want to measure, copy them into your click handlers, and verify them in GA4's Realtime and DebugView reports. It removes the guesswork from a setup step that quietly breaks a huge share of analytics implementations.

Key Benefits & Features

Correct gtag.js Install Snippet

Generates the current GA4 base tag with your Measurement ID, async loading, and config line in the right order.

Custom Event Builder

Produce gtag('event', ...) code for clicks, form submits, downloads, and conversions with proper parameters.

No GTM Required

Get clean code you can paste straight into any HTML head, theme, or app — no Google Tag Manager container needed.

Copy-Ready & Private

Snippets are built in your browser. Your Measurement ID is never sent to or stored on our servers.

How to Use the Google Analytics Gtag Helper Step-by-Step

This utility runs entirely inside your browser using client-side JavaScript. We prioritize your security: none of your inputted text is logged or stored.

  1. 1

    Enter your GA4 Measurement ID (it starts with G-, e.g. G-ABC123XYZ).

  2. 2

    Copy the generated base gtag.js snippet and paste it immediately after the opening <head> tag on every page.

  3. 3

    Optionally build a custom event — pick click, form submit, download, or conversion and fill in the event name.

  4. 4

    Paste the event snippet into your button or form handler, then confirm it fires in GA4 Realtime or DebugView.

Practical Examples

Input Example

Measurement ID: G-ABC123XYZ

Expected Output
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123XYZ"></script> <script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config','G-ABC123XYZ');</script>
Input Example

Event: button click on 'Sign Up'

Expected Output
gtag('event','sign_up_click',{ 'button_text':'Sign Up','page_location':location.href });
Input Example

Conversion: purchase, value 49.99 USD

Expected Output
gtag('event','purchase',{ 'value':49.99,'currency':'USD','transaction_id':'T12345' }); // mark as a key event in GA4

Frequently Asked Questions (FAQ)

Where exactly do I paste the GA4 gtag code?

Place the base gtag.js snippet as high as possible inside the <head> section — ideally immediately after the opening <head> tag — on every page you want to track. In WordPress you'd add it to the theme header (or via a header-scripts plugin); in a React or Next.js app you'd inject it into the document head. Putting it in the head ensures the tag loads before the user navigates away, so no pageviews are missed.

What's the difference between a GA4 Measurement ID and a Universal Analytics tracking ID?

GA4 Measurement IDs start with G- (for example G-ABC123XYZ), while the old Universal Analytics IDs started with UA-. Universal Analytics stopped processing data in 2023, so any UA- snippet you copy from an old tutorial will not work. This generator only produces GA4-compatible gtag.js code using your G- ID.

Do I need Google Tag Manager to use this code?

No. The gtag.js snippet this tool generates is the direct install method — it works entirely on its own. Google Tag Manager is an optional container system that's useful when non-developers need to manage many tags, but for a straightforward GA4 setup the gtag snippet is simpler and has one fewer dependency.

How do I track a button click or form submission as an event?

Use the custom event builder to generate a gtag('event', 'your_event_name', { ...parameters }) call, then place it inside the button's onclick handler or the form's submit handler. After deploying, open GA4's DebugView or Realtime report and trigger the action — you should see the event appear within seconds, which confirms it's wired correctly.

Why isn't my GA4 data showing up after adding the tag?

The usual causes are: the snippet is in the body instead of the head, the Measurement ID has a typo, an ad or privacy blocker is stopping googletagmanager.com from loading, or you're looking at standard reports (which can lag 24-48 hours) instead of Realtime. Check the Realtime report first — if your own visit shows there, the tag is working and the standard reports will populate over the next day or two.

Explore category: Traffic & SEO
Ready to boost your productivity?

Browse our full list of free traffic & seo and make your daily content, coding, or math tasks easier.

Related Traffic & SEO

View all