2026-05-23
How to set up analytics with Google Analytics and Google Tag Manager using an AI agent
A practical guide to connecting GA4 and Google Tag Manager, setting up events, key events, dataLayer, reports, and preparing an AI agent for analytics automation.
Correct analytics is the foundation of performance marketing. You can run ads, test creatives, change landing pages, and write hypotheses, but if events, conversions, and funnels are configured incorrectly, the team will make decisions blindly.
Google Analytics 4 helps collect data about user behavior on a website or app, while Google Tag Manager lets you manage tags, events, and tracking logic without asking a developer to change the code every time. GA4 uses an event-based data model, and GTM can be used to configure the Google tag and GA4 events that send data to Analytics. (Google Support)
An AI agent in OpenClaw or GolemWorkers can help with this work: prepare a tracking plan, check events, assemble reports, find missing conversions, and suggest which metrics should be tracked for paid acquisition.
What exactly can be automated
After Google Analytics and Google Tag Manager are connected, an agent can help with tasks like:
- prepare a tracking plan;
- define the key funnel events;
- prepare a naming convention for GA4 events;
- write dataLayer events for developers;
- check which events are already arriving in GA4;
- prepare reports by source, campaign, and conversion;
- find funnel problems;
- prepare a list of key events;
- suggest events for Google Ads / Meta Ads optimization;
- create weekly analytics summaries.
The main value is that the agent turns analytics from chaotic “set it up as we go” tracking into a controlled process: event → parameter → tag → validation → report → decision.
GA4, GTM, and dataLayer: who does what
Google Analytics 4 is where data is stored and analyzed: users, events, traffic sources, pages, conversions, retention, and other reports.
Google Tag Manager is a tag management system. Through GTM, you can add the Google tag, GA4 Event tags, advertising pixels, and other tracking scripts. For GA4 in GTM, you need to configure the Google tag that sends website data to Google Analytics. (Google Support)
dataLayer is the technical data layer on the website. Through it, the site tells GTM that an important action happened: registration, form submission, purchase, button click, or pricing page view.
Example dataLayer event:
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "lead_submit",
form_name: "demo_request",
page_type: "landing",
user_type: "new"
});
</script>
Why this needs more than just an API key
For serious Google Analytics and Google Tag Manager automation, you usually need access through Google Cloud and OAuth/service account credentials, not a simple API key.
There are two different scenarios:
1. Reading GA4 reports
For this, you use the Google Analytics Data API. It gives access to GA4 reporting data similar to what you see inside the Analytics interface. (Google for Developers)
2. Managing GA4 or GTM settings
For this, you need the Admin API / Tag Manager API and much more careful permission handling. The Google Analytics Admin API quickstart describes authorization scenarios through a user account or a service account. (Google for Developers)
For the first stage, it is better to give the agent read-only access to reports rather than permission to change the GTM container.
General analytics setup flow
The process looks like this:
- Create a GA4 property.
- Create a Web Data Stream.
- Get the Measurement ID.
- Create a GTM container.
- Install the GTM code on the website.
- Add the Google tag in GTM.
- Set up basic events.
- Set up custom events through dataLayer.
- Mark important events as key events.
- Validate events in DebugView / Realtime.
- Connect the GA4 Data API for the agent.
- Configure a safe reporting workflow.
Step 1. Create a GA4 property
First, create a property in Google Analytics.
Example structure:
Account: GolemWorkers
Property: GolemWorkers Website
Data Stream: Web
Website URL: https://example.com
After creating the Web Data Stream, Google Analytics will give you a Measurement ID:
G-XXXXXXXXXX
This ID will be used in Google Tag Manager to configure the Google tag.
Step 2. Create a GTM container
Next, create a container in Google Tag Manager.
Structure:
Account: GolemWorkers
Container: golemworkers.ai
Target platform: Web
After creating the container, GTM will provide two code snippets:
- code for
<head>; - code for
<body>.
These snippets need to be installed on every page of the website. If the site is built with Webflow, Framer, WordPress, Next.js, or another framework, the code should be placed globally in the layout or site settings.
Step 3. Add the Google tag in GTM
In GTM, create a new tag:
Tags
→ New
→ Tag Configuration
→ Google tag
In the Tag ID field, paste the GA4 Measurement ID:
G-XXXXXXXXXX
Trigger:
All Pages
Google states that to set up GA4 through GTM, you need to use the Google tag that sends data from your website to Google Analytics. (Google Support)
After that:
Save
→ Preview
→ Submit
→ Publish
Step 4. Set up basic events
GA4 can automatically collect some events, but marketing usually needs custom events.
Minimum set for a SaaS product / landing page:
page_view
view_pricing
click_cta
lead_submit
signup_start
signup_complete
demo_request
contact_submit
checkout_start
purchase
For GolemWorkers or an AI-agent product, you can use:
agent_create_click
enter_app_click
pricing_view
waitlist_submit
article_read
video_watch_50
demo_request
Do not create chaotic event names. Choose one style:
snake_case
Example:
lead_submit
demo_request
agent_create_click
Step 5. Configure dataLayer
If an event cannot be reliably captured through a regular click trigger, it is better to ask a developer to add dataLayer events.
Example for a CTA:
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "agent_create_click",
cta_text: "Create AI Agent",
page_location: window.location.href,
page_type: "homepage"
});
</script>
Example for a form:
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "lead_submit",
form_name: "early_access",
lead_type: "founder",
page_type: "landing"
});
</script>
Example for an article:
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "article_read",
article_slug: "setup-analytics-with-ga4-gtm-ai-agent",
article_category: "analytics",
read_depth: "75"
});
</script>
Step 6. Create a GA4 Event tag in GTM
For every dataLayer event, create a GA4 Event tag.
Example for lead_submit:
Tags
→ New
→ Google Analytics: GA4 Event
Event name:
lead_submit
Event parameters:
form_name = {{DLV - form_name}}
lead_type = {{DLV - lead_type}}
page_type = {{DLV - page_type}}
Trigger:
Custom Event
Event name: lead_submit
Google Tag Manager supports creating GA4 Event tags and sending event parameters to Google Analytics. (Google Support)
Step 7. Create Variables for dataLayer
For GTM to read parameters from dataLayer, you need to create Data Layer Variables.
Example:
Variables
→ New
→ Data Layer Variable
Name:
DLV - form_name
Data Layer Variable Name:
form_name
Repeat for:
lead_type
page_type
cta_text
article_slug
article_category
read_depth
Step 8. Mark important events as key events
Not every event should be a key event. A key event is an important user action that the business wants to measure as a valuable result. Google explains that any collected event can be marked as a key event to measure important user actions and marketing effectiveness. (Google Support)
For a landing page, you can mark:
lead_submit
demo_request
signup_complete
purchase
For a content funnel:
article_read
waitlist_submit
enter_app_click
But it is better not to turn everything into a key event. If you mark too many events, analytics loses focus.
Step 9. Validate events
Validation is mandatory. You should never set up tags and simply assume that analytics works.
Minimum validation flow:
GTM Preview Mode
→ open the website
→ perform the action
→ check that the trigger fired
→ check that the GA4 Event tag fired
→ open GA4 Realtime / DebugView
→ check that the event arrived
If the event does not arrive, common reasons include:
- the GTM code is not installed on the page;
- the Google tag has not been published;
- the event name in dataLayer and the trigger are different;
- the dataLayer variable is named incorrectly;
- the event fires before GTM loads;
- an ad blocker interferes with the test;
- changes have not been published.
Step 10. Connect the GA4 Data API for the AI agent
Once events are being collected, you can give the agent access to reports through the GA4 Data API. First, enable the Google Analytics Data API in a Google Cloud project. Google’s quickstart starts directly with the step “Enable the Data API.” (Google for Developers)
Safe option:
Google Cloud
→ Create Project
→ Enable Google Analytics Data API
→ Create Service Account
→ Download Service Account JSON
→ Add service account email to GA4 property with Viewer access
Example file:
{
"type": "service_account",
"project_id": "analytics-agent-project",
"private_key_id": "xxxx",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "analytics-agent@analytics-agent-project.iam.gserviceaccount.com",
"client_id": "1234567890",
"token_uri": "https://oauth2.googleapis.com/token"
}
The file can be saved in the OpenClaw environment:
~/.config/openclaw/secrets/ga4-service-account.json
Permissions:
mkdir -p ~/.config/openclaw/secrets
chmod 700 ~/.config/openclaw/secrets
chmod 600 ~/.config/openclaw/secrets/ga4-service-account.json
Step 11. JSON config for the agent
You can also create a simple separate config file:
{
"provider": "google_analytics_4",
"property_id": "123456789",
"service_account_json": "~/.config/openclaw/secrets/ga4-service-account.json",
"default_date_range": "last_7_days",
"safe_mode": true,
"allowed_actions": [
"read_reports",
"analyze_events",
"suggest_tracking_plan",
"generate_summary"
],
"blocked_actions": [
"publish_gtm_container",
"delete_tags",
"change_key_events",
"modify_property_settings"
]
}
At the first stage, the agent should only be allowed to read and analyze.
What you can ask the agent to do
1. Tracking plan
Prepare a tracking plan for the landing page.
Goal: leads and signups.
Needed: event name, trigger, parameters, where to place dataLayer, and which events should become key events.
2. Funnel check
Check GA4 for the last 7 days.
Show the funnel:
page_view → pricing_view → click_cta → lead_submit.
Where is the biggest drop-off?
3. Weekly analytics report
Prepare a weekly report:
- users;
- sessions;
- traffic sources;
- top pages;
- key events;
- conversion rate;
- problems;
- recommendations for next week.
4. Advertising traffic check
Check traffic from Google Ads and Meta Ads.
Compare on-site CTR, engagement, and lead_submit.
Which campaigns bring the highest-quality traffic?
5. Event audit
Check the list of events in GA4.
Find duplicates, poor naming, events without parameters, and events that should be marked as key events.
Safe policy for the agent
Analytics affects advertising decisions, so the agent must not be allowed to break tracking by itself.
Safe rules:
- The agent can read reports.
- The agent can suggest a tracking plan.
- The agent can write instructions for GTM.
- The agent can prepare dataLayer code.
- The agent cannot publish the GTM container without confirmation.
- The agent cannot delete tags.
- The agent cannot change key events without confirmation.
- The agent cannot change access or property settings.
- All changes must go through Preview Mode.
- Any GTM publication must happen only after human review.
Common mistakes
Mistake 1. No single tracking plan
The team adds events chaotically: submit, form_submit, lead, lead_form, contact. Later, the reports become impossible to read properly.
Mistake 2. Everything is marked as a key event
Key events should be tied to a business outcome. If you mark everything, reports lose meaning.
Mistake 3. Events have no parameters
A click_cta event without parameters is almost useless. It is better to pass:
cta_text
page_type
page_location
section_name
Mistake 4. No Preview Mode validation
Every tag should be tested before publication.
Mistake 5. The agent immediately gets write access
For the first version, read-only GA4 access is enough. Publishing the GTM container should remain a human-controlled action.
How GolemWorkers simplifies this
GolemWorkers can serve as a ready environment for an AI agent that helps marketers not only view reports, but also manage the analytics workflow.
The agent can:
- prepare a tracking plan;
- prepare dataLayer instructions;
- check GA4 events;
- create a weekly report;
- find funnel problems;
- suggest growth hypotheses;
- prepare tasks for developers;
- connect analytics with Google Ads / Meta Ads reports.
The main value is not that the agent “connects GA4.” The value is that it helps maintain analytics as a system: events, parameters, reports, and decisions are connected to each other.
Conclusion
Setting up analytics through Google Analytics and Google Tag Manager is not just about installing one script on the website.
You need to build the chain correctly:
business goal
→ tracking plan
→ dataLayer
→ GTM tags
→ GA4 events
→ key events
→ reports
→ growth decisions
For the first version, it is better to start with a read-only agent that analyzes data, prepares reports, and suggests a tracking plan. After validation, you can gradually add more advanced scenarios: GTM audit, tag preparation, server-side events, and automated weekly summaries.
This turns analytics from a one-time setup into a continuous process that helps the team understand faster what works in marketing, where the funnel breaks, and which hypotheses are worth testing next.