Guides / Prompting and shipping
How to Set Up Google Analytics 4 on Your Website
Set up Google Analytics 4 step by step: account, property, web data stream, the Google tag, single-page app page views, key events and consent.
Mythex Team · · 6 min read
To set up Google Analytics 4, create an account and a property at analytics.google.com, add a web data stream for your site's URL, and copy its measurement ID (it starts with G-). Then add the Google tag with that ID to the <head> of every page, publish, and check the Realtime report. On a single-page app, make sure page views fire on route changes, and if you have visitors in the EU or UK, connect the tag to a consent banner.
What GA4 gives you
Google Analytics 4 (GA4) measures what visitors do once they reach your site: how many come, from where (search, social, email, ads), which pages they view, and which actions they complete — sign-ups, purchases, contact form submissions. It records everything as events; a page view is just an event called page_view.
It is free, widely used and connects to Google Ads and Search Console. The trade-offs are complexity, cookies (which bring consent requirements in some regions) and data going to Google.
Should you use GA4 at all?
| Option | Good for | Trade-off |
|---|---|---|
| Google Analytics 4 | Detailed marketing analytics, Google Ads, free at any scale | Complex interface; cookies and consent; data shared with Google |
| Privacy-focused analytics (e.g. Plausible, Fathom, Umami) | Simple traffic stats with a lighter script, often without cookies | Usually paid (or self-hosted); fewer features |
| Product analytics (e.g. PostHog, Mixpanel, Amplitude) | Tracking what logged-in users do inside an app, funnels, retention | More setup; overkill for a brochure site |
| Your own events table | A few key actions counted in your own database | You build the reports yourself |
For a marketing site that runs Google Ads, GA4 is the usual choice. For a simple site where you just want visitor counts, a privacy-focused tool is often less hassle. For in-app behaviour, product analytics fit better. How to add analytics to your app compares these in more depth.
Step by step
These steps follow Google's own setup guide as of September 2026. Menu names in GA4 change occasionally; if a label differs, search the Admin screen for it.
1. Create an account
Go to analytics.google.com and click Start measuring. Enter an account name (usually your company) and choose your data-sharing settings. Use a Google account your business will keep.
2. Create a property
A property is one website or app (or one group of them) whose data is reported together. Enter a property name, reporting time zone and currency, then your industry and business goals. The time zone decides where "today" starts in reports, so pick the one you work in.
3. Add a web data stream
Choose Web, enter your site's URL and a stream name. Leave enhanced measurement on: it automatically collects page views, scrolls, outbound clicks, site search, form interactions, video engagement and file downloads without extra code.
After you create the stream, its details page shows your measurement ID, which starts with G-.
4. Add the Google tag to your site
GA4 gives you a snippet beginning with a <!-- Google tag (gtag.js) --> comment. Google's instruction is to place it immediately after the opening <head> tag on every page. Most site builders and CMSs have a field for the measurement ID instead; use that if yours does. On an AI-built site, ask:
Add Google Analytics 4 with measurement ID G-XXXXXXXXXX. Load the gtag.js snippet in the head of every page, only in the production build — not in local development or preview. This is a single-page app, so make sure each route change is recorded as exactly one page view.
If you have or need a consent banner, include it in the same request (see step 7).
5. Check page views in a single-page app
Many AI-built sites are single-page apps (SPAs): moving between pages changes the URL without reloading the page. In the enhanced measurement settings, the page views option has an advanced setting for page changes based on browser history events — it listens for the history changes SPA routers make. With that on, GA4 records SPA navigation on its own.
The common bug is double counting: history-based page views and a manual page_view sent from the router. Pick one. Check by clicking between pages and watching the Realtime report or DebugView.
6. Confirm it works
Publish the site, open it in a normal browser window (ad blockers often block GA — try one without) and look at Reports → Realtime. Google says data collection may take up to 30 minutes to begin. Standard reports take longer to fill in than Realtime.
7. Handle consent
In the EU, UK and some other places, analytics cookies generally need the visitor's consent first. Google's consent mode lets the tag respect a visitor's choice: you set defaults (for example, analytics_storage denied) before the tag loads, then send an update when the visitor chooses. For EEA traffic, Google's consent mode also expects the ad_user_data and ad_personalization signals. How to add cookie consent covers banners and consent mode in more detail. This is general information, not legal advice.
8. Set up key events
"Key events" (called conversions before 2024) are the actions that matter to your business. Send an event when something important happens, then mark it: in Admin → Events, click the star next to the event. As of September 2026, standard properties can mark up to 30 key events.
Send a GA4 event named sign_up when a user finishes registration, generate_lead when the contact form is submitted successfully, and purchase with value and currency after a confirmed payment. Only send each once per action, and never include names, emails or other personal information in event parameters.
Using Google's recommended event names (like sign_up, generate_lead, purchase) makes some reports fill in automatically.
9. Tidy up the settings
- Data retention: GA4 keeps user- and event-level data for 2 or 14 months on standard properties. This affects explorations, not standard reports. Choose 14 months if you want year-on-year explorations.
- Internal traffic: define your office IP addresses as internal traffic and filter them out so your own visits don't skew the numbers.
- Link Search Console to see search queries alongside behaviour.
- Add users with their own Google accounts and the lowest role they need.
Common mistakes
- Tag missing on some pages because it was added to one template only.
- Double-counted page views in single-page apps.
- Tracking development and preview traffic. Load the tag only in production.
- Personal data in events or URLs. Google's policies prohibit sending personally identifiable information such as email addresses; watch for emails in URL query strings.
- Loading GA before consent where consent is required, or a banner with no way to accept, which leaves you with no data.
- Too many custom events nobody looks at. Track the five actions that tell you whether the site works.
- Using a personal Google account that leaves the company with the person who set it up.
Setup checklist
- Account and property created under a business Google account
- Web data stream with enhanced measurement on
- Google tag with the right G- ID on every page, production only
- One page view per route change in a single-page app
- Realtime report shows your visit
- Consent banner connected via consent mode where needed
- Key events defined and marked
- Data retention, internal traffic filter and Search Console link set
- No personal data in events or URLs
Google Analytics on Mythex
Mythex doesn't include its own visitor analytics for your app; analytics is something you add, like any other third-party script (build a landing page lists it as bring-your-own). Paste the prompt from step 4 with your measurement ID into chat. Because the ID is public, it can live in the front-end code; keep any real secrets in project settings (environment variables and secrets). New Mythex apps are single-page apps by default, so check step 5. Then publish again — the tag only collects data on the published site.
Questions
Is Google Analytics 4 free?
The standard version of Google Analytics 4 is free. Google also sells Analytics 360, a paid version with higher limits for large organisations.
Why isn't Google Analytics showing any data?
Google says data collection can take up to 30 minutes to begin. If the Realtime report is still empty after that, check that the tag is on the live site with the right G- measurement ID, that an ad blocker isn't blocking it in your browser, and that your consent banner isn't denying analytics by default without an accept option.
Is the GA4 measurement ID secret?
No. The G- measurement ID is visible in your site's code by design, so it is fine to put it in front-end code. It is not an API key and doesn't grant access to your reports.
Does Google Analytics work with single-page apps?
Yes. GA4's enhanced measurement can send a page view when the URL changes through browser history events, which is how most single-page app routers navigate. Make sure that option is on, or send page views from your router yourself — but not both.
Do I need a cookie banner for Google Analytics?
In the EU and similar jurisdictions, Google Analytics cookies generally need consent before they are set, and Google requires consent for EEA users when using its consent mode for ads data. Rules vary by country, so check what applies to your visitors. This is not legal advice.