Skip to content

Analytics SDK: enable autoPageView by default (opt-out) in IIFE and React/Next.js #36090

@oidacra

Description

@oidacra

Description

The @dotcms/analytics SDK currently requires explicit opt-in for automatic page view tracking, and the React SDK ignores the autoPageView flag entirely:

Mode Current behavior Desired behavior
IIFE (ca.min.js) autoPageView defaults to false — requires data-analytics-auto-page-view="true" On by default — only data-analytics-auto-page-view="false" disables it
Traditional (server-rendered, auto-injected) If the autoPageView app secret is missing, the attribute renders as "" → SDK falls back to falsepage views silently off, contradicting the app config contract ("defaults to true") On by default — empty attribute resolves to enabled; only "false" (admin toggled it off) disables
React / Next.js (<DotContentAnalytics>) Router tracker always fires page views, autoPageView config is ignored On by default — respects autoPageView: false to disable
Core programmatic (initializeContentAnalytics) Manual pageView() only Unchanged (out of scope)

The goal is that analytics works out-of-the-box: a developer who installs the script or the React provider gets page view tracking without extra configuration, and can explicitly opt out with false.

Affected code

  • core-web/libs/sdk/analytics/src/lib/core/shared/utils/dot-analytics.utils.tsgetAnalyticsConfig() fallback defaults (autoPageView: false in both return paths) and attribute parsing (autoPageViewAttr === 'true')
  • core-web/libs/sdk/analytics/src/lib/react/components/DotContentAnalytics.tsxuseRouterTracker is called unconditionally (also currently inside an if, violating rules-of-hooks)
  • core-web/libs/sdk/analytics/src/lib/standalone.ts — auto pageview gate (logic stays, driven by new default)
  • core-web/libs/sdk/analytics/src/lib/core/shared/models/library.model.tsautoPageView?: boolean JSDoc should document the new default

Traditional injection path (reference only — no Java changes expected, the SDK fix aligns it automatically):

  • dotCMS/src/main/java/com/dotcms/analytics/web/AnalyticsWebAPIImpl.javagetJSCode() replaces ${autoPageView} with the app secret, or blank when the secret is missing
  • dotCMS/src/main/resources/ca/html/analytics_head.html — injected script tag always renders data-analytics-auto-page-view="${autoPageView}" (so a missing secret produces an empty attribute, not an absent one)
  • dotCMS/src/main/resources/apps/dotContentAnalytics-config.yml — app param autoPageView declares value: true and documents "If not specified, it defaults to true"

Acceptance Criteria

IIFE (standalone script)

  • When data-analytics-auto-page-view is not present, auto page view is enabled and a pageview event fires on DOM ready
  • When data-analytics-auto-page-view="false", auto page view is disabled (existing "Auto page view is disabled" console warning still shown)
  • Any value other than the literal string "false" (e.g. "true", "no", "0", empty string) results in auto page view enabled (opt-out semantics: attr !== 'false')
  • autoPageView: false set via the data-analytics-config JSON attribute also disables auto page view; explicit attribute still takes precedence over JSON config
  • Both default-config return paths of getAnalyticsConfig() (script found / script not found) default autoPageView to true

Traditional (server-rendered pages, auto-injected script)

  • When the autoPageView app secret is missing (script tag rendered with data-analytics-auto-page-view=""), auto page view is enabled — matching the app config contract ("If not specified, it defaults to true")
  • When the admin sets Enable Auto Page Views = off in the Content Analytics app (attribute renders "false"), auto page view is disabled
  • When the secret is stored as true (default app value), auto page view is enabled (current behavior preserved)
  • Verified end-to-end against a dotCMS-rendered LIVE page with the auto-injected script from AnalyticsWebAPIImpl (manual or e2e check — no Java changes expected)

React / Next.js

  • When config.autoPageView is undefined or true, <DotContentAnalytics> tracks page views on App Router route changes (current behavior preserved)
  • When config.autoPageView is explicitly false, no automatic page views are sent; manual pageView() via useContentAnalytics still works
  • useRouterTracker is invoked unconditionally at the top level of the component (fix the conditional hook call); the enable/disable decision is passed as data, not by skipping the hook

Docs & types

  • JSDoc in getAnalyticsConfig() and library.model.ts updated — remove "autoPageView default to false" wording, document the opt-out default
  • Unit tests updated/added for: missing attribute, "false", invalid values, JSON config override (IIFE) and autoPageView: false (React tracker)

Additional Context

  • Intentional behavior change: existing IIFE installations without the attribute will start sending automatic pageviews after upgrading. This is the desired out-of-the-box behavior.
  • Traditional sites are the main production consumer of the IIFE: today a site whose Content Analytics app was saved before the autoPageView param existed (or with partial secrets) gets an empty attribute and silently loses page views. The opt-out semantics (attr !== 'false') fix this without touching the Java injection code.
  • Related: Document dotCMS Analytics for the Early Adopter Program #35894 (docs/implementation mismatch for @dotcms/analytics defaults)

Metadata

Metadata

Assignees

Type

No fields configured for Task.

Projects

Status
New

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions