What you’ll need #
- GTM container correctly installed on your site (the
<head>+<body>snippets). - GA4 property with either a GA4 Configuration Tag in GTM or a Measurement ID ready.
- Access to Tag Assistant (Preview) and GA4 DebugView.
Tip: If you already send page_view via a GA4 Configuration tag in GTM, you’re set. If not, create that first and fire it on All Pages.
Step 1 — Identify the button selector #
Decide how you’ll target the button: by an id (preferred when unique) or by a class.
Example: By id #
<button id="signup-btn" class="btn btn-primary">Start Free Trial</button>
Selector to use: #signup-btn
Example: By class #
<button class="cta download-btn">Download Now</button>
Selector to use: .download-btn (or .cta.download-btn for extra specificity)
Heads‑up: If multiple buttons share the same class, you’ll track all of them. If you only want one, prefer a unique id or use a more specific CSS selector.
Step 2 — Enable GTM click variables #
- Open Google Tag Manager → Variables.
- Under Built‑In Variables, click Configure and enable:
- Click ID
- Click Classes
- Click Text
- Click URL (handy for links)
Step 3 — Create a Click trigger targeting your button #
- Go to Triggers → New.
- Choose Trigger Type: Click – All Elements (works for buttons, not just links).
- Select Some Clicks and add a condition using your selector:
When Operator Value Notes Click ID equals signup-btnBest when the button has a unique id.Click Classes contains download-btnGood when targeting by class. Click Element matches CSS selector #signup-btnor.cta.download-btnMost flexible (supports full CSS selectors). - Name it clearly, e.g., TRG — Click — #signup-btn.
Pro move: The matches CSS selector operator is incredibly reliable across varied markup. Prefer it when in doubt.
Step 4 — Create a GA4 Event tag #
- Go to Tags → New.
- Choose Tag Type: Google Analytics: GA4 Event.
- Set Configuration Tag to your existing GA4 Configuration tag (or add your GA4 Measurement ID if you use the newer GA4 Event + Measurement ID pattern).
- Set Event Name to something future‑proof, e.g.
button_clickorcta_click. - Add event parameters (recommended):
button_id: {{Click ID}} button_classes: {{Click Classes}} button_text: {{Click Text}} click_url: {{Click URL}} - Under Triggering, select the trigger you created in Step 3.
- Name it e.g., TAG — GA4 Event — button_click — #signup-btn and save.
Step 5 — Preview & debug in Tag Assistant #
- Click Preview in GTM and enter your site URL.
- In the connected browser window, click the target button.
- Back in Tag Assistant, find the related Click event in the left panel and confirm your tag fired.
- Verify variables in the Variables tab (e.g., Click ID, Click Classes, Click Text).
If it didn’t fire: Confirm you used the correct selector, the click actually occurs on the button element (not a child), and that no front‑end framework prevents default clicks. Try the matches CSS selector operator against Click Element.
Step 6 — Confirm receipt in GA4 DebugView #
- Open GA4 → Admin → DebugView (or use Reports → Realtime).
- Perform the click while Tag Assistant is connected.
- Look for your
button_clickevent and expand it to see parameters.
Step 7 — Publish #
- In GTM, click Submit.
- Add a descriptive Version Name and Notes (e.g., “Add button_click for #signup-btn”).
- Publish.
Optional — Make the data easy to report in GA4 #
If you want to use parameters (e.g., button_text or button_id) in standard reports, register them as custom dimensions:
- GA4 → Admin → Custom definitions → Create custom dimension.
- Scope: Event. Event parameter: choose
button_text(or the parameter you added). - Save. Data populates moving forward.
Explorations: For ad‑hoc analysis, use Explore → create a Free Form; add Event name as a filter = button_click, then use button_text or button_id as a dimension.
Step 8 — View & report your button clicks in GA4 #
Quick QA (now) #
- Realtime: GA4 → Reports → Realtime. Click your page in the map/list, then look under Event count by Event name for
button_click. - DebugView: GA4 → Admin → DebugView. Click your most recent event and expand to see
button_id,button_text, etc.
Standard reports (counts over time) #
- Go to Reports → Engagement → Events.
- Find button_click in the table and click it to open the event detail view.
- Event count shows total clicks.
- To break down by
button_idorbutton_text, first register them (see the “Optional — Make the data easy to report” section above), then use the report’s Customize (pencil icon) to Add dimension and include your parameter.
Mark important buttons as conversions (optional) #
- GA4 → Admin → Events.
- Find
button_clickand toggle Mark as conversion (or create a rule‑based custom event likesignup_cta_clickthat matchesevent_name=button_clickandbutton_id=signup-btn, then mark that as a conversion). - See results in Reports → Engagement → Conversions.
Explorations (flexible tables & trends) #
- Explore → Free form.
- Click + next to Dimensions and add Event name, button_id, button_text.
- Add metric Event count.
- Drag button_id (or button_text) to Rows, drag Event count to Values.
- Add a filter: Event name equals
button_click.
Dashboards (Looker Studio) #
- Create a Looker Studio report → Connect GA4 property.
- Build a table with button_id or button_text as a dimension and Event count as metric. Filter event name to
button_click.
Raw data (BigQuery export, optional) #
SELECT
PARSE_DATE('%Y%m%d', event_date) AS date,
ep.value.string_value AS button_id,
COUNT(*) AS clicks
FROM `YOUR_PROJECT.YOUR_DATASET.events_*`, UNNEST(event_params) ep
WHERE event_name = 'button_click'
AND ep.key = 'button_id'
GROUP BY 1, 2
ORDER BY 1, 2;
- Retention: GA4’s event data retention setting (Admin → Data settings) controls how far back you can analyze in Explore. Dashboards and standard reports aren’t affected the same way.
- Attribution: If you mark a button as a conversion, attribution and traffic source breakdowns appear in standard conversion reports.
Troubleshooting checklist #
- Nothing fires in Preview: Confirm GTM snippet is present on the page you’re testing (view source or use browser extensions).
- Wrong button tracked: Tighten your CSS selector; prefer
#unique-idor a more specific class chain. - Clicks inside nested elements: Try targeting the parent button via Click Element matches CSS selector (e.g.,
button#signup-btn). - SPA or framework issues: Some frameworks intercept clicks. Consider a Custom Event trigger if the app dispatches one, or listen for
history changein SPA setups. - Missing parameters in GA4 reports: Make sure you registered the parameter as a custom dimension (Admin → Custom definitions) and wait for new data.
1) Variables → Enable: Click ID, Click Classes, Click Text, Click URL
2) Trigger → Click – All Elements → Some Clicks
Condition: Click Element matches CSS selector #signup-btn
3) Tag → GA4 Event
Event name: button_click
Params: button_id {{Click ID}}, button_text {{Click Text}}, button_classes {{Click Classes}}
4) Attach trigger from (2) to tag from (3)
5) Preview (Tag Assistant) → click button → verify
6) DebugView (GA4) → confirm event + params
7) Submit & Publish
This button is for testing purposes #
test-click