Skip to content

feat: detect visitor type and report human/crawler/AI traffic to GA4 - #122

Merged
maximgorbatyuk merged 2 commits into
mainfrom
feat/visitor-type-analytics
Jun 15, 2026
Merged

maximgorbatyuk merged 2 commits into
mainfrom
feat/visitor-type-analytics

Conversation

@maximgorbatyuk

@maximgorbatyuk maximgorbatyuk commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

Classify each request (human / search-crawler / AI agent) from its User-Agent in the SSR server and expose it via res.locals + the X-Visitor-Type header.

Report a visitor_hit GA4 event per page render so all three buckets land in one comparable dimension: humans client-side via VisitorAnalyticsService, bots via the GA4 Measurement Protocol (env-gated on GA_MP_API_SECRET, bot User-Agent omitted so GA4's bot filter does not drop the hit). Measurement is scoped to Angular page renders, not static/well-known/sitemap requests.

The MP secret is injected through the deploy pipeline (GitHub Actions secret -> ssh-action envs -> docker-compose substitution).

Summary by CodeRabbit

Release Notes

  • New Features

    • Added visitor type analytics to distinguish human traffic from search crawlers and AI agents
    • Integrated GA4 Measurement Protocol for server-side visitor tracking
    • SSR server now classifies requests and sends analytics events for non-human visitors
  • Documentation

    • Added guides on configuring GA4 custom dimensions for visitor types
    • Documented runtime environment variables and SSR server configuration
    • Updated analytics documentation with visitor classification details
  • Tests

    • Added test coverage for visitor classification and GA4 event payloads
  • Chores

    • Updated deployment and Docker configuration to support GA4 Measurement Protocol

Classify each request (human / search-crawler / AI agent) from its User-Agent
in the SSR server and expose it via res.locals + the X-Visitor-Type header.

Report a visitor_hit GA4 event per page render so all three buckets land in one
comparable dimension: humans client-side via VisitorAnalyticsService, bots via
the GA4 Measurement Protocol (env-gated on GA_MP_API_SECRET, bot User-Agent
omitted so GA4's bot filter does not drop the hit). Measurement is scoped to
Angular page renders, not static/well-known/sitemap requests.

The MP secret is injected through the deploy pipeline (GitHub Actions secret ->
ssh-action envs -> docker-compose substitution).
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@maximgorbatyuk, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 34 minutes and 43 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 76b0c0ce-bd8e-46ea-b5a0-4ef97c312e77

📥 Commits

Reviewing files that changed from the base of the PR and between 5fbacf7 and 956380f.

📒 Files selected for processing (4)
  • docs/ga-ssr-visitor-type.md
  • docs/interactions.md
  • src/app/services/api.service.ts
  • src/app/services/visitor-analytics.service.ts

Walkthrough

Adds server-side User-Agent classification into "human", "search-crawler", or "ai-agent", a GA4 Measurement Protocol utility that fire-and-forget POSTs visitor_hit events for non-human SSR requests, a client-side VisitorAnalyticsService for human navigation tracking, GA_MP_API_SECRET injection via CI and docker-compose, and supporting documentation.

Changes

Visitor Analytics: UA Classification + GA4 Measurement Protocol

Layer / File(s) Summary
VisitorType classifier
src/server/visitor-type.util.ts, src/server/visitor-type.util.spec.ts
Exports VisitorType union and classifyVisitor function matching User-Agent strings against ordered regex lists for "ai-agent", "search-crawler", and "human", with full test coverage across AI agents, crawlers, browsers, and undefined input.
GA4 Measurement Protocol utility
src/server/ga-measurement-protocol.util.ts, src/server/ga-measurement-protocol.util.spec.ts
Adds clientIdFromUserAgent (FNV-1a hash), VisitorHitPayload interface, buildVisitorHitPayload, and sendVisitorHit (fire-and-forget POST, no-op without GA_MP_API_SECRET), with tests for id stability and payload correctness.
Express middleware and SSR measurement
src/server.ts
Registers middleware storing visitorType in res.locals and setting the X-Visitor-Type header; inserts post-render logic calling sendVisitorHit for non-human visitors with a constructed absolute URL.
Client-side VisitorAnalyticsService
src/app/services/visitor-analytics.service.ts, src/app/app.component.ts
Creates VisitorAnalyticsService that emits visitor_hit with visitor_type: "human" on each NavigationEnd in browser environments; wires init() into AppComponent.ngOnInit.
Secret injection: CI and docker-compose
.github/workflows/deploy.yml, docker-compose.frontend.yml
Injects GA_MP_API_SECRET from GitHub Actions secrets into the SSH deploy step and adds it to the frontend service in docker-compose.
Documentation
README.md, docs/ga-ssr-visitor-type.md, docs/geo.md, docs/interactions.md
Adds README section on SSR runtime env vars, a new GA4 custom dimension setup guide, and updates to geo and interactions docs describing classification behavior, event names, and configuration surface.

Sequence Diagram(s)

sequenceDiagram
  participant Browser
  participant ExpressSSR as Express SSR
  participant classifyVisitor
  participant AngularSSR as Angular Renderer
  participant sendVisitorHit
  participant GA4_MP as GA4 Measurement Protocol
  participant VisitorAnalyticsService

  Browser->>ExpressSSR: HTTP GET /page with User-Agent
  ExpressSSR->>classifyVisitor: userAgent string
  classifyVisitor-->>ExpressSSR: VisitorType human or crawler or ai-agent
  ExpressSSR->>ExpressSSR: store in res.locals, set X-Visitor-Type header
  ExpressSSR->>AngularSSR: render page
  AngularSSR-->>ExpressSSR: rendered HTML
  alt visitorType is not human
    ExpressSSR->>sendVisitorHit: visitorType, absolute URL, userAgent
    sendVisitorHit->>GA4_MP: POST visitor_hit event fire-and-forget
  end
  ExpressSSR-->>Browser: HTML + X-Visitor-Type header
  Note over Browser,VisitorAnalyticsService: On NavigationEnd in browser
  Browser->>VisitorAnalyticsService: NavigationEnd event
  VisitorAnalyticsService->>GA4_MP: gtag visitor_hit with visitor_type human
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: implementing visitor type detection and GA4 traffic reporting across human, crawler, and AI categories.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/visitor-type-analytics

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
src/app/services/visitor-analytics.service.ts (1)

6-6: ⚡ Quick win

Use the configured environment alias instead of a direct src/... path.

Please switch this import to @environments/environment to follow the repo alias contract.

Suggested fix
-import { environment } from "src/environments/environment";
+import { environment } from "`@environments/environment`";

As per coding guidelines, "**/*.ts: Use path alias @environments/* to reference files in the src/environments/ directory`".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/app/services/visitor-analytics.service.ts` at line 6, The import
statement in visitor-analytics.service.ts is using a direct filesystem path
instead of the configured path alias. Replace the import path from
"src/environments/environment" to "`@environments/environment`" to follow the
repository's path alias conventions for the environments directory.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/ga-ssr-visitor-type.md`:
- Around line 25-27: The instruction at step 2 incorrectly identifies
G-6GS9Y7GJD3 as a property ID when it is actually a web stream Measurement ID.
Clarify the instruction by either: specifying that users should select the GA4
Property ID (not the Measurement ID) in the property selection dropdown at the
top left, or if the intent is to help users locate the Measurement ID for
tracking code implementation, redirect them to navigate to Admin > Data Streams
where they can find the G-6GS9Y7GJD3 Measurement ID for the techinterview.space
web stream. Remove the misleading reference to selecting a "resource (property)"
using the Measurement ID.

In `@docs/interactions.md`:
- Line 117: The documentation row for GA_MP_API_SECRET incorrectly describes the
runtime source as coming from a Droplet .env file, which misleads operators
about where to set this configuration. Update the row to accurately reflect the
actual flow where the secret originates from GitHub Actions, passes through an
SSH session, and is then interpolated by docker-compose, rather than pointing
operators to a Droplet .env file.

In `@src/app/services/visitor-analytics.service.ts`:
- Around line 21-32: The init() method in VisitorAnalyticsService can be called
multiple times, creating duplicate subscriptions to router.events that cause the
track() method to be invoked multiple times for the same navigation event. Make
init() idempotent by adding a private flag property to track whether
initialization has already occurred, then check this flag at the start of init()
and return early if already initialized. Alternatively, apply the
takeUntilDestroyed() operator to the pipe chain to ensure automatic subscription
cleanup when the service is destroyed, preventing duplicate subscriptions from
accumulating.

In `@src/server/ga-measurement-protocol.util.ts`:
- Around line 79-86: The fetch call in the function is directly interpolating
measurement_id and api_secret into the query string without URL encoding, which
is not a best practice and could cause malformed requests if the secret format
changes or contains special characters. Replace the template string
interpolation with URLSearchParams to properly encode the query parameters.
Create a URLSearchParams object with the measurement_id and api_secret as
key-value pairs, then append it to the GA_MP_ENDPOINT URL using the search
property before passing it to the fetch call.

---

Nitpick comments:
In `@src/app/services/visitor-analytics.service.ts`:
- Line 6: The import statement in visitor-analytics.service.ts is using a direct
filesystem path instead of the configured path alias. Replace the import path
from "src/environments/environment" to "`@environments/environment`" to follow the
repository's path alias conventions for the environments directory.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 13575a33-a07b-49bd-bfe8-b2ad20596d23

📥 Commits

Reviewing files that changed from the base of the PR and between 04aa1b7 and 5fbacf7.

📒 Files selected for processing (13)
  • .github/workflows/deploy.yml
  • README.md
  • docker-compose.frontend.yml
  • docs/ga-ssr-visitor-type.md
  • docs/geo.md
  • docs/interactions.md
  • src/app/app.component.ts
  • src/app/services/visitor-analytics.service.ts
  • src/server.ts
  • src/server/ga-measurement-protocol.util.spec.ts
  • src/server/ga-measurement-protocol.util.ts
  • src/server/visitor-type.util.spec.ts
  • src/server/visitor-type.util.ts

Comment thread docs/ga-ssr-visitor-type.md Outdated
Comment thread docs/interactions.md Outdated
Comment thread src/app/services/visitor-analytics.service.ts
Comment thread src/server/ga-measurement-protocol.util.ts
@maximgorbatyuk
maximgorbatyuk merged commit 073059a into main Jun 15, 2026
2 checks passed
@maximgorbatyuk
maximgorbatyuk deleted the feat/visitor-type-analytics branch June 15, 2026 10:20
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.

1 participant