GA4 from an empty account to the first pageview
Set up Google Analytics 4 on a new site: account, property, web stream, the measurement ID in Next.js, and how to check that data arrives.
What a finished setup consists of
A working Google Analytics 4 setup is three objects and one string. You need an account, a property inside it, and a web data stream inside the property. The stream gives you a measurement ID that starts with G-, and your site loads the Google tag with that ID on every page. When your own visit shows up in the Realtime report, you are done.
By hand this is a few minutes of clicking. The parts that go wrong are rarely in Google Analytics itself. They are a variable that was missing when the site was built, a time zone nobody chose, or an API that was never switched on in the Cloud project behind an automation.
Account, property, stream
The three levels exist for different reasons, and each has its own rule about who may create it.
| Object | What it represents | How it gets created | Worth knowing |
|---|---|---|---|
| Account | You or your company | At analytics.google.com, by a person who accepts the terms of service | One account can hold up to 2,000 properties, so one is enough for every product you launch |
| Property | One product | In Admin, or with the Admin API by anyone holding the Editor role on the account | Reporting time zone and currency are set here |
| Web data stream | One website inside the property | In Admin under Data streams, or with the Admin API | Carries the measurement ID, for example G-PSW1MY7HB4 |
Creating the property and stream by hand
- Open analytics.google.com. If you have never used Analytics, press Start measuring, name the account and choose the data sharing settings. Otherwise open Admin, press Create and choose Property.
- Name the property after the product and pick the reporting time zone and currency.
- Answer the industry, business size and intended-use questions. The last one only decides which default reports you see.
- Press Create. On a new account you accept the Analytics terms of service and the data processing amendment here.
- Add a data stream of type Web. Enter the site address and a stream name, and leave enhanced measurement switched on.
- Press Create stream. To find the ID again later: Admin, then Data streams under Data collection and modification, then the stream. The measurement ID is in the first row of the stream details.
Choose the time zone before data arrives
The reporting time zone decides where one day ends and the next begins in every report. A visit at 01:00 in Amsterdam lands on the previous day if the property is set to Los Angeles. You can change the zone later, but the change only applies to new data. Google warns that the day of the switch shows a flat spot or a spike, and that you should not change it more than once a day.
This matters most when a tool creates the property for you, because the API insists on a time zone and whoever wrote the tool had to pick one. Check what you got on the first day, not in month three when you compare weeks.
Loading the tag in a Next.js app
Next.js has an official package for this, @next/third-parties. Its documentation still labels it experimental, and it has been the standard way to add the Google tag to an App Router project for a while. Install it with npm install @next/third-parties@latest and add one component to the root layout.
Two details are easy to miss. First, a variable whose name begins with NEXT_PUBLIC_ is copied into the JavaScript bundle when next build runs. If you add the variable to your host after the build, the deployed site still has no ID until you deploy again. Second, a Next.js app changes pages without a full reload. Google Analytics counts those as page views only because enhanced measurement listens for browser history changes. If you ever switch off the option called Page changes based on browser history events, you will count one page view per visit.
// app/layout.tsx
import { GoogleAnalytics } from '@next/third-parties/google'
const gaId = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
{gaId ? <GoogleAnalytics gaId={gaId} /> : null}
</html>
)
}Confirming that data arrives
Open the live site in one tab and the Realtime report in another. Realtime shows active users over the last 30 minutes, so your own visit should appear almost at once. The standard reports are a different matter: Google says processing can take 24 to 48 hours, so an empty Acquisition report on the first day tells you nothing.
For a closer look, Admin has DebugView, which lists each event from one device as it arrives. It only shows devices in debug mode. With the component shown above you switch that on by passing its debugMode option. With the plain Google tag you add 'debug_mode': true to the config command.
If Realtime stays empty, check these in order.
- View the page source of the deployed site and search for
G-. If the ID is not there, the variable was missing at build time. Set it and redeploy. - Try a private window with extensions off. Ad blockers stop the tag in your own browser, not in your visitors' browsers.
- Confirm the component sits in the root layout and not in one page.
- Compare the ID in the source with the stream. A site reporting to an older property looks exactly like a site reporting nothing.
The same setup through the Admin API
Everything except the account can be scripted with the Google Analytics Admin API. The usual identity for a script is a service account: a robot user that belongs to a Google Cloud project and signs its own requests with a key file, so no browser sign-in is involved.
Three things must be true before the first call works. The Google Analytics Admin API is enabled in the Cloud project that owns the service account. The service account's email address has been added in Analytics under Admin, Account access management, with the Editor role. And the access token was requested with the scope https://www.googleapis.com/auth/analytics.edit. Creating a property is then one POST, and adding the stream is a second one that returns the measurement ID.
Two error shapes account for almost every failure. A response saying the API has not been used in the project before, or that it is disabled, means the first condition is not met. A plain 403 with the API enabled means the service account is not an Editor on that Analytics account. Deleting a property through the API moves it to the trash. It can be restored from the Trash Can in the Analytics interface until it is purged, but there is no restore call in the API.
# 1. Create the property (timeZone is required)
curl -X POST https://analyticsadmin.googleapis.com/v1beta/properties \
-H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json" \
-d '{"parent":"accounts/123456789","displayName":"myapp","timeZone":"Europe/Amsterdam","currencyCode":"EUR"}'
# 2. Add the web stream to the property returned above
curl -X POST https://analyticsadmin.googleapis.com/v1beta/properties/987654321/dataStreams \
-H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json" \
-d '{"type":"WEB_DATA_STREAM","displayName":"myapp","webStreamData":{"defaultUri":"https://myapp.com"}}'
# The response contains webStreamData.measurementIdWhich steps Shipyard runs and which it cannot
Shipyard runs the API route above with a service account key that stays in the keychain on your computer. It looks for a property whose display name equals the project name, creates one if there is none, adds a web stream for https://yourdomain, reads the measurement ID and writes it to your host as NEXT_PUBLIC_GA_MEASUREMENT_ID. The starter app loads the tag from its root layout whenever that variable is set.
Two choices are made for you and are worth knowing about: the property is created with the time zone Etc/UTC and the currency USD. Change the time zone in Admin on day one if you want days to break at your own midnight.
What it does not do: create the Analytics account, accept Google's terms, create the service account or its key, or add that service account as an Editor. Those are one-time steps for a person. It also does not add custom events or a consent banner.
After the first pageview
Page views, scrolls to the bottom of a page and outbound clicks arrive without code, because enhanced measurement collects them. Two additions make the data useful for a product. Send an event at the moments that matter to the business, such as a completed sign-up, using sendGAEvent from the same package. Then mark that event as a key event in Admin so that reports can show which sources produce sign-ups and not only visits.
If you show a cookie consent banner, Google's consent mode is how the tag learns the visitor's answer. The pattern is a gtag('consent', 'default', ...) call that sets analytics_storage to denied before any other tag command runs, followed by an update call when the visitor chooses. Whether you need a banner at all is a legal question that depends on where your visitors are. Decide it before launch, because retrofitting consent means past data was collected under different rules from future data.
Sources
- Set up Analytics for a website or app (Analytics Help)support.google.com
- Admin API reference: propertiesdevelopers.google.com
- Admin API reference: properties.dataStreamsdevelopers.google.com
- Next.js guide: third-party librariesnextjs.org
- Monitor events in DebugView (Analytics Help)support.google.com
- Data freshness (Analytics Help)support.google.com
Launch in an hour, not a weekend
Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.
See how it works