Skip to content

Conversation

@lmammino
Copy link
Member

@lmammino lmammino commented Jan 30, 2026

Summary

This PR implements comprehensive GA4 custom event tracking across the site to measure user engagement and conversions. The implementation enables calculating click-through rates (CTR) and conversion rates by tracking both impressions (view events) and actions (click/submit events).

Key Features

  • Buy Button Tracking - View and click events with format (print/ebook) and location tracking
  • Free Chapter Form - View and submission tracking for conversion rate calculation
  • Blog CTA Variant Testing - Tracks 10 different promo card variants (promo01-promo10) to identify best performers
  • Scroll Depth - Tracks 25%, 50%, 75%, 100% scroll milestones on blog posts
  • Blog Read Completion - Fires when users reach the end of articles
  • Outbound Link Tracking - Captures clicks to external sites (Amazon, Packt, etc.)
  • Chapter Expansion - Tracks which chapters users explore
  • Internal Navigation - Monitors navigation patterns across the site

Files Changed

File Change
src/lib/analytics.ts New analytics utility module with type-safe event tracking
src/components/pages/Home/components/BuyButtons.astro Buy button view/click tracking
src/components/pages/Home/FreeChapter.astro Form view/submit tracking
src/components/blog/BookPromo.astro CTA view/click with variant tracking
src/components/blog/BlogLayout.astro Scroll depth and read completion
src/components/pages/Home/Chapters.astro Chapter expansion tracking
src/Layout.astro Outbound links and internal navigation

Test Plan

  • Verified all events fire correctly in browser console (debug mode)
  • Tested on localhost using Playwright
  • Validate events in GA4 DebugView after deployment
  • Create custom dimensions & metrics in GA4 admin (see below)

GA4 Custom Dimensions & Metrics Setup (manual step)

Based on the analytics implementation, here's the detailed table of GA4 custom dimensions and metrics you need to create:

Custom Dimensions (Event-scoped)

Dimension Name Scope Description Event Parameter
Book Format Event Format of book (print/ebook) book_format
Button Location Event Location of buy buttons on page button_location
CTA Variant Event Blog promo card variant (promo01-promo10) cta_variant
CTA Position Event Position of CTA on page (sidebar/inline/footer) cta_position
CTA Type Event Type of call-to-action cta_type
Content Type Event Type of content being viewed content_type
Form Location Event Location of the free chapter form form_location
Navigation Type Event Type of internal navigation navigation_type
Link Domain Event Domain of outbound link clicked link_domain
Link Text Event Anchor text of outbound link link_text
Link URL Event Full URL of outbound link link_url
Source Page Event Page where action originated source_page
From Page Event Origin page for navigation from_page
To Page Event Destination page for navigation to_page
Chapter Title Event Title of chapter viewed chapter_title

Custom Metrics (Event-scoped)

Metric Name Scope Description Event Parameter Unit
Percent Scrolled Event Scroll depth percentage reached percent_scrolled Standard
Estimated Read Time Event Estimated reading time in minutes estimated_read_time Standard
Time on Page Event Actual time spent on page (seconds) time_on_page Seconds
Chapter Number Event Chapter number viewed chapter_number Standard

How to Create in GA4

  1. Go to AdminData displayCustom definitions
  2. Click Create custom dimension (or Create custom metric)
  3. Enter the values from the table above
  4. For Scope, select "Event" for all of these
  5. The Event parameter field should match exactly what's in the table

Note: page_path is already a built-in GA4 dimension, so you don't need to create it as a custom dimension.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements comprehensive GA4 custom event tracking across the site to measure user engagement and conversions. The implementation follows a well-structured approach with type-safe analytics utilities and consistent tracking patterns across components.

Changes:

  • Adds a new analytics utility module (src/lib/analytics.ts) with type-safe event tracking functions and utilities
  • Implements impression and action tracking (view/click events) for buy buttons, free chapter forms, and blog CTAs to enable CTR and conversion rate calculations
  • Adds engagement tracking including scroll depth, blog read completion, outbound links, chapter expansions, and internal navigation
  • Includes A/B testing support for 10 blog promo variants (promo01-promo10) to identify best-performing CTAs

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/lib/analytics.ts New analytics utility module with type-safe event tracking functions, TypeScript interfaces, and helper utilities for GA4 integration
src/components/pages/Home/components/BuyButtons.astro Adds view and click tracking for buy buttons with format (print/ebook) and location tracking
src/components/pages/Home/FreeChapter.astro Implements form view and submission tracking for conversion rate analysis
src/components/blog/BookPromo.astro Adds CTA tracking with variant support (promo01-promo10) for A/B testing; changes double quotes to single quotes in alt text
src/components/blog/BlogLayout.astro Implements scroll depth tracking (25%, 50%, 75%, 100%) and blog read completion events
src/components/pages/Home/Chapters.astro Tracks chapter expansion events to measure content engagement
src/components/pages/Home/Hero.astro Updates BuyButtons component to pass location="hero"
src/components/pages/Home/Reviews.astro Updates BuyButtons component to pass location="reviews"
src/components/pages/Home/Quotes.astro Updates BuyButtons component to pass location="quotes"
src/components/pages/Home/ProblemStatement.astro Updates BuyButtons component to pass location="problem_statement"
src/components/pages/Home/ActionPlan.astro Updates BuyButtons component to pass location="action_plan"
src/Layout.astro Adds global outbound link and internal navigation tracking via event delegation
.claude/settings.local.json Adds permissions for analytics-related MCP tools and bash commands
.claude/features/analytics-events-tracking/tasks.md Comprehensive documentation of implementation tasks, event schemas, and success metrics

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lmammino lmammino force-pushed the feat/analytics-events-tracking branch from 4ad4373 to c8b5c42 Compare January 30, 2026 11:54
@lmammino
Copy link
Member Author

@copilot check if all the open comments have been resolved to your liking!

Copy link

Copilot AI commented Jan 30, 2026

@lmammino I've opened a new pull request, #31, to work on those changes. Once the pull request is ready, I'll request review from you.

@lmammino
Copy link
Member Author

@copilot I have already done the fixes, just review if all looks good now! 😞

Copy link

Copilot AI commented Jan 30, 2026

@lmammino I've opened a new pull request, #32, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 14 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lmammino lmammino merged commit 9479512 into main Jan 30, 2026
1 check passed
@lmammino lmammino deleted the feat/analytics-events-tracking branch January 30, 2026 14:29
lmammino added a commit that referenced this pull request Feb 2, 2026
* feat: add analytics tracking

* chore: typing improvements

* chore: fixes after review

* fix: small glitch in book promo (image not clickable)

* chore: various fixes and improvements after review

* chore: small linting fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants